{
    "annotations": {
        "list": [
            {
                "$$hashKey": "object:30",
                "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": 1,
    "iteration": 1656104359102,
    "links": [
        {
            "icon": "doc",
            "tags": [],
            "targetBlank": true,
            "title": "Select last three drives",
            "type": "link",
            "url": "/d/FkUpJpQZk?from=$from"
        },
        {
            "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"
        },
        {
            "datasource": "TeslaMate",
            "fieldConfig": {
                "defaults": {
                    "color": {
                        "mode": "thresholds"
                    },
                    "custom": {
                        "hideFrom": {
                            "legend": false,
                            "tooltip": false,
                            "viz": false
                        }
                    },
                    "mappings": [],
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "green",
                                "value": null
                            }
                        ]
                    }
                },
                "overrides": []
            },
            "gridPos": {
                "h": 13,
                "w": 13,
                "x": 0,
                "y": 1
            },
            "id": 6,
            "maxDataPoints": 500,
            "options": {
                "basemap": {
                    "config": {},
                    "name": "Layer 0",
                    "type": "osm-standard"
                },
                "controls": {
                    "mouseWheelZoom": true,
                    "showAttribution": true,
                    "showDebug": false,
                    "showMeasure": false,
                    "showScale": false,
                    "showZoom": true
                },
                "layers": [
                    {
                        "config": {
                            "arrow": 0,
                            "style": {
                                "color": {
                                    "fixed": "dark-blue"
                                },
                                "lineWidth": 2,
                                "opacity": 1,
                                "rotation": {
                                    "fixed": 0,
                                    "max": 360,
                                    "min": -360,
                                    "mode": "mod"
                                },
                                "size": {
                                    "fixed": 3,
                                    "max": 15,
                                    "min": 2
                                },
                                "symbol": {
                                    "fixed": "img/icons/marker/circle.svg",
                                    "mode": "fixed"
                                },
                                "symbolAlign": {
                                    "horizontal": "center",
                                    "vertical": "center"
                                },
                                "textConfig": {
                                    "fontSize": 12,
                                    "offsetX": 0,
                                    "offsetY": 0,
                                    "textAlign": "center",
                                    "textBaseline": "middle"
                                }
                            }
                        },
                        "name": "Layer 1",
                        "tooltip": true,
                        "type": "route"
                    }
                ],
                "tooltip": {
                    "mode": "details"
                },
                "view": {
                    "allLayers": true,
                    "id": "fit",
                    "lat": 0,
                    "lon": 0,
                    "zoom": 15
                }
            },
            "pluginVersion": "10.1.2",
            "targets": [
                {
                    "datasource": "TeslaMate",
                    "editorMode": "code",
                    "format": "time_series",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "SELECT\n\t$__timeGroup(date, '5s') AS time,\n\tavg(latitude) AS latitude,\n\tavg(longitude) AS longitude\nFROM\n\tpositions\nWHERE\n  car_id = $car_id AND\n\t$__timeFilter(date)\nGROUP BY\n\t1\nORDER BY\n\t1 ASC",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["latitude"],
                                "type": "column"
                            }
                        ]
                    ],
                    "sql": {
                        "columns": [
                            {
                                "parameters": [],
                                "type": "function"
                            }
                        ],
                        "groupBy": [
                            {
                                "property": {
                                    "type": "string"
                                },
                                "type": "groupBy"
                            }
                        ],
                        "limit": 50
                    },
                    "table": "addresses",
                    "timeColumn": "inserted_at",
                    "timeColumnType": "timestamp",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "transformations": [],
            "transparent": true,
            "type": "geomap"
        },
        {
            "datasource": "TeslaMate",
            "fieldConfig": {
                "defaults": {
                    "mappings": [],
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "#c7d0d9",
                                "value": null
                            }
                        ]
                    },
                    "unit": "none"
                },
                "overrides": [
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "distance_km"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "km"
                            },
                            {
                                "id": "displayName",
                                "value": "Distance"
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "distance_mi"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "mi"
                            },
                            {
                                "id": "displayName",
                                "value": "Distance"
                            }
                        ]
                    }
                ]
            },
            "gridPos": {
                "h": 2,
                "w": 6,
                "x": 13,
                "y": 1
            },
            "id": 10,
            "links": [],
            "maxDataPoints": 100,
            "options": {
                "colorMode": "value",
                "fieldOptions": {
                    "calcs": ["lastNotNull"]
                },
                "graphMode": "area",
                "justifyMode": "auto",
                "orientation": "auto",
                "reduceOptions": {
                    "calcs": ["mean"],
                    "fields": "",
                    "values": true
                },
                "textMode": "auto"
            },
            "pluginVersion": "10.1.2",
            "targets": [
                {
                    "format": "table",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "SELECT convert_km((max(odometer) - min(odometer))::numeric, '$length_unit') as \"distance_$length_unit\"\nFROM positions\nWHERE car_id = $car_id AND $__timeFilter(date)\nORDER BY 1;",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["latitude"],
                                "type": "column"
                            }
                        ]
                    ],
                    "table": "addresses",
                    "timeColumn": "inserted_at",
                    "timeColumnType": "timestamp",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "type": "stat"
        },
        {
            "datasource": "TeslaMate",
            "fieldConfig": {
                "defaults": {
                    "color": {
                        "mode": "palette-classic"
                    },
                    "custom": {
                        "hideFrom": {
                            "legend": false,
                            "tooltip": false,
                            "viz": false
                        }
                    },
                    "decimals": 1,
                    "mappings": [],
                    "unit": "dtdurations"
                },
                "overrides": [
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "charging (AC)"
                        },
                        "properties": [
                            {
                                "id": "color",
                                "value": {
                                    "fixedColor": "#73BF69",
                                    "mode": "fixed"
                                }
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "charging (DC)"
                        },
                        "properties": [
                            {
                                "id": "color",
                                "value": {
                                    "fixedColor": "#FADE2A",
                                    "mode": "fixed"
                                }
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "driving"
                        },
                        "properties": [
                            {
                                "id": "color",
                                "value": {
                                    "fixedColor": "#5794F2",
                                    "mode": "fixed"
                                }
                            }
                        ]
                    }
                ]
            },
            "gridPos": {
                "h": 8,
                "w": 5,
                "x": 19,
                "y": 1
            },
            "id": 38,
            "links": [],
            "maxDataPoints": 3,
            "options": {
                "displayLabels": ["name", "percent"],
                "legend": {
                    "calcs": [],
                    "displayMode": "list",
                    "placement": "bottom",
                    "showLegend": true,
                    "values": ["value"]
                },
                "pieType": "pie",
                "reduceOptions": {
                    "calcs": ["lastNotNull"],
                    "fields": "",
                    "values": true
                },
                "tooltip": {
                    "mode": "single",
                    "sort": "none"
                }
            },
            "targets": [
                {
                    "format": "time_series",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "SELECT\n\tnow() AS time,\n\tsum(extract(epoch FROM end_position.date - start_position.date)) as duration_sec,\n\t'driving' as metric\nFROM\n\tdrives\n\tJOIN positions start_position ON start_position_id = start_position.id\n\tJOIN positions end_position ON end_position_id = end_position.id\nWHERE\n\tdrives.car_id = $car_id\n\tAND $__timeFilter(start_date);",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["latitude"],
                                "type": "column"
                            }
                        ]
                    ],
                    "table": "addresses",
                    "timeColumn": "inserted_at",
                    "timeColumnType": "timestamp",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                },
                {
                    "format": "time_series",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "WITH charges_current AS (\n  SELECT\n\t\tcp.id,\n  \textract(epoch FROM LEAST(end_date, $__timeTo()) - GREATEST(start_date, $__timeFrom())) as duration_sec,\n\t\tCASE WHEN NULLIF(mode() within group (order by charger_phases),0) is null THEN 'charging (DC)'\n\t\t\t\t ELSE 'charging (AC)'\n\t\tEND AS current\n\tFROM charging_processes cp\n  RIGHT JOIN charges ON cp.id = charges.charging_process_id\n  WHERE\n\t  cp.car_id = $car_id\n\t  AND cp.charge_energy_added > 0\n  \tAND ($__timeFilter(start_date) OR $__timeFilter(end_date))\n  GROUP BY 1,2\n),\n\ncharges_total AS (\n  SELECT\n  \tsum(duration_sec) AS duration_sec,\n  \tcurrent AS metric\n  FROM charges_current\n  GROUP BY 2\n  ORDER BY metric\n)\n\nSELECT\n\tnow() AS time,\n\tcoalesce(duration_sec, 0) as duration_sec,\n  metric\nFROM\n\tcharges_total;",
                    "refId": "B",
                    "select": [
                        [
                            {
                                "params": ["latitude"],
                                "type": "column"
                            }
                        ]
                    ],
                    "table": "addresses",
                    "timeColumn": "inserted_at",
                    "timeColumnType": "timestamp",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "type": "piechart"
        },
        {
            "datasource": "TeslaMate",
            "description": "",
            "fieldConfig": {
                "defaults": {
                    "mappings": [
                        {
                            "options": {
                                "match": "null",
                                "result": {
                                    "text": "N/A"
                                }
                            },
                            "type": "special"
                        }
                    ],
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "#c7d0d9",
                                "value": null
                            }
                        ]
                    },
                    "unit": "none"
                },
                "overrides": [
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "speed_km"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "velocitykmh"
                            },
                            {
                                "id": "displayName",
                                "value": "excl. breaks"
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "speed_mi"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "velocitymph"
                            },
                            {
                                "id": "displayName",
                                "value": "excl. breaks"
                            }
                        ]
                    }
                ]
            },
            "gridPos": {
                "h": 2,
                "w": 6,
                "x": 13,
                "y": 3
            },
            "id": 26,
            "links": [],
            "maxDataPoints": 100,
            "options": {
                "colorMode": "value",
                "fieldOptions": {
                    "calcs": ["lastNotNull"]
                },
                "graphMode": "none",
                "justifyMode": "auto",
                "orientation": "horizontal",
                "reduceOptions": {
                    "calcs": ["mean"],
                    "fields": "",
                    "values": false
                },
                "textMode": "value_and_name"
            },
            "pluginVersion": "10.1.2",
            "targets": [
                {
                    "format": "table",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "SELECT\n  convert_km(sum(end_position.odometer - start_position.odometer)::numeric, '$length_unit') / (sum(extract(epoch FROM end_position.date - start_position.date)) / 3600) as \"speed_$length_unit\"\nFROM\n\tdrives\n\tJOIN positions start_position ON start_position_id = start_position.id\n\tJOIN positions end_position ON end_position_id = end_position.id\nWHERE\n\tdrives.car_id = $car_id\n\tAND $__timeFilter(start_date)",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["latitude"],
                                "type": "column"
                            }
                        ]
                    ],
                    "table": "addresses",
                    "timeColumn": "inserted_at",
                    "timeColumnType": "timestamp",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "type": "stat"
        },
        {
            "datasource": "TeslaMate",
            "fieldConfig": {
                "defaults": {
                    "mappings": [],
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "#c7d0d9",
                                "value": null
                            }
                        ]
                    },
                    "unit": "none"
                },
                "overrides": [
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "speed_km"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "velocitykmh"
                            },
                            {
                                "id": "displayName",
                                "value": "incl. DC charging"
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "speed_mi"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "velocitymph"
                            },
                            {
                                "id": "displayName",
                                "value": "incl. DC charging"
                            }
                        ]
                    }
                ]
            },
            "gridPos": {
                "h": 2,
                "w": 6,
                "x": 13,
                "y": 5
            },
            "id": 28,
            "links": [],
            "maxDataPoints": 100,
            "options": {
                "colorMode": "value",
                "fieldOptions": {
                    "calcs": ["lastNotNull"]
                },
                "graphMode": "none",
                "justifyMode": "auto",
                "orientation": "horizontal",
                "reduceOptions": {
                    "calcs": ["mean"],
                    "fields": "",
                    "values": false
                },
                "textMode": "value_and_name"
            },
            "pluginVersion": "10.1.2",
            "targets": [
                {
                    "format": "table",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "WITH dc_charges AS (\n  SELECT\n\t\tcp.id,\n    extract(epoch FROM cp.end_date - cp.start_date) as duration_sec,\n\t\tCASE WHEN NULLIF(mode() within group (order by charger_phases),0) is null THEN 'DC'\n\t\t\t\t ELSE 'AC'\n\t\tEND AS current\n\tFROM charging_processes cp\n  RIGHT JOIN charges ON cp.id = charges.charging_process_id\n  WHERE\n\t  cp.car_id = $car_id\n\t  AND cp.charge_energy_added > 0\n    AND ($__timeFilter(start_date) OR $__timeFilter(end_date))\n  GROUP BY 1,2\n),\n\ndata AS (\n  (\n  SELECT\n    sum(end_position.odometer - start_position.odometer) as distance, \n    sum(extract(epoch FROM end_position.date - start_position.date)) as duration_sec\n  FROM\n    drives\n    JOIN positions start_position ON start_position_id = start_position.id\n    JOIN positions end_position ON end_position_id = end_position.id\n  WHERE\n    drives.car_id = $car_id\n    AND $__timeFilter(start_date)\n  ) UNION ALL (\n  SELECT\n    NULL as distance,\n    sum(duration_sec)\n  FROM\n    dc_charges\n  WHERE\n   current = 'DC'\n  )\n)\n\nSELECT convert_km(sum(distance)::numeric, '$length_unit') / (sum(duration_sec) / 3600) as \"speed_$length_unit\"\nfrom data",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["latitude"],
                                "type": "column"
                            }
                        ]
                    ],
                    "table": "addresses",
                    "timeColumn": "inserted_at",
                    "timeColumnType": "timestamp",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "type": "stat"
        },
        {
            "datasource": "TeslaMate",
            "description": "",
            "fieldConfig": {
                "defaults": {
                    "decimals": 0,
                    "mappings": [],
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "#c7d0d9",
                                "value": null
                            }
                        ]
                    },
                    "unit": "none"
                },
                "overrides": [
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "consumption_km"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "Wh/km"
                            },
                            {
                                "id": "displayName",
                                "value": "net"
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "consumption_mi"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "Wh/mi"
                            },
                            {
                                "id": "displayName",
                                "value": "net"
                            }
                        ]
                    }
                ]
            },
            "gridPos": {
                "h": 4,
                "w": 3,
                "x": 13,
                "y": 7
            },
            "id": 30,
            "links": [],
            "maxDataPoints": 100,
            "options": {
                "colorMode": "value",
                "fieldOptions": {
                    "calcs": ["lastNotNull"]
                },
                "graphMode": "none",
                "justifyMode": "auto",
                "orientation": "auto",
                "reduceOptions": {
                    "calcs": ["mean"],
                    "fields": "",
                    "values": true
                },
                "text": {},
                "textMode": "auto"
            },
            "pluginVersion": "10.1.2",
            "targets": [
                {
                    "format": "table",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "SELECT\n  sum(GREATEST(start_[[preferred_range]]_range_km - end_[[preferred_range]]_range_km, 0) * car.efficiency * 1000) / \n  convert_km(sum(distance)::numeric, '$length_unit') as \"consumption_$length_unit\"\nFROM drives\nJOIN cars car ON car.id = car_id\nWHERE $__timeFilter(start_date) AND car_id = $car_id",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["latitude"],
                                "type": "column"
                            }
                        ]
                    ],
                    "table": "addresses",
                    "timeColumn": "inserted_at",
                    "timeColumnType": "timestamp",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "type": "stat"
        },
        {
            "datasource": "TeslaMate",
            "description": "",
            "fieldConfig": {
                "defaults": {
                    "decimals": 0,
                    "mappings": [],
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "#c7d0d9",
                                "value": null
                            }
                        ]
                    },
                    "unit": "none"
                },
                "overrides": [
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "consumption_km"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "Wh/km"
                            },
                            {
                                "id": "displayName",
                                "value": "gross"
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "consumption_mi"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "Wh/mi"
                            },
                            {
                                "id": "displayName",
                                "value": "gross"
                            }
                        ]
                    }
                ]
            },
            "gridPos": {
                "h": 4,
                "w": 3,
                "x": 16,
                "y": 7
            },
            "id": 32,
            "links": [],
            "maxDataPoints": 100,
            "options": {
                "colorMode": "value",
                "fieldOptions": {
                    "calcs": ["lastNotNull"]
                },
                "graphMode": "none",
                "justifyMode": "auto",
                "orientation": "auto",
                "reduceOptions": {
                    "calcs": ["mean"],
                    "fields": "",
                    "values": true
                },
                "textMode": "value_and_name"
            },
            "pluginVersion": "10.1.2",
            "targets": [
                {
                    "format": "table",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "WITH d AS (\n\tSELECT\n\t\tc.car_id,\n\t\tlag(end_[[preferred_range]]_range_km) OVER (ORDER BY start_date) - start_[[preferred_range]]_range_km AS range_loss,\n\t\tp.odometer - lag(p.odometer) OVER (ORDER BY start_date) AS distance\n\tFROM charging_processes c\n\tLEFT JOIN positions p ON p.id = c.position_id \n\tWHERE\n\t  end_date IS NOT NULL AND\n\t  c.car_id = $car_id AND\n\t  $__timeFilter(start_date)\n\tORDER BY start_date\n),\n\nrange_loss_between_charges AS (\n  SELECT sum(range_loss) AS range_loss\n  FROM d\n  WHERE distance >= 0 AND range_loss >= 0\n  GROUP BY car_id\n),\n\ncharge_dates AS (\n\tSELECT\n\t\tmin(start_date) as first_charge,\n\t\tmax(end_date) as last_charge\n\tFROM\n\t\tcharging_processes\n\tWHERE\n\t\tcar_id = $car_id\n\t\tAND $__timeFilter(start_date)\n),\n\nrange_loss_before_first_charge AS (\n\tSELECT\n\t\tmax([[preferred_range]]_battery_range_km) - min([[preferred_range]]_battery_range_km) AS range_loss\n\tFROM positions, charge_dates\n\tWHERE\n\t\tcar_id = $car_id\n\t\tAND $__timeFilter(date)\n\t\tAND ((select first_charge from charge_dates) is null OR date < (select first_charge from charge_dates))\n),\n\nrange_loss_after_last_charge AS (\n\tSELECT\n\t\tmax([[preferred_range]]_battery_range_km) - min([[preferred_range]]_battery_range_km) AS range_loss\n\tFROM positions, charge_dates\n\tWHERE\n\t\tcar_id = $car_id\n\t\tAND $__timeFilter(date)\n\t\tAND date > (select last_charge from charge_dates)\t\n),\n\ntotal_range_loss AS (\n  SELECT sum(range_loss) as range_loss\n  FROM (\n    SELECT range_loss FROM range_loss_between_charges\n    UNION ALL\n    SELECT range_loss FROM range_loss_before_first_charge\n    UNION ALL\n    SELECT range_loss FROM range_loss_after_last_charge\n  ) r\n),\n\ndistance AS (\n  SELECT max(odometer) - min(odometer) as distance\n  FROM positions\n  WHERE car_id = $car_id AND $__timeFilter(date)\n)\n\nSELECT \n  NULLIF(range_loss, 0) * (c.efficiency * 1000) / convert_km(NULLIF(distance::numeric, 0), '$length_unit') as \"consumption_$length_unit\"\nFROM total_range_loss, distance\nLEFT JOIN cars c ON c.id = $car_id",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["latitude"],
                                "type": "column"
                            }
                        ]
                    ],
                    "table": "addresses",
                    "timeColumn": "inserted_at",
                    "timeColumnType": "timestamp",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "type": "stat"
        },
        {
            "datasource": "TeslaMate",
            "description": "",
            "fieldConfig": {
                "defaults": {
                    "displayName": "Cost",
                    "mappings": [
                        {
                            "options": {
                                "match": "null",
                                "result": {
                                    "text": "N/A"
                                }
                            },
                            "type": "special"
                        }
                    ],
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "#c7d0d9",
                                "value": null
                            }
                        ]
                    },
                    "unit": "none"
                },
                "overrides": []
            },
            "gridPos": {
                "h": 2,
                "w": 5,
                "x": 19,
                "y": 9
            },
            "id": 22,
            "links": [],
            "maxDataPoints": 100,
            "options": {
                "colorMode": "value",
                "fieldOptions": {
                    "calcs": ["lastNotNull"]
                },
                "graphMode": "none",
                "justifyMode": "auto",
                "orientation": "auto",
                "reduceOptions": {
                    "calcs": ["mean"],
                    "fields": "",
                    "values": true
                },
                "textMode": "value_and_name"
            },
            "pluginVersion": "10.1.2",
            "targets": [
                {
                    "format": "table",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "select sum(cost) as \"Cost\" from charging_processes where $__timeFilter(start_date) AND car_id = $car_id;",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["latitude"],
                                "type": "column"
                            }
                        ]
                    ],
                    "table": "addresses",
                    "timeColumn": "inserted_at",
                    "timeColumnType": "timestamp",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "type": "stat"
        },
        {
            "datasource": "TeslaMate",
            "fieldConfig": {
                "defaults": {
                    "decimals": 1,
                    "displayName": "${__cell_0}",
                    "links": [],
                    "mappings": [],
                    "min": 0,
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "light-yellow",
                                "value": null
                            },
                            {
                                "color": "semi-dark-yellow",
                                "value": 10
                            },
                            {
                                "color": "semi-dark-orange",
                                "value": 100
                            }
                        ]
                    },
                    "unit": "kwatth"
                },
                "overrides": []
            },
            "gridPos": {
                "h": 3,
                "w": 11,
                "x": 13,
                "y": 11
            },
            "id": 40,
            "options": {
                "displayMode": "gradient",
                "minVizHeight": 10,
                "minVizWidth": 0,
                "orientation": "horizontal",
                "reduceOptions": {
                    "calcs": ["mean"],
                    "fields": "",
                    "values": true
                },
                "showUnfilled": false,
                "valueMode": "color"
            },
            "pluginVersion": "10.1.2",
            "targets": [
                {
                    "format": "table",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "WITH d AS (\n\tSELECT\n\t\tc.car_id,\n\t\tlag(end_[[preferred_range]]_range_km) OVER (ORDER BY start_date) - start_[[preferred_range]]_range_km AS range_loss,\n\t\tp.odometer - lag(p.odometer) OVER (ORDER BY start_date) AS distance\n\tFROM charging_processes c\n\tLEFT JOIN positions p ON p.id = c.position_id \n\tWHERE\n\t  end_date IS NOT NULL AND\n\t  c.car_id = $car_id AND\n\t  $__timeFilter(start_date)\n\tORDER BY start_date\n),\n\nrange_loss_between_charges AS (\n  SELECT sum(range_loss) AS range_loss\n  FROM d\n  WHERE distance >= 0 AND range_loss >= 0\n  GROUP BY car_id\n),\n\ncharge_dates AS (\n\tSELECT\n\t\tmin(start_date) as first_charge,\n\t\tmax(end_date) as last_charge\n\tFROM\n\t\tcharging_processes\n\tWHERE\n\t\tend_date IS NOT NULL\n\t\tAND car_id = $car_id\n\t\tAND $__timeFilter(start_date)\n),\n\nrange_loss_before_first_charge AS (\n\tSELECT\n\t\tmax([[preferred_range]]_battery_range_km) - min([[preferred_range]]_battery_range_km) AS range_loss\n\tFROM positions, charge_dates\n\tWHERE\n\t\tcar_id = $car_id\n\t\tAND $__timeFilter(date)\n\t\tAND ((select first_charge from charge_dates) is null OR date < (select first_charge from charge_dates))\n),\n\nrange_loss_after_last_charge AS (\n\tSELECT\n\t\tmax([[preferred_range]]_battery_range_km) - min([[preferred_range]]_battery_range_km) AS range_loss\n\tFROM positions, charge_dates\n\tWHERE\n\t\tcar_id = $car_id\n\t\tAND $__timeFilter(date)\n\t\tAND date > (select last_charge from charge_dates)\t\n),\n\ntotal_range_loss AS (\n\tSELECT range_loss FROM range_loss_between_charges\n\tUNION ALL\n\tSELECT range_loss FROM range_loss_before_first_charge\n\tUNION ALL\n\tSELECT range_loss FROM range_loss_after_last_charge\n)\n\nSELECT 'used' as metric, sum(range_loss * c.efficiency) AS value\nFROM total_range_loss\nLEFT JOIN cars c ON c.id = $car_id;",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["latitude"],
                                "type": "column"
                            }
                        ]
                    ],
                    "table": "addresses",
                    "timeColumn": "inserted_at",
                    "timeColumnType": "timestamp",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                },
                {
                    "format": "table",
                    "group": [],
                    "hide": false,
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "WITH charges_current AS (\n  SELECT\n\t\tcp.id,\n\t\tcp.charge_energy_added as energy_added,\n\t\tCASE WHEN NULLIF(mode() within group (order by charger_phases),0) is null THEN 'added (DC)'\n\t\t\t\t ELSE 'added (AC)'\n\t\tEND AS metric\n\tFROM charging_processes cp\n  RIGHT JOIN charges ON cp.id = charges.charging_process_id\n  WHERE\n\t  cp.car_id = $car_id\n\t  AND cp.charge_energy_added > 0\n  \tAND ($__timeFilter(start_date) OR $__timeFilter(end_date))\n  GROUP BY 1,2\n)\n\nSELECT metric, sum(energy_added) AS energy_added\nFROM charges_current\nGROUP BY 1\nORDER BY 1 DESC;",
                    "refId": "B",
                    "select": [
                        [
                            {
                                "params": ["latitude"],
                                "type": "column"
                            }
                        ]
                    ],
                    "table": "addresses",
                    "timeColumn": "inserted_at",
                    "timeColumnType": "timestamp",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "type": "bargauge"
        },
        {
            "datasource": "TeslaMate",
            "description": "",
            "fieldConfig": {
                "defaults": {
                    "color": {
                        "mode": "continuous-GrYlRd"
                    },
                    "custom": {
                        "fillOpacity": 100,
                        "hideFrom": {
                            "legend": false,
                            "tooltip": false,
                            "viz": false
                        },
                        "insertNulls": false,
                        "lineWidth": 0,
                        "spanNulls": false
                    },
                    "mappings": [
                        {
                            "options": {
                                "0": {
                                    "color": "#6ED0E0",
                                    "index": 0,
                                    "text": "online"
                                },
                                "1": {
                                    "color": "#8F3BB8",
                                    "index": 1,
                                    "text": "driving"
                                },
                                "2": {
                                    "color": "#F2CC0C",
                                    "index": 2,
                                    "text": "charging"
                                },
                                "3": {
                                    "color": "#FFB357",
                                    "index": 3,
                                    "text": "offline"
                                },
                                "4": {
                                    "color": "#56A64B",
                                    "index": 4,
                                    "text": "asleep"
                                },
                                "5": {
                                    "color": "#6ED0E0",
                                    "index": 5,
                                    "text": "online"
                                },
                                "6": {
                                    "color": "#E02F44",
                                    "index": 6,
                                    "text": "updating"
                                },
                                "null": {
                                    "index": 7,
                                    "text": "N/A"
                                }
                            },
                            "type": "value"
                        }
                    ],
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "green",
                                "value": null
                            }
                        ]
                    }
                },
                "overrides": []
            },
            "gridPos": {
                "h": 2,
                "w": 24,
                "x": 0,
                "y": 14
            },
            "id": 20,
            "options": {
                "alignValue": "center",
                "legend": {
                    "displayMode": "list",
                    "placement": "bottom",
                    "showLegend": true
                },
                "mergeValues": true,
                "rowHeight": 1,
                "showValue": "never",
                "tooltip": {
                    "mode": "single",
                    "sort": "none"
                }
            },
            "targets": [
                {
                    "format": "time_series",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "WITH states AS (\n  SELECT\n    unnest(ARRAY [start_date + interval '1 second', end_date]) AS date,\n    unnest(ARRAY [2, 0]) AS state\n  FROM charging_processes\n  WHERE\n    car_id = $car_id AND \n    ($__timeFrom() :: timestamp - interval '30 day') < start_date AND \n    (end_date < ($__timeTo() :: timestamp + interval '30 day') OR end_date IS NULL)\n  UNION\n  SELECT\n    unnest(ARRAY [start_date + interval '1 second', end_date]) AS date,\n    unnest(ARRAY [1, 0]) AS state\n  FROM drives\n  WHERE\n    car_id = $car_id AND \n    ($__timeFrom() :: timestamp - interval '30 day') < start_date AND \n    (end_date < ($__timeTo() :: timestamp + interval '30 day') OR end_date IS NULL)\n  UNION\n  SELECT\n    start_date AS date,\n    CASE\n      WHEN state = 'offline' THEN 3\n      WHEN state = 'asleep' THEN 4\n      WHEN state = 'online' THEN 5\n    END AS state\n  FROM states\n  WHERE\n    car_id = $car_id AND \n    ($__timeFrom() :: timestamp - interval '30 day') < start_date AND \n    (end_date < ($__timeTo() :: timestamp + interval '30 day') OR end_date IS NULL)\n  UNION\n  SELECT\n    unnest(ARRAY [start_date + interval '1 second', end_date]) AS date,\n    unnest(ARRAY [6, 0]) AS state\n  FROM updates\n  WHERE\n    car_id = $car_id AND \n    ($__timeFrom() :: timestamp - interval '30 day') < start_date AND \n    (end_date < ($__timeTo() :: timestamp + interval '30 day') OR end_date IS NULL)\n)\nSELECT date AS \"time\", state\nFROM states\nWHERE \n  date IS NOT NULL AND\n  ($__timeFrom() :: timestamp - interval '30 day') < date AND \n  date < ($__timeTo() :: timestamp + interval '30 day') \nORDER BY date ASC, state ASC;",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["latitude"],
                                "type": "column"
                            }
                        ]
                    ],
                    "table": "addresses",
                    "timeColumn": "inserted_at",
                    "timeColumnType": "timestamp",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "transparent": true,
            "type": "state-timeline"
        },
        {
            "datasource": "TeslaMate",
            "fieldConfig": {
                "defaults": {
                    "color": {
                        "mode": "thresholds"
                    },
                    "custom": {
                        "align": "auto",
                        "cellOptions": {
                            "type": "auto"
                        },
                        "inspect": false
                    },
                    "decimals": 2,
                    "displayName": "",
                    "mappings": [],
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "green",
                                "value": null
                            },
                            {
                                "color": "red",
                                "value": 80
                            }
                        ]
                    },
                    "unit": "short"
                },
                "overrides": [
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "start_date"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Date"
                            },
                            {
                                "id": "unit",
                                "value": "dateTimeAsLocal"
                            },
                            {
                                "id": "links",
                                "value": [
                                    {
                                        "targetBlank": false,
                                        "title": "View drive details",
                                        "url": "d/zm7wN6Zgz?from=${__data.fields.start_date_ts.numeric}&to=${__data.fields.end_date_ts.numeric}&var-car_id=${__data.fields.car_id.numeric}&var-drive_id=${__data.fields.drive_id.numeric}"
                                    }
                                ]
                            },
                            {
                                "id": "custom.width",
                                "value": 180
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "consumption_kwh_km"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Consumption"
                            },
                            {
                                "id": "unit",
                                "value": "Wh/km"
                            },
                            {
                                "id": "decimals",
                                "value": 0
                            },
                            {
                                "id": "custom.width",
                                "value": 100
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "consumption_kwh_mi"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Consumption"
                            },
                            {
                                "id": "unit",
                                "value": "Wh/mi"
                            },
                            {
                                "id": "decimals",
                                "value": 0
                            },
                            {
                                "id": "custom.width",
                                "value": 100
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "distance_km"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "km"
                            },
                            {
                                "id": "unit",
                                "value": "lengthkm"
                            },
                            {
                                "id": "decimals",
                                "value": 0
                            },
                            {
                                "id": "custom.width",
                                "value": 80
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "start_address"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Start"
                            },
                            {
                                "id": "links",
                                "value": [
                                    {
                                        "targetBlank": true,
                                        "title": "Create or edit geo-fence",
                                        "url": "[[base_url:raw]]/geo-fences/${__data.fields.start_path}"
                                    }
                                ]
                            },
                            {
                                "id": "custom.minWidth",
                                "value": 200
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "end_address"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Destination"
                            },
                            {
                                "id": "links",
                                "value": [
                                    {
                                        "targetBlank": true,
                                        "title": "Create or edit geo-fence",
                                        "url": "[[base_url:raw]]/geo-fences/${__data.fields.end_path}"
                                    }
                                ]
                            },
                            {
                                "id": "custom.minWidth",
                                "value": 200
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "duration_min"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Duration"
                            },
                            {
                                "id": "unit",
                                "value": "m"
                            },
                            {
                                "id": "decimals",
                                "value": 0
                            },
                            {
                                "id": "links",
                                "value": [
                                    {
                                        "title": "${__data.fields.duration_str}",
                                        "url": ""
                                    }
                                ]
                            },
                            {
                                "id": "custom.width",
                                "value": 100
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byRegexp",
                            "options": "/.*_ts/"
                        },
                        "properties": [
                            {
                                "id": "custom.hidden",
                                "value": true
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "distance_mi"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "mi"
                            },
                            {
                                "id": "unit",
                                "value": "lengthmi"
                            },
                            {
                                "id": "decimals",
                                "value": 0
                            },
                            {
                                "id": "custom.width",
                                "value": 80
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "% Start"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "percent"
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "decimals",
                                "value": 0
                            },
                            {
                                "id": "custom.width",
                                "value": 65
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "% End"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "percent"
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "decimals",
                                "value": 0
                            },
                            {
                                "id": "custom.width",
                                "value": 65
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byRegexp",
                            "options": "(start_path|end_path|duration_str|car_id|drive_id)"
                        },
                        "properties": [
                            {
                                "id": "custom.hidden",
                                "value": true
                            }
                        ]
                    }
                ]
            },
            "gridPos": {
                "h": 11,
                "w": 24,
                "x": 0,
                "y": 16
            },
            "id": 2,
            "links": [],
            "options": {
                "cellHeight": "sm",
                "footer": {
                    "countRows": false,
                    "fields": "",
                    "reducer": ["sum"],
                    "show": false
                },
                "showHeader": true,
                "sortBy": [
                    {
                        "desc": true,
                        "displayName": "Date"
                    }
                ]
            },
            "pluginVersion": "10.1.2",
            "targets": [
                {
                    "alias": "",
                    "datasource": "TeslaMate",
                    "format": "table",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "WITH data AS (\n  SELECT\n    round(extract(epoch FROM start_date)) * 1000 AS start_date_ts,\n    round(extract(epoch FROM end_date)) * 1000 AS end_date_ts,\n    car.id as car_id,\n    CASE WHEN start_geofence.id IS NULL THEN CONCAT('new?lat=', start_position.latitude, '&lng=', start_position.longitude)\n         WHEN start_geofence.id IS NOT NULL THEN CONCAT(start_geofence.id, '/edit')\n    END as start_path,\n    CASE WHEN end_geofence.id IS NULL THEN CONCAT('new?lat=', end_position.latitude, '&lng=', end_position.longitude)\n         WHEN end_geofence.id IS NOT NULL THEN CONCAT(end_geofence.id, '/edit')\n    END as end_path,\n    TO_CHAR((duration_min * INTERVAL '1 minute'), 'HH24:MI') as duration_str,\n    drives.id as drive_id,\n    -- Columns\n    start_date,\n    COALESCE(start_geofence.name, CONCAT_WS(', ', COALESCE(start_address.name, nullif(CONCAT_WS(' ', start_address.road, start_address.house_number), '')), start_address.city)) AS start_address,\n    COALESCE(end_geofence.name, CONCAT_WS(', ', COALESCE(end_address.name, nullif(CONCAT_WS(' ', end_address.road, end_address.house_number), '')), end_address.city)) AS end_address,\n    duration_min,\n    distance,\n    start_position.usable_battery_level as start_usable_battery_level,\n    start_position.battery_level as start_battery_level,\n    end_position.usable_battery_level as end_usable_battery_level,\n    end_position.battery_level as end_battery_level,\n    start_position.battery_level != start_position.usable_battery_level OR end_position.battery_level != end_position.usable_battery_level  as reduced_range,\n    duration_min > 1 AND distance > 1 AND ( \n      start_position.usable_battery_level IS NULL OR end_position.usable_battery_level IS NULL\tOR\n      (end_position.battery_level - end_position.usable_battery_level) = 0 \n    ) as is_sufficiently_precise,\n    NULLIF(GREATEST(start_[[preferred_range]]_range_km - end_[[preferred_range]]_range_km, 0), 0) as range_diff,\n    car.efficiency as car_efficiency,\n    outside_temp_avg,\n    distance / NULLIF(duration_min, 0) * 60 AS avg_speed\n  FROM drives\n  LEFT JOIN addresses start_address ON start_address_id = start_address.id\n  LEFT JOIN addresses end_address ON end_address_id = end_address.id\n  LEFT JOIN positions start_position ON start_position_id = start_position.id\n  LEFT JOIN positions end_position ON end_position_id = end_position.id\n  LEFT JOIN geofences start_geofence ON start_geofence_id = start_geofence.id\n  LEFT JOIN geofences end_geofence ON end_geofence_id = end_geofence.id\n  LEFT JOIN cars car ON car.id = drives.car_id\n  WHERE $__timeFilter(start_date) AND drives.car_id = $car_id\n  ORDER BY start_date DESC\n)\nSELECT\n    start_date_ts,\n    end_date_ts,\n    car_id,\n    start_path,\n    end_path,\n    duration_str,\n    drive_id,\n    -- Columns\n    start_date,\n    start_address,\n    end_address,\n    duration_min,\n    convert_km(distance::numeric, '$length_unit') AS distance_$length_unit,\n    start_battery_level as \"% Start\",\n    end_battery_level as \"% End\",\n    CASE WHEN is_sufficiently_precise THEN range_diff * car_efficiency / distance * 1000 * CASE WHEN '$length_unit' = 'km' THEN 1\n                                                                                                WHEN '$length_unit' = 'mi' THEN 1.60934\n                                                                                           END\n    END AS consumption_kWh_$length_unit\nFROM data;",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["value"],
                                "type": "column"
                            }
                        ]
                    ],
                    "timeColumn": "time",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "title": "Drives",
            "transformations": [],
            "type": "table"
        },
        {
            "datasource": "TeslaMate",
            "fieldConfig": {
                "defaults": {
                    "color": {
                        "mode": "thresholds"
                    },
                    "custom": {
                        "align": "auto",
                        "cellOptions": {
                            "type": "auto"
                        },
                        "inspect": false
                    },
                    "decimals": 2,
                    "displayName": "",
                    "mappings": [],
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "green"
                            },
                            {
                                "color": "red",
                                "value": 80
                            }
                        ]
                    },
                    "unit": "short"
                },
                "overrides": [
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "start_date"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Date"
                            },
                            {
                                "id": "unit",
                                "value": "dateTimeAsLocal"
                            },
                            {
                                "id": "links",
                                "value": [
                                    {
                                        "targetBlank": false,
                                        "title": "View charge details",
                                        "url": "d/BHhxFeZRz?from=${__data.fields.start_date_ts.numeric}&to=${__data.fields.end_date_ts.numeric}&var-car_id=${__data.fields.car_id.numeric}&var-charging_process_id=${__data.fields.id.numeric:raw}"
                                    }
                                ]
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.width",
                                "value": 180
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "charge_energy_added"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Added"
                            },
                            {
                                "id": "unit",
                                "value": "kwatth"
                            },
                            {
                                "id": "decimals",
                                "value": 2
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.width",
                                "value": 100
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "start_battery_level"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "% Start"
                            },
                            {
                                "id": "unit",
                                "value": "percent"
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "decimals",
                                "value": 0
                            },
                            {
                                "id": "custom.width",
                                "value": 65
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "end_battery_level"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "% End"
                            },
                            {
                                "id": "unit",
                                "value": "percent"
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "decimals",
                                "value": 0
                            },
                            {
                                "id": "custom.width",
                                "value": 65
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "duration_min"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Duration"
                            },
                            {
                                "id": "unit",
                                "value": "m"
                            },
                            {
                                "id": "decimals",
                                "value": 1
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.width",
                                "value": 100
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "cost"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Cost"
                            },
                            {
                                "id": "unit",
                                "value": "none"
                            },
                            {
                                "id": "decimals",
                                "value": 2
                            },
                            {
                                "id": "links",
                                "value": [
                                    {
                                        "targetBlank": false,
                                        "title": "Set Cost",
                                        "url": "[[base_url:raw]]/charge-cost/${__data.fields.id.numeric:raw}"
                                    }
                                ]
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.width",
                                "value": 80
                            }
                        ]
                    },
                    {
                        "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": "id"
                        },
                        "properties": [
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.hidden",
                                "value": true
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "address"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Location"
                            },
                            {
                                "id": "links",
                                "value": [
                                    {
                                        "targetBlank": true,
                                        "title": "Create or edit geo-fence",
                                        "url": "[[base_url:raw]]/geo-fences/${__data.fields.path}"
                                    }
                                ]
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.minWidth",
                                "value": 200
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "distance_km"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Driven"
                            },
                            {
                                "id": "unit",
                                "value": "lengthkm"
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "decimals",
                                "value": 0
                            },
                            {
                                "id": "custom.width",
                                "value": 80
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "charge_energy_added_per_hour"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "kW"
                            },
                            {
                                "id": "unit",
                                "value": "kwatt"
                            },
                            {
                                "id": "decimals",
                                "value": 1
                            },
                            {
                                "id": "custom.cellOptions",
                                "value": {
                                    "type": "color-text"
                                }
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "thresholds",
                                "value": {
                                    "mode": "absolute",
                                    "steps": [
                                        {
                                            "color": "#96D98D"
                                        },
                                        {
                                            "color": "#56A64B",
                                            "value": 20
                                        },
                                        {
                                            "color": "#37872D",
                                            "value": 55
                                        }
                                    ]
                                }
                            },
                            {
                                "id": "custom.width",
                                "value": 80
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "distance_mi"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Driven"
                            },
                            {
                                "id": "unit",
                                "value": "lengthmi"
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "decimals",
                                "value": 0
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "path"
                        },
                        "properties": [
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.hidden",
                                "value": true
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "charge_energy_used"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Used"
                            },
                            {
                                "id": "unit",
                                "value": "kwatth"
                            },
                            {
                                "id": "decimals",
                                "value": 2
                            },
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.width",
                                "value": 100
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "car_id"
                        },
                        "properties": [
                            {
                                "id": "custom.align"
                            },
                            {
                                "id": "custom.hidden",
                                "value": true
                            }
                        ]
                    }
                ]
            },
            "gridPos": {
                "h": 8,
                "w": 24,
                "x": 0,
                "y": 27
            },
            "id": 36,
            "links": [],
            "options": {
                "footer": {
                    "fields": "",
                    "reducer": ["sum"],
                    "show": false
                },
                "showHeader": true,
                "sortBy": [
                    {
                        "desc": true,
                        "displayName": "Date"
                    }
                ]
            },
            "pluginVersion": "8.5.4",
            "targets": [
                {
                    "datasource": "TeslaMate",
                    "format": "table",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "WITH data AS (\n    SELECT\n        (round(extract(epoch FROM start_date) - 10) * 1000) AS start_date_ts,\n        (round(extract(epoch FROM end_date) + 10) * 1000) AS end_date_ts,\n        start_date,\n        end_date,\n        CONCAT_WS(', ', COALESCE(addresses.name, CONCAT_WS(' ', addresses.road, addresses.house_number)), addresses.city) AS address,\n        g.name as geofence_name,\n        g.id as geofence_id,\n        p.latitude,\n        p.longitude,\n        charge_energy_added,\n        charge_energy_used,\n        duration_min,\n        start_battery_level,\n        end_battery_level,\n        start_[[preferred_range]]_range_km,\n        end_[[preferred_range]]_range_km,\n        outside_temp_avg,\n        c.id,\n        lag(end_[[preferred_range]]_range_km) OVER (ORDER BY start_date) - start_[[preferred_range]]_range_km AS range_loss,\n        p.odometer - lag(p.odometer) OVER (ORDER BY start_date) AS distance,\n        cars.efficiency,\n        c.car_id,\n        cost\n    FROM\n        charging_processes c\n    LEFT JOIN positions p ON p.id = c.position_id\n    LEFT JOIN cars ON cars.id = c.car_id\n    LEFT JOIN addresses ON addresses.id = c.address_id\n    LEFT JOIN geofences g ON g.id = geofence_id\nWHERE \n    (charge_energy_added IS NULL OR charge_energy_added > 0) AND\n    c.car_id = $car_id AND\n    $__timeFilter(start_date)\nORDER BY\n    start_date\n)\nSELECT\n    start_date_ts,\n    end_date_ts,\n    CASE WHEN geofence_id IS NULL THEN CONCAT('new?lat=', latitude, '&lng=', longitude)\n         WHEN geofence_id IS NOT NULL THEN CONCAT(geofence_id, '/edit')\n    END as path,\n    car_id,\n    id,\n    -- Columns\n    start_date,\n    COALESCE(geofence_name, address) as address,    \n    duration_min,\n    cost,\n    charge_energy_added,\n    charge_energy_used,\n    charge_energy_added * 60 / NULLIF (duration_min, 0) AS charge_energy_added_per_hour,\n    start_battery_level,\n    end_battery_level,\n    convert_km(distance::numeric, '$length_unit') AS distance_$length_unit\n FROM\n    data\nWHERE\n    (distance >= 0 OR distance IS NULL)\nORDER BY\n  start_date DESC;\n    ",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["value"],
                                "type": "column"
                            }
                        ]
                    ],
                    "timeColumn": "time",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "title": "Charges",
            "transformations": [
                {
                    "id": "merge",
                    "options": {
                        "reducers": []
                    }
                }
            ],
            "type": "table"
        },
        {
            "datasource": "TeslaMate",
            "fieldConfig": {
                "defaults": {
                    "color": {
                        "mode": "palette-classic"
                    },
                    "custom": {
                        "axisLabel": "",
                        "axisPlacement": "auto",
                        "barAlignment": 0,
                        "drawStyle": "line",
                        "fillOpacity": 10,
                        "gradientMode": "opacity",
                        "hideFrom": {
                            "legend": false,
                            "tooltip": false,
                            "viz": false
                        },
                        "lineInterpolation": "linear",
                        "lineWidth": 1,
                        "pointSize": 5,
                        "scaleDistribution": {
                            "type": "linear"
                        },
                        "showPoints": "never",
                        "spanNulls": true,
                        "stacking": {
                            "group": "A",
                            "mode": "none"
                        },
                        "thresholdsStyle": {
                            "mode": "off"
                        }
                    },
                    "decimals": 0,
                    "mappings": [],
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "green"
                            },
                            {
                                "color": "red",
                                "value": 80
                            }
                        ]
                    },
                    "unit": "percent"
                },
                "overrides": [
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "battery_level"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "percent"
                            },
                            {
                                "id": "displayName",
                                "value": "SOC"
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byRegexp",
                            "options": ".*_km$"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "lengthkm"
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byRegexp",
                            "options": ".*_mi$"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "lengthmi"
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byRegexp",
                            "options": "range_ideal_.*"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Range (ideal)"
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byRegexp",
                            "options": "range_rated_.*"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Range (rated)"
                            }
                        ]
                    }
                ]
            },
            "gridPos": {
                "h": 8,
                "w": 12,
                "x": 0,
                "y": 35
            },
            "id": 42,
            "options": {
                "legend": {
                    "calcs": ["min", "max", "lastNotNull"],
                    "displayMode": "table",
                    "placement": "bottom",
                    "showLegend": true
                },
                "tooltip": {
                    "mode": "multi",
                    "sort": "none"
                }
            },
            "pluginVersion": "8.5.4",
            "targets": [
                {
                    "datasource": "TeslaMate",
                    "format": "time_series",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "(\n  SELECT $__timeGroup(date, '5s'), avg(battery_level) as battery_level, convert_km(avg([[preferred_range]]_battery_range_km), '$length_unit') as range_[[preferred_range]]_[[length_unit]]\n  FROM positions\n  WHERE date BETWEEN ($__timeFrom()::timestamp - interval '1 day') AND ($__timeTo()::timestamp + interval '1 day') AND car_id = $car_id\n  GROUP BY 1\n) UNION ALL (\n  SELECT $__timeGroup(date, '5s'), avg(battery_level) as battery_level, convert_km(avg([[preferred_range]]_battery_range_km), '$length_unit') as range_[[preferred_range]]_[[length_unit]]\n  FROM charges c\n  LEFT JOIN charging_processes p ON c.charging_process_id = p.id\n  WHERE date BETWEEN ($__timeFrom()::timestamp - interval '1 day') AND ($__timeTo()::timestamp + interval '1 day') AND p.car_id = $car_id\n  GROUP BY 1\n)\nORDER BY 1",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["latitude"],
                                "type": "column"
                            }
                        ]
                    ],
                    "table": "addresses",
                    "timeColumn": "inserted_at",
                    "timeColumnType": "timestamp",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "title": "Battery Level & Range",
            "type": "timeseries"
        },
        {
            "datasource": "TeslaMate",
            "fieldConfig": {
                "defaults": {
                    "color": {
                        "mode": "palette-classic"
                    },
                    "custom": {
                        "axisLabel": "",
                        "axisPlacement": "auto",
                        "barAlignment": 0,
                        "drawStyle": "line",
                        "fillOpacity": 10,
                        "gradientMode": "opacity",
                        "hideFrom": {
                            "legend": false,
                            "tooltip": false,
                            "viz": false
                        },
                        "lineInterpolation": "linear",
                        "lineWidth": 1,
                        "pointSize": 5,
                        "scaleDistribution": {
                            "type": "linear"
                        },
                        "showPoints": "never",
                        "spanNulls": true,
                        "stacking": {
                            "group": "A",
                            "mode": "none"
                        },
                        "thresholdsStyle": {
                            "mode": "off"
                        }
                    },
                    "mappings": [],
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "green"
                            },
                            {
                                "color": "red",
                                "value": 80
                            }
                        ]
                    },
                    "unit": "short"
                },
                "overrides": [
                    {
                        "matcher": {
                            "id": "byRegexp",
                            "options": ".*_m$"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "lengthm"
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byRegexp",
                            "options": ".*_ft$"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "lengthft"
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byRegexp",
                            "options": "elevation_.*"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Elevation"
                            },
                            {
                                "id": "color",
                                "value": {
                                    "fixedColor": "semi-dark-blue",
                                    "mode": "fixed"
                                }
                            }
                        ]
                    }
                ]
            },
            "gridPos": {
                "h": 8,
                "w": 12,
                "x": 12,
                "y": 35
            },
            "id": 8,
            "options": {
                "legend": {
                    "calcs": ["min", "max", "lastNotNull"],
                    "displayMode": "table",
                    "placement": "bottom",
                    "showLegend": true
                },
                "tooltip": {
                    "mode": "multi",
                    "sort": "none"
                }
            },
            "pluginVersion": "8.5.4",
            "targets": [
                {
                    "datasource": "TeslaMate",
                    "format": "time_series",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "SELECT\n\t$__timeGroup(date, '5s'),\n\tROUND(convert_m(avg(elevation), '$alternative_length_unit')) AS elevation_[[alternative_length_unit]]\nFROM\n\tpositions\nWHERE\n car_id = $car_id AND\n date BETWEEN ($__timeFrom()::timestamp - interval '1 day') AND ($__timeTo()::timestamp + interval '1 day')\nGROUP BY\n 1\nORDER BY\n 1 ASC",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["latitude"],
                                "type": "column"
                            }
                        ]
                    ],
                    "table": "addresses",
                    "timeColumn": "inserted_at",
                    "timeColumnType": "timestamp",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "title": "Elevation",
            "type": "timeseries"
        }
    ],
    "refresh": "",
    "schemaVersion": 38,
    "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": {
                    "selected": false,
                    "text": "C",
                    "value": "C"
                },
                "datasource": "TeslaMate",
                "definition": "select unit_of_temperature from settings limit 1;",
                "hide": 2,
                "includeAll": false,
                "label": "temperature unit",
                "multi": false,
                "name": "temp_unit",
                "options": [],
                "query": "select unit_of_temperature from settings limit 1;",
                "refresh": 1,
                "regex": "",
                "skipUrlSync": false,
                "sort": 0,
                "tagValuesQuery": "",
                "tagsQuery": "",
                "type": "query",
                "useTags": false
            },
            {
                "current": {
                    "selected": false,
                    "text": "km",
                    "value": "km"
                },
                "datasource": "TeslaMate",
                "definition": "select unit_of_length from settings limit 1;",
                "hide": 2,
                "includeAll": false,
                "label": "length unit",
                "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": "m",
                    "value": "m"
                },
                "datasource": "TeslaMate",
                "definition": "select case when unit_of_length = 'km' then 'm' when unit_of_length = 'mi' then 'ft' end  from settings limit 1;",
                "hide": 2,
                "includeAll": false,
                "label": "",
                "multi": false,
                "name": "alternative_length_unit",
                "options": [],
                "query": "select case when unit_of_length = 'km' then 'm' when unit_of_length = 'mi' then 'ft' end  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,
                "label": "",
                "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
            },
            {
                "current": {
                    "selected": false,
                    "text": "1642593521525",
                    "value": "1642593521525"
                },
                "datasource": "TeslaMate",
                "definition": "with last_drives as (select start_date from drives order by start_date desc limit 3)\nselect extract(epoch from min(start_date)) * 1000 from last_drives;",
                "hide": 2,
                "includeAll": false,
                "multi": false,
                "name": "from",
                "options": [],
                "query": "with last_drives as (select start_date from drives order by start_date desc limit 3)\nselect extract(epoch from min(start_date)) * 1000 from last_drives;",
                "refresh": 1,
                "regex": "",
                "skipUrlSync": false,
                "sort": 0,
                "tagValuesQuery": "",
                "tagsQuery": "",
                "type": "query",
                "useTags": false
            }
        ]
    },
    "time": {
        "from": "now/d",
        "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": "Trip",
    "uid": "FkUpJpQZk",
    "version": 2,
    "weekStart": ""
}
