{
  "$schema": "./.schemas/page.schema.json",
  "title": "Tesla",
  "icon": "car.side",
  "sources": {
    "car": {
      "url": "https://api.tessie.com/YOUR_VIN/state?use_cache=true",
      "secret": "tessie",
      "every": "5m",
      "derive": [
        {
          "name": "awake",
          "value": {
            "if": {
              "path": "drive_state.shift_state",
              "in": [
                "D",
                "R",
                "N"
              ]
            },
            "then": {
              "template": "Driving"
            },
            "else": {
              "path": "state",
              "values": {
                "asleep": "Asleep",
                "online": "Awake",
                "offline": "Offline",
                "waking": "Waking up"
              },
              "default": "Not reporting"
            }
          }
        },
        {
          "name": "open_text",
          "value": {
            "if": {
              "path": "vehicle_state.locked",
              "equals": false
            },
            "then": {
              "template": "Unlocked"
            },
            "else": {
              "if": {
                "any": [
                  {
                    "path": "vehicle_state.df",
                    "gte": 1
                  },
                  {
                    "path": "vehicle_state.dr",
                    "gte": 1
                  },
                  {
                    "path": "vehicle_state.pf",
                    "gte": 1
                  },
                  {
                    "path": "vehicle_state.pr",
                    "gte": 1
                  }
                ]
              },
              "then": {
                "template": "A door is open"
              },
              "else": {
                "if": {
                  "path": "vehicle_state.ft",
                  "gte": 1
                },
                "then": {
                  "template": "Frunk open"
                },
                "else": {
                  "if": {
                    "path": "vehicle_state.rt",
                    "gte": 1
                  },
                  "then": {
                    "template": "Trunk open"
                  },
                  "else": {
                    "if": {
                      "any": [
                        {
                          "path": "vehicle_state.fd_window",
                          "gte": 1
                        },
                        {
                          "path": "vehicle_state.fp_window",
                          "gte": 1
                        },
                        {
                          "path": "vehicle_state.rd_window",
                          "gte": 1
                        },
                        {
                          "path": "vehicle_state.rp_window",
                          "gte": 1
                        }
                      ]
                    },
                    "then": {
                      "template": "A window is open"
                    },
                    "else": {
                      "if": {
                        "any": [
                          {
                            "path": "vehicle_state.tpms_hard_warning_fl",
                            "equals": true
                          },
                          {
                            "path": "vehicle_state.tpms_hard_warning_fr",
                            "equals": true
                          },
                          {
                            "path": "vehicle_state.tpms_hard_warning_rl",
                            "equals": true
                          },
                          {
                            "path": "vehicle_state.tpms_hard_warning_rr",
                            "equals": true
                          }
                        ]
                      },
                      "then": {
                        "template": "A tire is very low"
                      },
                      "else": {
                        "if": {
                          "any": [
                            {
                              "path": "vehicle_state.tpms_soft_warning_fl",
                              "equals": true
                            },
                            {
                              "path": "vehicle_state.tpms_soft_warning_fr",
                              "equals": true
                            },
                            {
                              "path": "vehicle_state.tpms_soft_warning_rl",
                              "equals": true
                            },
                            {
                              "path": "vehicle_state.tpms_soft_warning_rr",
                              "equals": true
                            }
                          ]
                        },
                        "then": {
                          "template": "A tire is low"
                        },
                        "else": {
                          "if": {
                            "path": "charge_state.battery_level",
                            "lt": 20
                          },
                          "then": {
                            "template": "Battery at {charge_state.battery_level}%"
                          },
                          "else": {
                            "template": "Locked"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        },
        {
          "name": "car_state",
          "value": {
            "if": {
              "any": [
                {
                  "path": "vehicle_state.tpms_hard_warning_fl",
                  "equals": true
                },
                {
                  "path": "vehicle_state.tpms_hard_warning_fr",
                  "equals": true
                },
                {
                  "path": "vehicle_state.tpms_hard_warning_rl",
                  "equals": true
                },
                {
                  "path": "vehicle_state.tpms_hard_warning_rr",
                  "equals": true
                }
              ]
            },
            "then": {
              "template": "critical"
            },
            "else": {
              "if": {
                "any": [
                  {
                    "path": "vehicle_state.locked",
                    "equals": false
                  },
                  {
                    "any": [
                      {
                        "path": "vehicle_state.df",
                        "gte": 1
                      },
                      {
                        "path": "vehicle_state.dr",
                        "gte": 1
                      },
                      {
                        "path": "vehicle_state.pf",
                        "gte": 1
                      },
                      {
                        "path": "vehicle_state.pr",
                        "gte": 1
                      }
                    ]
                  },
                  {
                    "any": [
                      {
                        "path": "vehicle_state.ft",
                        "gte": 1
                      },
                      {
                        "path": "vehicle_state.rt",
                        "gte": 1
                      }
                    ]
                  },
                  {
                    "any": [
                      {
                        "path": "vehicle_state.fd_window",
                        "gte": 1
                      },
                      {
                        "path": "vehicle_state.fp_window",
                        "gte": 1
                      },
                      {
                        "path": "vehicle_state.rd_window",
                        "gte": 1
                      },
                      {
                        "path": "vehicle_state.rp_window",
                        "gte": 1
                      }
                    ]
                  },
                  {
                    "any": [
                      {
                        "path": "vehicle_state.tpms_soft_warning_fl",
                        "equals": true
                      },
                      {
                        "path": "vehicle_state.tpms_soft_warning_fr",
                        "equals": true
                      },
                      {
                        "path": "vehicle_state.tpms_soft_warning_rl",
                        "equals": true
                      },
                      {
                        "path": "vehicle_state.tpms_soft_warning_rr",
                        "equals": true
                      }
                    ]
                  },
                  {
                    "path": "charge_state.battery_level",
                    "lt": 20
                  }
                ]
              },
              "then": {
                "template": "warning"
              },
              "else": {
                "template": "ok"
              }
            }
          }
        },
        {
          "name": "tire_text",
          "value": {
            "if": {
              "any": [
                {
                  "path": "vehicle_state.tpms_hard_warning_fl",
                  "equals": true
                },
                {
                  "path": "vehicle_state.tpms_hard_warning_fr",
                  "equals": true
                },
                {
                  "path": "vehicle_state.tpms_hard_warning_rl",
                  "equals": true
                },
                {
                  "path": "vehicle_state.tpms_hard_warning_rr",
                  "equals": true
                }
              ]
            },
            "then": {
              "template": "A tire is very low"
            },
            "else": {
              "if": {
                "any": [
                  {
                    "path": "vehicle_state.tpms_soft_warning_fl",
                    "equals": true
                  },
                  {
                    "path": "vehicle_state.tpms_soft_warning_fr",
                    "equals": true
                  },
                  {
                    "path": "vehicle_state.tpms_soft_warning_rl",
                    "equals": true
                  },
                  {
                    "path": "vehicle_state.tpms_soft_warning_rr",
                    "equals": true
                  }
                ]
              },
              "then": {
                "template": "A tire is low"
              },
              "else": {
                "template": "All four normal"
              }
            }
          }
        },
        {
          "name": "fl_psi",
          "value": {
            "path": "vehicle_state.tpms_pressure_fl",
            "scale": 14.5038,
            "round": 0
          }
        },
        {
          "name": "fr_psi",
          "value": {
            "path": "vehicle_state.tpms_pressure_fr",
            "scale": 14.5038,
            "round": 0
          }
        },
        {
          "name": "rl_psi",
          "value": {
            "path": "vehicle_state.tpms_pressure_rl",
            "scale": 14.5038,
            "round": 0
          }
        },
        {
          "name": "rr_psi",
          "value": {
            "path": "vehicle_state.tpms_pressure_rr",
            "scale": 14.5038,
            "round": 0
          }
        },
        {
          "name": "inside_f",
          "value": {
            "path": "climate_state.inside_temp",
            "scale": 1.8,
            "offset": 32,
            "round": 0
          }
        },
        {
          "name": "outside_f",
          "value": {
            "path": "climate_state.outside_temp",
            "scale": 1.8,
            "offset": 32,
            "round": 0
          }
        },
        {
          "name": "set_f",
          "value": {
            "path": "climate_state.driver_temp_setting",
            "scale": 1.8,
            "offset": 32,
            "round": 0
          }
        },
        {
          "name": "charge_left",
          "value": {
            "multiply": [
              "charge_state.minutes_to_full_charge",
              60
            ]
          }
        },
        {
          "name": "schedule_end",
          "value": {
            "add": [
              "charge_state.scheduled_charging_start_time",
              {
                "multiply": [
                  {
                    "subtract": [
                      "charge_schedule_data.charge_schedules[0].end_time",
                      "charge_schedule_data.charge_schedules[0].start_time"
                    ]
                  },
                  60
                ]
              }
            ]
          }
        },
        {
          "name": "update_pending",
          "value": {
            "if": {
              "path": "vehicle_state.software_update.status",
              "in": [
                "available",
                "scheduled",
                "downloading",
                "downloading_wifi_wait",
                "installing"
              ]
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        }
      ]
    },
    "car-day": {
      "url": "https://api.tessie.com",
      "secret": "tessie",
      "every": "15m",
      "requests": {
        "day": "/YOUR_VIN/states?from={date.hours_ago_24_unix}&to={date.now_unix}&interval=900",
        "now": "/YOUR_VIN/state?use_cache=true"
      }
    },
    "car-health": {
      "url": "https://api.tessie.com",
      "secret": "tessie",
      "every": "1h",
      "requests": {
        "alerts": "/YOUR_VIN/firmware_alerts"
      }
    }
  },
  "cards": [
    {
      "id": "status",
      "type": "heading",
      "text": "Status",
      "collapsed": true,
      "cards": [
        "car",
        "to-charging",
        "to-trips"
      ]
    },
    {
      "id": "car",
      "type": "status",
      "label": "Model Y",
      "state": "unknown",
      "width": "full",
      "url": "https://my.tessie.com",
      "source": {
        "ref": "car",
        "map": {
          "state": "car_state",
          "detail": {
            "template": "{open_text} · {awake} · last report {vehicle_state.timestamp|ago}"
          }
        }
      }
    },
    {
      "id": "to-charging",
      "type": "link",
      "layout": "tile",
      "label": "Charging",
      "icon": "bolt.fill",
      "detail": "Schedule, limit, sessions",
      "url": "legato://page/tesla-charging"
    },
    {
      "id": "to-trips",
      "type": "link",
      "layout": "tile",
      "label": "Trips",
      "icon": "map",
      "detail": "Miles, efficiency, FSD",
      "url": "legato://page/tesla-trips"
    },
    {
      "id": "controls",
      "type": "text",
      "label": "Controls",
      "actions_placement": "corner",
      "source": {
        "ref": "car"
      },
      "actions": [
        {
          "label": "Charge limit",
          "icon": "battery.75percent",
          "method": "POST",
          "path": "/YOUR_VIN/command/set_charge_limit?percent={choice}",
          "success": {
            "path": "result",
            "equals": true
          },
          "current": "charge_state.charge_limit_soc",
          "choices": [
            {
              "label": "70%",
              "value": 70
            },
            {
              "label": "80%",
              "value": 80
            },
            {
              "label": "90%",
              "value": 90
            },
            {
              "label": "100%",
              "value": 100
            }
          ]
        },
        {
          "label": "Current",
          "icon": "bolt",
          "method": "POST",
          "path": "/YOUR_VIN/command/set_charging_amps?amps={choice}",
          "success": {
            "path": "result",
            "equals": true
          },
          "current": "charge_state.charge_current_request",
          "choices": [
            {
              "label": "16 A",
              "value": 16
            },
            {
              "label": "24 A",
              "value": 24
            },
            {
              "label": "32 A",
              "value": 32
            },
            {
              "label": "40 A",
              "value": 40
            },
            {
              "label": "48 A",
              "value": 48
            }
          ]
        },
        {
          "label": "Start charging",
          "icon": "bolt.fill",
          "method": "POST",
          "path": "/YOUR_VIN/command/start_charging",
          "success": {
            "path": "result",
            "equals": true
          },
          "when": {
            "path": "charge_state.charging_state",
            "equals": "Stopped"
          }
        },
        {
          "label": "Stop charging",
          "icon": "bolt.slash.fill",
          "method": "POST",
          "path": "/YOUR_VIN/command/stop_charging",
          "success": {
            "path": "result",
            "equals": true
          },
          "when": {
            "path": "charge_state.charging_state",
            "equals": "Charging"
          },
          "prominent": true
        },
        "|",
        {
          "label": "Turn on heat protection",
          "icon": "thermometer.sun",
          "method": "POST",
          "path": "/YOUR_VIN/command/set_cabin_overheat_protection?on=true&fan_only=false",
          "success": {
            "path": "result",
            "equals": true
          },
          "when": {
            "path": "climate_state.cabin_overheat_protection",
            "equals": "Off"
          }
        },
        {
          "label": "Turn off heat protection",
          "icon": "thermometer.sun.fill",
          "method": "POST",
          "path": "/YOUR_VIN/command/set_cabin_overheat_protection?on=false",
          "success": {
            "path": "result",
            "equals": true
          },
          "when": {
            "path": "climate_state.cabin_overheat_protection",
            "in": [
              "On",
              "FanOnly"
            ]
          }
        },
        {
          "label": "Turn on Sentry",
          "icon": "eye",
          "method": "POST",
          "path": "/YOUR_VIN/command/enable_sentry",
          "success": {
            "path": "result",
            "equals": true
          },
          "when": {
            "path": "vehicle_state.sentry_mode",
            "equals": false
          }
        },
        {
          "label": "Turn off Sentry",
          "icon": "eye.slash",
          "method": "POST",
          "path": "/YOUR_VIN/command/disable_sentry",
          "success": {
            "path": "result",
            "equals": true
          },
          "when": {
            "path": "vehicle_state.sentry_mode",
            "equals": true
          }
        },
        {
          "label": "Lock",
          "icon": "lock.fill",
          "method": "POST",
          "path": "/YOUR_VIN/command/lock",
          "success": {
            "path": "result",
            "equals": true
          },
          "when": {
            "path": "vehicle_state.locked",
            "equals": false
          },
          "prominent": true
        }
      ]
    },
    {
      "id": "climate-controls",
      "type": "text",
      "label": "Climate",
      "actions_placement": "corner",
      "source": {
        "ref": "car"
      },
      "actions": [
        {
          "label": "Start climate",
          "icon": "fan",
          "method": "POST",
          "path": "/YOUR_VIN/command/start_climate",
          "success": {
            "path": "result",
            "equals": true
          },
          "when": {
            "path": "climate_state.is_climate_on",
            "equals": false
          }
        },
        {
          "label": "Stop climate",
          "icon": "fan.slash",
          "method": "POST",
          "path": "/YOUR_VIN/command/stop_climate",
          "success": {
            "path": "result",
            "equals": true
          },
          "when": {
            "path": "climate_state.is_climate_on",
            "equals": true
          },
          "prominent": true
        },
        {
          "label": "Start defrost",
          "icon": "windshield.front.and.heat.waves",
          "method": "POST",
          "path": "/YOUR_VIN/command/start_max_defrost",
          "success": {
            "path": "result",
            "equals": true
          },
          "when": {
            "path": "climate_state.is_front_defroster_on",
            "equals": false
          }
        },
        {
          "label": "Stop defrost",
          "icon": "windshield.front.and.wiper",
          "method": "POST",
          "path": "/YOUR_VIN/command/stop_max_defrost",
          "success": {
            "path": "result",
            "equals": true
          },
          "when": {
            "path": "climate_state.is_front_defroster_on",
            "equals": true
          }
        },
        {
          "label": "Start wheel heat",
          "icon": "steeringwheel",
          "method": "POST",
          "path": "/YOUR_VIN/command/start_steering_wheel_heater",
          "success": {
            "path": "result",
            "equals": true
          },
          "when": {
            "path": "climate_state.steering_wheel_heater",
            "equals": false
          }
        },
        {
          "label": "Stop wheel heat",
          "icon": "steeringwheel.slash",
          "method": "POST",
          "path": "/YOUR_VIN/command/stop_steering_wheel_heater",
          "success": {
            "path": "result",
            "equals": true
          },
          "when": {
            "path": "climate_state.steering_wheel_heater",
            "equals": true
          }
        },
        {
          "label": "Temperature",
          "icon": "thermometer.medium",
          "method": "POST",
          "path": "/YOUR_VIN/command/set_temperatures?temperature={choice}",
          "success": {
            "path": "result",
            "equals": true
          },
          "current": "climate_state.driver_temp_setting",
          "choices": [
            {
              "label": "64°F",
              "value": 18
            },
            {
              "label": "66°F",
              "value": 19
            },
            {
              "label": "68°F",
              "value": 20
            },
            {
              "label": "70°F",
              "value": 21
            },
            {
              "label": "72°F",
              "value": 22
            },
            {
              "label": "74°F",
              "value": 23.5
            },
            {
              "label": "76°F",
              "value": 24.5
            }
          ]
        },
        "|",
        {
          "label": "Start driver heat",
          "icon": "carseat.left.fill",
          "method": "POST",
          "path": "/YOUR_VIN/command/set_seat_heat?seat=front_left&level=3",
          "success": {
            "path": "result",
            "equals": true
          },
          "when": {
            "path": "climate_state.seat_heater_left",
            "equals": 0
          }
        },
        {
          "label": "Stop driver heat",
          "icon": "carseat.left",
          "method": "POST",
          "path": "/YOUR_VIN/command/set_seat_heat?seat=front_left&level=0",
          "success": {
            "path": "result",
            "equals": true
          },
          "when": {
            "path": "climate_state.seat_heater_left",
            "gte": 1
          }
        },
        {
          "label": "Start passenger heat",
          "icon": "carseat.right.fill",
          "method": "POST",
          "path": "/YOUR_VIN/command/set_seat_heat?seat=front_right&level=3",
          "success": {
            "path": "result",
            "equals": true
          },
          "when": {
            "path": "climate_state.seat_heater_right",
            "equals": 0
          }
        },
        {
          "label": "Stop passenger heat",
          "icon": "carseat.right",
          "method": "POST",
          "path": "/YOUR_VIN/command/set_seat_heat?seat=front_right&level=0",
          "success": {
            "path": "result",
            "equals": true
          },
          "when": {
            "path": "climate_state.seat_heater_right",
            "gte": 1
          }
        }
      ]
    },
    {
      "id": "battery-day",
      "type": "sparkline",
      "unit": "%",
      "points": [
        0,
        0
      ],
      "target_label": "Limit",
      "right_unit": "mi",
      "change": false,
      "source": {
        "ref": "car-day",
        "map": {
          "points": {
            "each": "day.results.battery_levels",
            "value": {
              "path": "$"
            }
          },
          "times": {
            "each": "day.results.timestamps",
            "value": {
              "path": "$"
            }
          },
          "labels": {
            "each": "day.results.timestamps",
            "template": "{$|weekday} {$|clock}"
          },
          "axis_labels": {
            "each": "day.results.timestamps",
            "template": "{$|clock_hour}"
          },
          "target": "now.charge_state.charge_limit_soc",
          "right_scale": {
            "divide": [
              "now.charge_state.battery_range",
              "now.charge_state.battery_level"
            ]
          }
        }
      }
    },
    {
      "id": "car-facts",
      "type": "facts",
      "label": "Car",
      "items": [
        {
          "key": "Charging",
          "value": "—"
        },
        {
          "key": "Tires",
          "value": "—"
        },
        {
          "key": "Temps",
          "value": "—"
        },
        {
          "key": "Software",
          "value": "—"
        },
        {
          "key": "Odometer",
          "value": "—"
        }
      ],
      "source": {
        "ref": "car",
        "map": {
          "items[0].value": {
            "if": {
              "path": "charge_state.charging_state",
              "equals": "Charging"
            },
            "then": {
              "template": "{charge_state.charge_limit_soc}% in {charge_left|duration} · {charge_state.charger_power} kW"
            },
            "else": {
              "if": {
                "path": "charge_state.charging_state",
                "equals": "Complete"
              },
              "then": {
                "template": "Done at {charge_state.battery_level}%"
              },
              "else": {
                "if": {
                  "all": [
                    {
                      "path": "charge_state.charging_state",
                      "equals": "Stopped"
                    },
                    {
                      "path": "charge_state.scheduled_charging_pending",
                      "equals": true
                    }
                  ]
                },
                "then": {
                  "template": "Plugged in · starts {charge_state.scheduled_charging_start_time|clock}"
                },
                "else": {
                  "if": {
                    "path": "charge_state.charging_state",
                    "equals": "Stopped"
                  },
                  "then": {
                    "template": "Plugged in, stopped"
                  },
                  "else": {
                    "if": {
                      "all": [
                        {
                          "path": "charge_state.charging_state",
                          "equals": "Disconnected"
                        },
                        {
                          "path": "charge_state.scheduled_charging_pending",
                          "equals": true
                        }
                      ]
                    },
                    "then": {
                      "template": "Unplugged · schedule {charge_state.scheduled_charging_start_time|clock}"
                    },
                    "else": {
                      "if": {
                        "path": "charge_state.charging_state",
                        "equals": "Disconnected"
                      },
                      "then": {
                        "template": "Unplugged"
                      },
                      "else": {
                        "if": {
                          "path": "charge_state.charging_state",
                          "equals": "NoPower"
                        },
                        "then": {
                          "template": "Charger has no power"
                        },
                        "else": {
                          "if": {
                            "path": "charge_state.charging_state",
                            "equals": "Starting"
                          },
                          "then": {
                            "template": "Starting"
                          },
                          "else": {
                            "template": "{charge_state.charging_state}"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "items[1].value": {
            "template": "{fl_psi} · {fr_psi} · {rl_psi} · {rr_psi} psi"
          },
          "items[2].value": {
            "template": "{inside_f}° in · {outside_f}° out"
          },
          "items[3].value": "vehicle_state.car_version",
          "items[4].value": {
            "template": "{vehicle_state.odometer|round:0} mi"
          }
        }
      }
    },
    {
      "id": "update",
      "type": "text",
      "label": "Software update",
      "icon": "arrow.down.circle",
      "body": "…",
      "source": {
        "ref": "car",
        "map": {
          "body": {
            "if": {
              "path": "vehicle_state.software_update.status",
              "equals": "scheduled"
            },
            "then": {
              "template": "{vehicle_state.software_update.version} installs at {vehicle_state.software_update.scheduled_time_ms|clock}"
            },
            "else": {
              "if": {
                "path": "vehicle_state.software_update.status",
                "equals": "installing"
              },
              "then": {
                "template": "{vehicle_state.software_update.version} is installing"
              },
              "else": {
                "template": "{vehicle_state.software_update.version} is ready to install"
              }
            }
          }
        },
        "when": {
          "path": "update_pending",
          "equals": "yes"
        }
      }
    },
    {
      "id": "alerts",
      "type": "list",
      "label": "Recent alerts",
      "max_items": 4,
      "items": [
        "—"
      ],
      "source": {
        "ref": "car-health",
        "map": {
          "items": {
            "each": "alerts.results",
            "limit": 20,
            "where": {
              "path": "audiences",
              "contains": "customer"
            },
            "fields": {
              "text": {
                "template": "{customer_facing_messages[0]}"
              },
              "detail": {
                "template": "{timestamp|ago}"
              }
            }
          }
        }
      }
    }
  ]
}
