{
    "annotations": {
        "list": [
            {
                "builtIn": 1,
                "datasource": "-- Grafana --",
                "enable": true,
                "hide": true,
                "iconColor": "rgba(0, 211, 255, 1)",
                "name": "Annotations & Alerts",
                "target": {
                    "limit": 100,
                    "matchAny": false,
                    "tags": [],
                    "type": "dashboard"
                },
                "type": "dashboard"
            }
        ]
    },
    "editable": true,
    "fiscalYearStartMonth": 0,
    "graphTooltip": 0,
    "iteration": 1642779900099,
    "links": [
        {
            "icon": "dashboard",
            "tags": [],
            "title": "TeslaMate",
            "tooltip": "",
            "type": "link",
            "url": "[[base_url:raw]]"
        },
        {
            "asDropdown": true,
            "icon": "external link",
            "tags": ["tesla"],
            "title": "Dashboards",
            "type": "dashboards"
        }
    ],
    "liveNow": false,
    "panels": [
        {
            "collapsed": false,
            "datasource": "TeslaMate",
            "gridPos": {
                "h": 1,
                "w": 24,
                "x": 0,
                "y": 0
            },
            "id": 4,
            "panels": [],
            "repeat": "car_id",
            "title": "$car_id",
            "type": "row"
        },
        {
            "fieldConfig": {
                "defaults": {
                    "color": {
                        "mode": "thresholds"
                    },
                    "custom": {
                        "align": "auto",
                        "displayMode": "auto"
                    },
                    "mappings": [],
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "green",
                                "value": null
                            },
                            {
                                "color": "red",
                                "value": 80
                            }
                        ]
                    }
                },
                "overrides": [
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "start_date"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Start"
                            },
                            {
                                "id": "unit",
                                "value": "dateTimeAsLocal"
                            },
                            {
                                "id": "links",
                                "value": [
                                    {
                                        "targetBlank": false,
                                        "title": "",
                                        "url": "d/zm7wN6Zgz?from=${__data.fields.start_date_ts.numeric}&to=${__data.fields.end_date_ts.numeric}"
                                    }
                                ]
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.width",
                                "value": 180
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "end_date"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "End"
                            },
                            {
                                "id": "unit",
                                "value": "dateTimeAsLocal"
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.width",
                                "value": 180
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "range_diff_km"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "TR Loss"
                            },
                            {
                                "id": "unit",
                                "value": "lengthkm"
                            },
                            {
                                "id": "decimals",
                                "value": 2
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.width",
                                "value": 90
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "duration"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Period"
                            },
                            {
                                "id": "unit",
                                "value": "s"
                            },
                            {
                                "id": "decimals",
                                "value": 1
                            },
                            {
                                "id": "custom.displayMode",
                                "value": "color-text"
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "thresholds",
                                "value": {
                                    "mode": "absolute",
                                    "steps": [
                                        {
                                            "color": "rgb(133, 142, 133)",
                                            "value": null
                                        },
                                        {
                                            "color": "#56A64B",
                                            "value": 43200
                                        }
                                    ]
                                }
                            },
                            {
                                "id": "custom.minWidth",
                                "value": 100
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "range_lost_per_hour_km"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "TR Loss / h"
                            },
                            {
                                "id": "unit",
                                "value": "lengthkm"
                            },
                            {
                                "id": "decimals",
                                "value": 2
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.width",
                                "value": 90
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byRegexp",
                            "options": "/.*_ts/"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "short"
                            },
                            {
                                "id": "decimals",
                                "value": 2
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.hidden",
                                "value": true
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "standby"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Standby"
                            },
                            {
                                "id": "unit",
                                "value": "percentunit"
                            },
                            {
                                "id": "custom.displayMode",
                                "value": "color-text"
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "thresholds",
                                "value": {
                                    "mode": "absolute",
                                    "steps": [
                                        {
                                            "color": "#FF7383",
                                            "value": null
                                        },
                                        {
                                            "color": "#FFB357",
                                            "value": 0.3
                                        },
                                        {
                                            "color": "#56A64B",
                                            "value": 0.85
                                        }
                                    ]
                                }
                            },
                            {
                                "id": "decimals",
                                "value": 0
                            },
                            {
                                "id": "custom.width",
                                "value": 75
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "consumption"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "kWh"
                            },
                            {
                                "id": "unit",
                                "value": "kwatth"
                            },
                            {
                                "id": "decimals",
                                "value": 2
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.width",
                                "value": 100
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "avg_power"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Ø-Power"
                            },
                            {
                                "id": "unit",
                                "value": "watt"
                            },
                            {
                                "id": "decimals",
                                "value": 1
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.width",
                                "value": 80
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "range_lost_per_hour_mi"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "TR Loss / h"
                            },
                            {
                                "id": "unit",
                                "value": "lengthmi"
                            },
                            {
                                "id": "decimals",
                                "value": 2
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.width",
                                "value": 90
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "range_diff_mi"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "TR Loss"
                            },
                            {
                                "id": "unit",
                                "value": "lengthmi"
                            },
                            {
                                "id": "decimals",
                                "value": 2
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.width",
                                "value": 90
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "soc_diff"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "SOC"
                            },
                            {
                                "id": "unit",
                                "value": "percent"
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.width",
                                "value": 65
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "has_reduced_range"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": " "
                            },
                            {
                                "id": "custom.align",
                                "value": "center"
                            },
                            {
                                "id": "mappings",
                                "value": [
                                    {
                                        "options": {
                                            "0": {
                                                "color": "transparent",
                                                "index": 1,
                                                "text": " "
                                            },
                                            "1": {
                                                "color": "dark-blue",
                                                "index": 0,
                                                "text": "❄"
                                            }
                                        },
                                        "type": "value"
                                    }
                                ]
                            },
                            {
                                "id": "links",
                                "value": [
                                    {
                                        "title": "In cold weather, the estimated range loss cannot be estimated correctly and is therefore hidden.",
                                        "url": ""
                                    }
                                ]
                            },
                            {
                                "id": "custom.width",
                                "value": 5
                            }
                        ]
                    }
                ]
            },
            "gridPos": {
                "h": 23,
                "w": 24,
                "x": 0,
                "y": 1
            },
            "id": 2,
            "links": [],
            "options": {
                "footer": {
                    "fields": "",
                    "reducer": ["sum"],
                    "show": false
                },
                "showHeader": true
            },
            "pluginVersion": "8.3.4",
            "targets": [
                {
                    "alias": "",
                    "datasource": "TeslaMate",
                    "format": "table",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "with merge as (\n SELECT \n    c.start_date AS start_date,\n    c.end_date AS end_date,\n    c.start_ideal_range_km AS start_ideal_range_km,\n    c.end_ideal_range_km AS end_ideal_range_km,\n    c.start_rated_range_km AS start_rated_range_km,\n    c.end_rated_range_km AS end_rated_range_km,\n    start_battery_level,\n    end_battery_level,\n    p.usable_battery_level AS start_usable_battery_level,\n    NULL AS end_usable_battery_level,\n    p.odometer AS start_km,\n    p.odometer AS end_km\n FROM charging_processes c\n JOIN positions p ON c.position_id = p.id\n WHERE c.car_id = $car_id AND $__timeFilter(start_date)\n UNION\n SELECT \n    d.start_date AS start_date,\n    d.end_date AS end_date,\n    d.start_ideal_range_km AS start_ideal_range_km,\n    d.end_ideal_range_km AS end_ideal_range_km,\n    d.start_rated_range_km AS start_rated_range_km,\n    d.end_rated_range_km AS end_rated_range_km,\n    start_position.battery_level AS start_battery_level,\n    end_position.battery_level AS end_battery_level,\n    start_position.usable_battery_level AS start_usable_battery_level,\n    end_position.usable_battery_level AS end_usable_battery_level,\n    d.start_km AS start_km,\n    d.end_km AS end_km\n FROM drives d\n JOIN positions start_position ON d.start_position_id = start_position.id\n JOIN positions end_position ON d.end_position_id = end_position.id\n WHERE d.car_id = $car_id AND $__timeFilter(start_date)\n), \nv as (\n SELECT\n    lag(t.end_date) OVER w AS start_date,\n    t.start_date AS end_date,\n    lag(t.end_[[preferred_range]]_range_km) OVER w AS start_range,\n    t.start_[[preferred_range]]_range_km AS end_range,\n    lag(t.end_km) OVER w AS start_km,\n    t.start_km AS end_km,\n    EXTRACT(EPOCH FROM age(t.start_date, lag(t.end_date) OVER w)) AS duration,\n    lag(t.end_battery_level) OVER w AS start_battery_level,\n    lag(t.end_usable_battery_level) OVER w AS start_usable_battery_level,\n\t\tstart_battery_level AS end_battery_level,\n\t\tstart_usable_battery_level AS end_usable_battery_level,\n\t\tstart_battery_level > COALESCE(start_usable_battery_level, start_battery_level) AS has_reduced_range\n  FROM merge t\n  WINDOW w AS (ORDER BY t.start_date ASC)\n  ORDER BY start_date DESC\n)\n\nSELECT\n  round(extract(epoch FROM v.start_date)) * 1000 AS start_date_ts,\n  round(extract(epoch FROM v.end_date)) * 1000 AS end_date_ts,\n  -- Columns\n  v.start_date,\n  v.end_date,\n  v.duration,\n  (coalesce(s_asleep.sleep, 0) + coalesce(s_offline.sleep, 0)) / v.duration as standby,\n\t-greatest(v.start_battery_level - v.end_battery_level, 0) as soc_diff,\n\tCASE WHEN has_reduced_range THEN 1 ELSE 0 END as has_reduced_range,\n\tconvert_km(CASE WHEN has_reduced_range THEN NULL ELSE (v.start_range - v.end_range)::numeric END, '$length_unit') AS range_diff_$length_unit,\n  CASE WHEN has_reduced_range THEN NULL ELSE (v.start_range - v.end_range) * c.efficiency END AS consumption,\n  CASE WHEN has_reduced_range THEN NULL ELSE ((v.start_range - v.end_range) * c.efficiency) / (v.duration / 3600) * 1000 END as avg_power,\n  convert_km(CASE WHEN has_reduced_range THEN NULL ELSE ((v.start_range - v.end_range) / (v.duration / 3600))::numeric END, '$length_unit') AS range_lost_per_hour_[[length_unit]]\nFROM v,\n  LATERAL (\n    SELECT EXTRACT(EPOCH FROM sum(age(s.end_date, s.start_date))) as sleep\n    FROM states s\n    WHERE\n      state = 'asleep' AND\n      v.start_date <= s.start_date AND s.end_date <= v.end_date AND\n      s.car_id = $car_id\n  ) s_asleep,\n  LATERAL (\n    SELECT EXTRACT(EPOCH FROM sum(age(s.end_date, s.start_date))) as sleep\n    FROM states s\n    WHERE\n      state = 'offline' AND\n      v.start_date <= s.start_date AND s.end_date <= v.end_date AND\n      s.car_id = $car_id\n  ) s_offline\nJOIN cars c ON c.id = $car_id\nWHERE\n  v.duration > ($duration * 60 * 60)\n  AND v.start_range - v.end_range >= 0\n  AND v.end_km - v.start_km < 1;",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["value"],
                                "type": "column"
                            }
                        ]
                    ],
                    "timeColumn": "time",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "datasource": "TeslaMate",
            "title": "Vampire Drain",
            "transformations": [
                {
                    "id": "merge",
                    "options": {
                        "reducers": []
                    }
                }
            ],
            "type": "table"
        }
    ],
    "schemaVersion": 36,
    "style": "dark",
    "tags": ["tesla"],
    "templating": {
        "list": [
            {
                "current": {
                    "selected": false,
                    "text": "All",
                    "value": "$__all"
                },
                "datasource": "TeslaMate",
                "definition": "SELECT name AS __text, id AS __value FROM cars ORDER BY display_priority ASC, name ASC;",
                "hide": 2,
                "includeAll": true,
                "label": "Car",
                "multi": false,
                "name": "car_id",
                "options": [],
                "query": "SELECT name AS __text, id AS __value FROM cars ORDER BY display_priority ASC, name ASC;",
                "refresh": 1,
                "regex": "",
                "skipUrlSync": false,
                "sort": 0,
                "tagValuesQuery": "",
                "tagsQuery": "",
                "type": "query",
                "useTags": false
            },
            {
                "current": {
                    "tags": [],
                    "text": "6",
                    "value": "6"
                },
                "hide": 0,
                "includeAll": false,
                "label": "min. Idle Time (h)",
                "multi": false,
                "name": "duration",
                "options": [
                    {
                        "selected": false,
                        "text": "0",
                        "value": "0"
                    },
                    {
                        "selected": false,
                        "text": "1",
                        "value": "1"
                    },
                    {
                        "selected": false,
                        "text": "3",
                        "value": "3"
                    },
                    {
                        "selected": true,
                        "text": "6",
                        "value": "6"
                    },
                    {
                        "selected": false,
                        "text": "12",
                        "value": "12"
                    },
                    {
                        "selected": false,
                        "text": "18",
                        "value": "18"
                    },
                    {
                        "selected": false,
                        "text": "24",
                        "value": "24"
                    }
                ],
                "query": "0,1,3,6,12,18,24",
                "skipUrlSync": false,
                "type": "custom"
            },
            {
                "current": {
                    "selected": false,
                    "text": "km",
                    "value": "km"
                },
                "datasource": "TeslaMate",
                "definition": "select unit_of_length from settings limit 1;",
                "hide": 2,
                "includeAll": false,
                "label": "",
                "multi": false,
                "name": "length_unit",
                "options": [],
                "query": "select unit_of_length from settings limit 1;",
                "refresh": 1,
                "regex": "",
                "skipUrlSync": false,
                "sort": 0,
                "tagValuesQuery": "",
                "tagsQuery": "",
                "type": "query",
                "useTags": false
            },
            {
                "current": {
                    "selected": false,
                    "text": "ideal",
                    "value": "ideal"
                },
                "datasource": "TeslaMate",
                "definition": "select preferred_range from settings limit 1;",
                "hide": 2,
                "includeAll": false,
                "multi": false,
                "name": "preferred_range",
                "options": [],
                "query": "select preferred_range from settings limit 1;",
                "refresh": 1,
                "regex": "",
                "skipUrlSync": false,
                "sort": 0,
                "tagValuesQuery": "",
                "tagsQuery": "",
                "type": "query",
                "useTags": false
            },
            {
                "current": {
                    "selected": false,
                    "text": "http://localhost:4000",
                    "value": "http://localhost:4000"
                },
                "datasource": "TeslaMate",
                "definition": "select base_url from settings limit 1;",
                "hide": 2,
                "includeAll": false,
                "label": "",
                "multi": false,
                "name": "base_url",
                "options": [],
                "query": "select base_url from settings limit 1;",
                "refresh": 1,
                "regex": "",
                "skipUrlSync": false,
                "sort": 0,
                "tagValuesQuery": "",
                "tagsQuery": "",
                "type": "query",
                "useTags": false
            }
        ]
    },
    "time": {
        "from": "now-90d",
        "to": "now"
    },
    "timepicker": {
        "refresh_intervals": [
            "5s",
            "10s",
            "30s",
            "1m",
            "5m",
            "15m",
            "30m",
            "1h",
            "2h",
            "1d"
        ],
        "time_options": [
            "5m",
            "15m",
            "1h",
            "6h",
            "12h",
            "24h",
            "2d",
            "7d",
            "30d"
        ]
    },
    "timezone": "",
    "title": "Vampire Drain",
    "uid": "zhHx2Fggk",
    "version": 1,
    "weekStart": ""
}
