{
  "title": "Charging sessions",
  "icon": "bolt.car",
  "sidebar": false,
  "period": {
    "choices": [
      {
        "id": "30d",
        "label": "30 days",
        "last_days": 30
      },
      {
        "id": "90d",
        "label": "90 days",
        "last_days": 90
      },
      {
        "id": "365d",
        "label": "Year",
        "last_days": 365
      }
    ]
  },
  "sources": {
    "c": {
      "url": "https://api.tessie.com",
      "secret": "tessie",
      "every": "1h",
      "requests": {
        "charges": "/YOUR_VIN/charges?from={period.start_unix}&to={period.end_unix}"
      },
      "derive": [
        {
          "name": "sessions",
          "value": {
            "count": "charges.results"
          }
        },
        {
          "name": "kwh",
          "value": {
            "sum": "energy_added",
            "of": "charges.results",
            "round": 1
          }
        },
        {
          "name": "cost",
          "value": {
            "sum": "cost",
            "of": "charges.results",
            "where": {
              "path": "cost",
              "gte": 0
            },
            "round": 2
          }
        },
        {
          "name": "priced",
          "value": {
            "count": "charges.results",
            "where": {
              "path": "cost",
              "gte": 0
            }
          }
        },
        {
          "name": "priced_kwh",
          "value": {
            "sum": "energy_added",
            "of": "charges.results",
            "where": {
              "path": "cost",
              "gte": 0
            }
          }
        },
        {
          "name": "per_kwh",
          "value": {
            "divide": [
              "cost",
              "priced_kwh"
            ],
            "round": 2
          }
        },
        {
          "name": "avg_kwh",
          "value": {
            "avg": "energy_added",
            "of": "charges.results",
            "round": 1
          }
        },
        {
          "name": "miles",
          "value": {
            "sum": "miles_added",
            "of": "charges.results",
            "round": 0
          }
        },
        {
          "name": "fast",
          "value": {
            "count": "charges.results",
            "where": {
              "any": [
                {
                  "path": "is_supercharger",
                  "equals": true
                },
                {
                  "path": "is_fast_charger",
                  "equals": true
                }
              ]
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "energy",
      "type": "stat",
      "label": "Energy added",
      "unit": "kWh",
      "value": 0,
      "source": {
        "ref": "c",
        "map": {
          "value": "kwh"
        }
      }
    },
    {
      "id": "spent",
      "type": "stat",
      "label": "Cost",
      "unit": "USD",
      "value": 0,
      "source": {
        "ref": "c",
        "map": {
          "value": "cost"
        }
      }
    },
    {
      "id": "per-session",
      "type": "sparkline",
      "label": "Each session",
      "unit": "kWh",
      "points": [
        0,
        0
      ],
      "change": false,
      "source": {
        "ref": "c",
        "empty": {
          "when": {
            "path": "sessions",
            "lt": 2
          },
          "text": "Fewer than two sessions in these days"
        },
        "map": {
          "points": {
            "each": "charges.results",
            "sort": {
              "by": "started_at"
            },
            "value": {
              "path": "energy_added"
            }
          },
          "times": {
            "each": "charges.results",
            "sort": {
              "by": "started_at"
            },
            "value": {
              "path": "started_at"
            }
          }
        }
      }
    },
    {
      "id": "summary",
      "type": "facts",
      "label": "{period.dates}",
      "items": [
        {
          "key": "Sessions",
          "value": "—"
        },
        {
          "key": "Miles added",
          "value": "—"
        },
        {
          "key": "Average session",
          "value": "—"
        },
        {
          "key": "Cost per kWh",
          "value": "—"
        },
        {
          "key": "Fast charging",
          "value": "—"
        }
      ],
      "source": {
        "ref": "c",
        "when": {
          "path": "sessions",
          "gt": 0
        },
        "map": {
          "items[0].value": {
            "template": "{sessions}"
          },
          "items[1].value": {
            "template": "{miles} mi"
          },
          "items[2].value": {
            "template": "{avg_kwh} kWh"
          },
          "items[3].value": {
            "if": {
              "path": "priced",
              "gt": 0
            },
            "then": {
              "template": "${per_kwh}"
            },
            "else": {
              "template": "No prices"
            }
          },
          "items[4].value": {
            "template": "{fast}"
          }
        }
      }
    },
    {
      "id": "sessions-list",
      "type": "list",
      "label": "Sessions",
      "max_items": 15,
      "items": [],
      "source": {
        "ref": "c",
        "empty": {
          "when": {
            "path": "sessions",
            "equals": 0
          },
          "text": "No charging in these days"
        },
        "map": {
          "items": {
            "each": "charges.results",
            "fields": {
              "text": {
                "template": "{started_at|day}"
              },
              "detail": {
                "if": {
                  "path": "cost",
                  "gte": 0
                },
                "then": {
                  "template": "{energy_added|round:1} kWh · ${cost|round:2}"
                },
                "else": {
                  "template": "{energy_added|round:1} kWh"
                }
              },
              "right": {
                "template": "{starting_battery}–{ending_battery}%"
              }
            }
          }
        }
      }
    }
  ]
}
