{
    "__elements": {},
    "__requires": [
        {
            "type": "panel",
            "id": "bargauge",
            "name": "Bar gauge",
            "version": ""
        },
        {
            "type": "grafana",
            "id": "grafana",
            "name": "Grafana",
            "version": "10.1.2"
        },
        {
            "type": "datasource",
            "id": "postgres",
            "name": "PostgreSQL",
            "version": "1.0.0"
        },
        {
            "type": "panel",
            "id": "stat",
            "name": "Stat",
            "version": ""
        },
        {
            "type": "panel",
            "id": "table",
            "name": "Table",
            "version": ""
        }
    ],
    "annotations": {
        "list": [
            {
                "builtIn": 1,
                "datasource": {
                    "type": "datasource",
                    "uid": "grafana"
                },
                "enable": true,
                "hide": true,
                "definition": "TeslaMate|U2FsdGVkX1/cEWK+8cz7pjEKXtzJnDN7b21ZDXt1MGneFGPWTLqOPtxKmu02mJPLzi/f29I+NBHd3vi0FB8R4Xn0+GtobWDgk6VAVSBTdSNniOKO8i2WPlhRaOsl2+hG7gnZ7wrf1Th2nxR7f1uYCrbwOek0IzkfLzrkjh7gkr6inT6bbDuJqrmogZajLxmAMrQ6V+/vHxBRGiwjJhgiEeq3hM1q2h04JKkNiZ8RHbsF5Cd/xd8Q9u0JVrZzIrtnhM/SFlaApU7RtRMu8CSj1llTX7WEOj6VDZAMSf+XUAanWdk725kEPN9MNu89o2zEq5P3E3cju8IbbBdPzXLV3oVuzD6/tMnxFToIIV1E/BrpF7s2RtNa8+KJJ1PF8xgs6m+/KTD2hy+WsP0636AgObRAmYg7+qotGrgNvpNPdE0EgrB7WHYlV7R/1q66bcq6tCe51X1Un70k+zo+K6AK0o4B1H6IyMlEVuRH/Fz8QVl9aYu2ztd08RbuKJlYVKpkH+pxVETAO9MclYQ90tzE6TfwDZrQZzsAlMenr4s1ZB1OlFXjLjVjnddnUilzO76cqv4yI2THQEuyQ47nuVQ4gUbx02K59vMQhns3C01JOAYokOaSXe66Y7QYdMlk09Lf|aes-256-cbc",
                "iconColor": "rgba(0, 211, 255, 1)",
                "name": "Annotations & Alerts",
                "target": {
                    "limit": 100,
                    "matchAny": false,
                    "tags": [],
                    "type": "dashboard"
                },
                "type": "dashboard"
            }
        ]
    },
    "editable": true,
    "fiscalYearStartMonth": 0,
    "graphTooltip": 0,
    "id": null,
    "links": [
        {
            "icon": "dashboard",
            "tags": [],
            "title": "TeslaMate",
            "tooltip": "",
            "type": "link",
            "url": "[[base_url:raw]]"
        },
        {
            "asDropdown": true,
            "icon": "external link",
            "tags": ["tesla"],
            "title": "Dashboards",
            "type": "dashboards"
        },
        {
            "asDropdown": true,
            "icon": "external link",
            "includeVars": false,
            "keepTime": false,
            "tags": ["TeslamateCustomDashboards"],
            "targetBlank": false,
            "title": "Custom Dashboards",
            "tooltip": "",
            "type": "dashboards",
            "url": ""
        }
    ],
    "liveNow": false,
    "panels": [
        {
            "datasource": {
                "type": "postgres",
                "uid": "TeslaMate"
            },
            "fieldConfig": {
                "defaults": {
                    "custom": {
                        "align": "auto",
                        "cellOptions": {
                            "type": "auto"
                        },
                        "filterable": false,
                        "inspect": false
                    },
                    "mappings": [],
                    "min": 0,
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "green",
                                "value": null
                            }
                        ]
                    }
                },
                "overrides": [
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "avg_consumption_km"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Avg. Consumption"
                            },
                            {
                                "id": "unit",
                                "value": "Wh/km"
                            },
                            {
                                "id": "custom.width",
                                "value": 200
                            },
                            {
                                "id": "decimals",
                                "value": 0
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "avg_consumption_mi"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Avg. Consumption"
                            },
                            {
                                "id": "unit",
                                "value": "Wh/mi"
                            },
                            {
                                "id": "custom.width",
                                "value": 200
                            },
                            {
                                "id": "decimals",
                                "value": 0
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "avg_distance_km"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Avg. Distance"
                            },
                            {
                                "id": "unit",
                                "value": "km"
                            },
                            {
                                "id": "custom.cellOptions",
                                "value": {
                                    "mode": "gradient",
                                    "type": "gauge"
                                }
                            },
                            {
                                "id": "color",
                                "value": {
                                    "mode": "continuous-GrYlRd"
                                }
                            },
                            {
                                "id": "min"
                            },
                            {
                                "id": "max"
                            },
                            {
                                "id": "decimals",
                                "value": 1
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "avg_distance_mi"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Avg. Distance"
                            },
                            {
                                "id": "unit",
                                "value": "mi"
                            },
                            {
                                "id": "custom.cellOptions",
                                "value": {
                                    "mode": "gradient",
                                    "type": "gauge"
                                }
                            },
                            {
                                "id": "max"
                            },
                            {
                                "id": "min"
                            },
                            {
                                "id": "color",
                                "value": {
                                    "mode": "continuous-GrYlRd"
                                }
                            },
                            {
                                "id": "decimals",
                                "value": 1
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "speed_range_km"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Segment (km/h)"
                            },
                            {
                                "id": "custom.width",
                                "value": 125
                            },
                            {
                                "id": "custom.align",
                                "value": "center"
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "speed_range_mi"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Segment (mph)"
                            },
                            {
                                "id": "custom.width",
                                "value": 125
                            },
                            {
                                "id": "custom.align",
                                "value": "center"
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "avg_speed_km"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Avg Speed"
                            },
                            {
                                "id": "unit",
                                "value": "velocitykmh"
                            },
                            {
                                "id": "custom.width",
                                "value": 120
                            },
                            {
                                "id": "decimals",
                                "value": 0
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "avg_speed_mi"
                        },
                        "properties": [
                            {
                                "id": "displayName",
                                "value": "Avg. Speed"
                            },
                            {
                                "id": "unit",
                                "value": "velocitymph"
                            },
                            {
                                "id": "custom.width",
                                "value": 120
                            },
                            {
                                "id": "decimals",
                                "value": 0
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "avg_power"
                        },
                        "properties": [
                            {
                                "id": "custom.width",
                                "value": 200
                            },
                            {
                                "id": "displayName",
                                "value": "Avg. Power"
                            },
                            {
                                "id": "unit",
                                "value": "kwatt"
                            },
                            {
                                "id": "decimals",
                                "value": 0
                            }
                        ]
                    }
                ]
            },
            "gridPos": {
                "h": 13,
                "w": 14,
                "x": 0,
                "y": 0
            },
            "id": 2,
            "links": [],
            "options": {
                "cellHeight": "sm",
                "footer": {
                    "countRows": false,
                    "enablePagination": false,
                    "fields": "",
                    "reducer": ["sum"],
                    "show": false
                },
                "showHeader": true,
                "sortBy": []
            },
            "pluginVersion": "10.1.2",
            "targets": [
                {
                    "datasource": {
                        "type": "postgres",
                        "uid": "TeslaMate"
                    },
                    "format": "table",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "SELECT\r\n    BinnedData.speed_bin AS speed_range_$length_unit,\r\n    convert_km(BinnedData.total_odometer_distance::numeric, '$length_unit') AS avg_distance_$length_unit,\r\n    BinnedData.avg_speed * CASE WHEN '$length_unit' = 'km' THEN 1 WHEN '$length_unit' = 'mi' THEN 0.621371 END AS avg_speed_$length_unit,\r\n    ((BinnedData.total_energy_consumed / (BinnedData.total_distance * CASE WHEN '$length_unit' = 'km' THEN 1 WHEN '$length_unit' = 'mi' THEN 0.621371 END)) * 1000 ) / (BinnedData.avg_speed * CASE WHEN '$length_unit' = 'km' THEN 1 WHEN '$length_unit' = 'mi' THEN 0.621371 END) AS avg_consumption_$length_unit,\r\n    avg_power\r\nFROM\r\n    (SELECT\r\n        CASE\r\n            WHEN elevation_diff >= 1 THEN 'Uphill'\r\n            WHEN elevation_diff <= -1 THEN 'Downhill'\r\n            ELSE 'Flat'\r\n        END AS terrain_type,\r\n        ROUND(speed * CASE WHEN '$length_unit' = 'km' THEN 1 WHEN '$length_unit' = 'mi' THEN 0.621371 END / 10, 0) * 10 AS speed_bin,\r\n        AVG(power) AS avg_power,\r\n        SUM(power::integer * speed::integer / 60.0) AS total_energy_consumed,\r\n        SUM(speed / 60.0) AS total_distance,\r\n        SUM(odometer_distance) AS total_odometer_distance,\r\n        AVG(speed) AS avg_speed,\r\n        car_id\r\n    FROM\r\n        (SELECT\r\n            drives.car_id,\r\n            positions.speed,\r\n            positions.power,\r\n            positions.elevation - LAG(positions.elevation) OVER (PARTITION BY positions.drive_id ORDER BY positions.date) AS elevation_diff,\r\n            positions.odometer - LAG(positions.odometer) OVER (PARTITION BY positions.drive_id ORDER BY positions.date) AS odometer_distance\r\n        FROM\r\n            drives\r\n        JOIN positions ON drives.id = positions.drive_id\r\n        WHERE\r\n            $__timeFilter(drives.start_date)\r\n            AND drives.car_id = $car_id\r\n            AND drives.end_date IS NOT NULL\r\n            AND drives.distance >= convert_km($min_distance::numeric, '$length_unit')\r\n        ) AS DifferentialData\r\n    GROUP BY speed_bin, terrain_type, car_id\r\n    ) AS BinnedData\r\nJOIN cars ON BinnedData.car_id = cars.id\r\nWHERE BinnedData.terrain_type = '$terrain_type'\r\n    AND BinnedData.total_distance > 0 \r\n    AND BinnedData.total_odometer_distance > 0\r\n    AND BinnedData.speed_bin >= $min_speed_segment\r\nORDER BY BinnedData.speed_bin DESC;\r\n",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["latitude"],
                                "type": "column"
                            }
                        ]
                    ],
                    "table": "addresses",
                    "timeColumn": "inserted_at",
                    "timeColumnType": "timestamp",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "title": "Consumption by speed - $terrain_type Terrain",
            "type": "table"
        },
        {
            "datasource": {
                "type": "postgres",
                "uid": "TeslaMate"
            },
            "fieldConfig": {
                "defaults": {
                    "decimals": 0,
                    "mappings": [],
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "#c7d0d9",
                                "value": null
                            }
                        ]
                    },
                    "unit": "none"
                },
                "overrides": [
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "distance_km"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "km"
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "distance_mi"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "mi"
                            }
                        ]
                    }
                ]
            },
            "gridPos": {
                "h": 6,
                "w": 10,
                "x": 14,
                "y": 0
            },
            "id": 6,
            "links": [],
            "maxDataPoints": 100,
            "options": {
                "colorMode": "value",
                "fieldOptions": {
                    "calcs": ["mean"]
                },
                "graphMode": "none",
                "justifyMode": "auto",
                "orientation": "horizontal",
                "reduceOptions": {
                    "calcs": ["mean"],
                    "fields": "",
                    "values": false
                },
                "textMode": "value"
            },
            "pluginVersion": "10.1.2",
            "targets": [
                {
                    "datasource": {
                        "type": "postgres",
                        "uid": "TeslaMate"
                    },
                    "format": "table",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "select convert_km(sum(distance)::numeric, '$length_unit') as \"distance_$length_unit\" \nfrom drives \nwhere $__timeFilter(end_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"
                        }
                    ]
                }
            ],
            "title": "Logged Distance",
            "type": "stat"
        },
        {
            "datasource": {
                "type": "postgres",
                "uid": "TeslaMate"
            },
            "fieldConfig": {
                "defaults": {
                    "decimals": 0,
                    "mappings": [],
                    "noValue": "N/A",
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "#c7d0d9",
                                "value": null
                            }
                        ]
                    },
                    "unit": "none"
                },
                "overrides": [
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "consumption_km"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "Wh/km"
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "consumption_mi"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "Wh/mi"
                            }
                        ]
                    }
                ]
            },
            "gridPos": {
                "h": 7,
                "w": 3,
                "x": 14,
                "y": 6
            },
            "id": 4,
            "links": [],
            "maxDataPoints": 100,
            "options": {
                "colorMode": "value",
                "fieldOptions": {
                    "calcs": ["mean"]
                },
                "graphMode": "none",
                "justifyMode": "auto",
                "orientation": "horizontal",
                "reduceOptions": {
                    "calcs": ["mean"],
                    "fields": "",
                    "values": false
                },
                "textMode": "value"
            },
            "pluginVersion": "10.1.2",
            "targets": [
                {
                    "datasource": {
                        "type": "postgres",
                        "uid": "TeslaMate"
                    },
                    "format": "table",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "select \n  sum((start_[[preferred_range]]_range_km - end_[[preferred_range]]_range_km) * cars.efficiency) / sum(distance) * 1000 * \n    CASE WHEN '$length_unit' = 'km' THEN 1\n         WHEN '$length_unit' = 'mi' THEN 1.60934\n    END AS \"consumption_$length_unit\"\nfrom drives \ninner join cars on cars.id = car_id\nwhere $__timeFilter(end_date) \n  and distance is not null and\n  start_[[preferred_range]]_range_km - end_[[preferred_range]]_range_km >= 0.1 and\n  car_id = $car_id",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["latitude"],
                                "type": "column"
                            }
                        ]
                    ],
                    "table": "addresses",
                    "timeColumn": "inserted_at",
                    "timeColumnType": "timestamp",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "title": "Consumption (net)",
            "type": "stat"
        },
        {
            "datasource": {
                "type": "postgres",
                "uid": "TeslaMate"
            },
            "fieldConfig": {
                "defaults": {
                    "decimals": 0,
                    "mappings": [],
                    "noValue": "N/A",
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "#c7d0d9",
                                "value": null
                            }
                        ]
                    },
                    "unit": "none"
                },
                "overrides": [
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "consumption_km"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "Wh/km"
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "consumption_mi"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "Wh/mi"
                            }
                        ]
                    }
                ]
            },
            "gridPos": {
                "h": 7,
                "w": 3,
                "x": 17,
                "y": 6
            },
            "id": 8,
            "links": [],
            "maxDataPoints": 100,
            "options": {
                "colorMode": "value",
                "fieldOptions": {
                    "calcs": ["mean"]
                },
                "graphMode": "none",
                "justifyMode": "auto",
                "orientation": "horizontal",
                "reduceOptions": {
                    "calcs": ["mean"],
                    "fields": "",
                    "values": false
                },
                "textMode": "value"
            },
            "pluginVersion": "10.1.2",
            "targets": [
                {
                    "datasource": {
                        "type": "postgres",
                        "uid": "TeslaMate"
                    },
                    "format": "table",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "WITH d1 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\n\t\tcharging_processes c\n\tLEFT JOIN positions p ON p.id = c.position_id \n\tWHERE\n\t  $__timeFilter(end_date)\n\t  and end_date IS NOT NULL AND\n\t  c.car_id = $car_id\n\tORDER BY\n\t\tstart_date\n),\nd2 AS (\nSELECT\n\tcar_id,\n\tsum(range_loss) AS range_loss,\n\tsum(distance) AS distance\nFROM\n\td1\nWHERE\n\tdistance > 0 AND range_loss >= 0\nGROUP BY\n\tcar_id\n)\nSELECT\nrange_loss * c.efficiency / distance * 1000 *\n  CASE WHEN '$length_unit' = 'km' THEN 1\n       WHEN '$length_unit' = 'mi' THEN 1.60934\n  END AS \"consumption_$length_unit\"\nFROM\n\td2\n\tLEFT 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"
                        }
                    ]
                }
            ],
            "title": "Consumption (gross) ",
            "type": "stat"
        },
        {
            "datasource": {
                "type": "postgres",
                "uid": "TeslaMate"
            },
            "fieldConfig": {
                "defaults": {
                    "color": {
                        "mode": "thresholds"
                    },
                    "decimals": 0,
                    "mappings": [],
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "light-green",
                                "value": null
                            }
                        ]
                    },
                    "unit": "short"
                },
                "overrides": [
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "efficiency_km"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "Wh/km"
                            }
                        ]
                    },
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "efficiency_mi"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "Wh/mi"
                            }
                        ]
                    }
                ]
            },
            "gridPos": {
                "h": 7,
                "w": 4,
                "x": 20,
                "y": 6
            },
            "id": 14,
            "links": [],
            "maxDataPoints": 100,
            "options": {
                "colorMode": "value",
                "graphMode": "area",
                "justifyMode": "auto",
                "orientation": "horizontal",
                "reduceOptions": {
                    "calcs": ["lastNotNull"],
                    "fields": "",
                    "values": false
                },
                "textMode": "auto"
            },
            "pluginVersion": "10.1.2",
            "targets": [
                {
                    "datasource": {
                        "type": "postgres",
                        "uid": "TeslaMate"
                    },
                    "format": "table",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "SELECT\n\tCASE WHEN '$length_unit' = 'km' THEN efficiency\n\t     WHEN '$length_unit' = 'mi' THEN efficiency * 1.60934\n\tEND * 1000 as \"efficiency_$length_unit\"\nFROM\n\tcars\nWHERE\n\tid = $car_id;",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["charge_energy_added"],
                                "type": "column"
                            }
                        ]
                    ],
                    "table": "charges",
                    "timeColumn": "date",
                    "timeColumnType": "timestamp",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "title": "Current $preferred_range efficiency",
            "type": "stat"
        },
        {
            "datasource": {
                "type": "postgres",
                "uid": "TeslaMate"
            },
            "fieldConfig": {
                "defaults": {
                    "color": {
                        "mode": "continuous-GrYlRd"
                    },
                    "mappings": [],
                    "noValue": "0",
                    "thresholds": {
                        "mode": "absolute",
                        "steps": [
                            {
                                "color": "super-light-green",
                                "value": null
                            }
                        ]
                    }
                },
                "overrides": [
                    {
                        "matcher": {
                            "id": "byName",
                            "options": "seconds_elapsed"
                        },
                        "properties": [
                            {
                                "id": "unit",
                                "value": "s"
                            },
                            {
                                "id": "decimals",
                                "value": 1
                            }
                        ]
                    }
                ]
            },
            "gridPos": {
                "h": 11,
                "w": 24,
                "x": 0,
                "y": 13
            },
            "id": 16,
            "links": [],
            "options": {
                "displayMode": "gradient",
                "minVizHeight": 10,
                "minVizWidth": 0,
                "orientation": "horizontal",
                "reduceOptions": {
                    "calcs": ["lastNotNull"],
                    "fields": "",
                    "values": true
                },
                "showUnfilled": false,
                "valueMode": "color"
            },
            "pluginVersion": "10.1.2",
            "targets": [
                {
                    "datasource": {
                        "type": "postgres",
                        "uid": "TeslaMate"
                    },
                    "editorMode": "code",
                    "format": "table",
                    "group": [],
                    "metricColumn": "none",
                    "rawQuery": true,
                    "rawSql": "SELECT \r\n    speed_section_$length_unit || CASE WHEN '$length_unit' = 'km' THEN ' km/h' WHEN '$length_unit' = 'mi' THEN ' mph' END AS speed,\r\n    SUM(seconds_elapsed) AS seconds_elapsed\r\nFROM (\r\n    SELECT\r\n        ROUND(convert_km(p.speed::numeric, '$length_unit') / 10,0) * 10 AS speed_section_$length_unit,\r\n        EXTRACT(EPOCH FROM (LEAD(p.\"date\") OVER (ORDER BY p.\"date\") - p.\"date\")) AS seconds_elapsed\r\n    FROM drives d\r\n    INNER JOIN positions p ON p.drive_id = d.id\r\n    WHERE\r\n        $__timeFilter(d.start_date)\r\n        AND d.car_id = $car_id \r\n        AND d.end_date IS NOT NULL\r\n        AND d.distance >= convert_km($min_distance::numeric,'$length_unit')\r\n        AND p.speed > 0.1\r\n) AS drivedata\r\nWHERE speed_section_$length_unit >= $min_speed_segment\r\nGROUP BY speed_section_$length_unit\r\nORDER BY speed_section_$length_unit DESC",
                    "refId": "A",
                    "select": [
                        [
                            {
                                "params": ["value"],
                                "type": "column"
                            }
                        ]
                    ],
                    "sql": {
                        "columns": [
                            {
                                "parameters": [],
                                "type": "function"
                            }
                        ],
                        "groupBy": [
                            {
                                "property": {
                                    "type": "string"
                                },
                                "type": "groupBy"
                            }
                        ],
                        "limit": 50
                    },
                    "timeColumn": "time",
                    "where": [
                        {
                            "name": "$__timeFilter",
                            "params": [],
                            "type": "macro"
                        }
                    ]
                }
            ],
            "title": "Top Speeds",
            "type": "bargauge"
        }
    ],
    "refresh": "",
    "schemaVersion": 38,
    "style": "dark",
    "tags": ["TeslamateCustomDashboards"],
    "templating": {
        "list": [
            {
                "current": {},
                "datasource": {
                    "type": "postgres",
                    "uid": "TeslaMate"
                },
                "definition": "SELECT name AS __text, id AS __value FROM cars ORDER BY display_priority ASC, name ASC;",
                "hide": 0,
                "includeAll": false,
                "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": {},
                "datasource": {
                    "type": "postgres",
                    "uid": "TeslaMate"
                },
                "definition": "select unit_of_length from settings limit 1;",
                "hide": 2,
                "includeAll": false,
                "label": "",
                "multi": false,
                "name": "length_unit",
                "options": [],
                "query": "select unit_of_length from settings limit 1;",
                "refresh": 1,
                "regex": "",
                "skipUrlSync": false,
                "sort": 0,
                "tagValuesQuery": "",
                "tagsQuery": "",
                "type": "query",
                "useTags": false
            },
            {
                "current": {},
                "datasource": {
                    "type": "postgres",
                    "uid": "TeslaMate"
                },
                "definition": "select preferred_range from settings limit 1;",
                "hide": 2,
                "includeAll": false,
                "multi": false,
                "name": "preferred_range",
                "options": [],
                "query": "select preferred_range from settings limit 1;",
                "refresh": 1,
                "regex": "",
                "skipUrlSync": false,
                "sort": 0,
                "tagValuesQuery": "",
                "tagsQuery": "",
                "type": "query",
                "useTags": false
            },
            {
                "current": {
                    "selected": true,
                    "text": "50",
                    "value": "50"
                },
                "hide": 0,
                "includeAll": false,
                "label": "Min. speed segment",
                "multi": false,
                "name": "min_speed_segment",
                "options": [
                    {
                        "selected": false,
                        "text": "10",
                        "value": "10"
                    },
                    {
                        "selected": false,
                        "text": "20",
                        "value": "20"
                    },
                    {
                        "selected": false,
                        "text": "30",
                        "value": "30"
                    },
                    {
                        "selected": false,
                        "text": "40",
                        "value": "40"
                    },
                    {
                        "selected": true,
                        "text": "50",
                        "value": "50"
                    }
                ],
                "query": "10,20,30,40,50",
                "queryValue": "",
                "skipUrlSync": false,
                "type": "custom"
            },
            {
                "current": {
                    "selected": true,
                    "text": "25",
                    "value": "25"
                },
                "hide": 0,
                "includeAll": false,
                "label": "Min. driving distance",
                "multi": false,
                "name": "min_distance",
                "options": [
                    {
                        "selected": false,
                        "text": "1",
                        "value": "1"
                    },
                    {
                        "selected": false,
                        "text": "5",
                        "value": "5"
                    },
                    {
                        "selected": false,
                        "text": "10",
                        "value": "10"
                    },
                    {
                        "selected": true,
                        "text": "25",
                        "value": "25"
                    },
                    {
                        "selected": false,
                        "text": "50",
                        "value": "50"
                    },
                    {
                        "selected": false,
                        "text": "100",
                        "value": "100"
                    },
                    {
                        "selected": false,
                        "text": "150",
                        "value": "150"
                    },
                    {
                        "selected": false,
                        "text": "200",
                        "value": "200"
                    }
                ],
                "query": "1,5,10,25,50,100,150,200",
                "queryValue": "",
                "skipUrlSync": false,
                "type": "custom"
            },
            {
                "current": {},
                "datasource": {
                    "type": "postgres",
                    "uid": "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": true,
                    "text": "Flat",
                    "value": "Flat"
                },
                "hide": 0,
                "includeAll": false,
                "label": "Terrain Type",
                "multi": false,
                "name": "terrain_type",
                "options": [
                    {
                        "selected": true,
                        "text": "Flat",
                        "value": "Flat"
                    },
                    {
                        "selected": false,
                        "text": "Uphill",
                        "value": "Uphill"
                    },
                    {
                        "selected": false,
                        "text": "Downhill",
                        "value": "Downhill"
                    }
                ],
                "query": "Flat, Uphill, Downhill",
                "queryValue": "",
                "skipUrlSync": false,
                "type": "custom"
            }
        ]
    },
    "time": {
        "from": "now-30d",
        "to": "now"
    },
    "timepicker": {
        "hidden": false,
        "time_options": [
            "5m",
            "15m",
            "1h",
            "6h",
            "12h",
            "24h",
            "2d",
            "7d",
            "30d"
        ]
    },
    "timezone": "",
    "title": "Speed Rates",
    "uid": "jchmfu4SiQgWz",
    "version": 10,
    "weekStart": ""
}
