Skip to content

Commit

Permalink
docs: fix Home Assistant MQTT sensor JSON templates warnings (#4257)
Browse files Browse the repository at this point in the history
* Fix Home Assistant active route MQTT sensor JSON templates

More gracefully handles JSONs with error (not actively routing)

* Fix tesla_active_route_distance_to_arrival_km sensor

* Fix template sensors

Conditionally convert values if the sensor actually has a value, to prevent converting invalid non-numeric values

* Fix copy/paste mistakes
  • Loading branch information
longzheng authored and JakobLichterfeld committed Oct 19, 2024
1 parent 84b1c4b commit f6b354f
Showing 1 changed file with 70 additions and 17 deletions.
87 changes: 70 additions & 17 deletions website/docs/integrations/home_assistant.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ Don't forget to replace `<teslamate url>` and `<your tesla model>` with correct
value_template: "{{ 'offline' if value_json.error else 'online' }}"
device: *teslamate_device_info
json_attributes_topic: "teslamate/cars/1/active_route"
json_attributes_template: "{{ value_json.location | tojson }}"
json_attributes_template: >
{% if not value_json.error and value_json.location %}
{{ value_json.location | tojson }}
{% else %}
{}
{% endif %}
icon: mdi:crosshairs-gps
- sensor:
Expand Down Expand Up @@ -446,7 +451,10 @@ Don't forget to replace `<teslamate url>` and `<your tesla model>` with correct
availability: *teslamate_active_route_availability
device: *teslamate_device_info
state_topic: "teslamate/cars/1/active_route"
value_template: "{{ value_json.destination }}"
value_template: >
{% if not value_json.error and value_json.destination %}
{{ value_json.destination }}
{% endif %}
icon: mdi:map-marker
- sensor:
Expand All @@ -456,7 +464,10 @@ Don't forget to replace `<teslamate url>` and `<your tesla model>` with correct
availability: *teslamate_active_route_availability
device: *teslamate_device_info
state_topic: "teslamate/cars/1/active_route"
value_template: "{{ value_json.energy_at_arrival }}"
value_template: >
{% if not value_json.error and value_json.energy_at_arrival %}
{{ value_json.energy_at_arrival }}
{% endif %}
unit_of_measurement: "%"
icon: mdi:battery-80
Expand All @@ -467,7 +478,10 @@ Don't forget to replace `<teslamate url>` and `<your tesla model>` with correct
availability: *teslamate_active_route_availability
device: *teslamate_device_info
state_topic: "teslamate/cars/1/active_route"
value_template: "{{ value_json.miles_to_arrival }}"
value_template: >
{% if not value_json.error and value_json.miles_to_arrival %}
{{ value_json.miles_to_arrival }}
{% endif %}
unit_of_measurement: mi
icon: mdi:map-marker-distance
Expand All @@ -478,7 +492,10 @@ Don't forget to replace `<teslamate url>` and `<your tesla model>` with correct
availability: *teslamate_active_route_availability
device: *teslamate_device_info
state_topic: "teslamate/cars/1/active_route"
value_template: "{{ value_json.minutes_to_arrival }}"
value_template: >
{% if not value_json.error and value_json.minutes_to_arrival %}
{{ value_json.minutes_to_arrival }}
{% endif %}
unit_of_measurement: min
icon: mdi:clock-outline
Expand All @@ -489,7 +506,10 @@ Don't forget to replace `<teslamate url>` and `<your tesla model>` with correct
availability: *teslamate_active_route_availability
device: *teslamate_device_info
state_topic: "teslamate/cars/1/active_route"
value_template: "{{ value_json.traffic_minutes_delay }}"
value_template: >
{% if not value_json.error and value_json.traffic_minutes_delay %}
{{ value_json.traffic_minutes_delay }}
{% endif %}
unit_of_measurement: min
icon: mdi:clock-alert-outline
Expand Down Expand Up @@ -653,78 +673,111 @@ Don't forget to replace `<teslamate url>` and `<your tesla model>` with correct
friendly_name: Estimated Range (mi)
unit_of_measurement: mi
icon_template: mdi:gauge
availability_template: "{{ has_value('sensor.tesla_est_battery_range_km') }}"
value_template: >
{{ (states('sensor.tesla_est_battery_range_km') | float / 1.609344) | round(2) }}
{% if has_value('sensor.tesla_est_battery_range_km') %}
{{ (states('sensor.tesla_est_battery_range_km') | float / 1.609344) | round(2) }}
{% endif %}
tesla_rated_battery_range_mi:
friendly_name: Rated Range (mi)
unit_of_measurement: mi
icon_template: mdi:gauge
availability_template: "{{ has_value('sensor.tesla_rated_battery_range_km') }}"
value_template: >
{{ (states('sensor.tesla_rated_battery_range_km') | float / 1.609344) | round(2) }}
{% if has_value('sensor.tesla_rated_battery_range_km') %}
{{ (states('sensor.tesla_rated_battery_range_km') | float / 1.609344) | round(2) }}
{% endif %}
tesla_ideal_battery_range_mi:
friendly_name: Ideal Range (mi)
unit_of_measurement: mi
icon_template: mdi:gauge
availability_template: "{{ has_value('sensor.tesla_ideal_battery_range_km') }}"
value_template: >
{{ (states('sensor.tesla_ideal_battery_range_km') | float / 1.609344) | round(2) }}
{% if has_value('sensor.tesla_ideal_battery_range_km') %}
{{ (states('sensor.tesla_ideal_battery_range_km') | float / 1.609344) | round(2) }}
{% endif %}
tesla_odometer_mi:
friendly_name: Odometer (mi)
unit_of_measurement: mi
icon_template: mdi:counter
availability_template: "{{ has_value('sensor.tesla_odometer') }}"
value_template: >
{{ (states('sensor.tesla_odometer') | float / 1.609344) | round(2) }}
{% if has_value('sensor.tesla_odometer') %}
{{ (states('sensor.tesla_odometer') | float / 1.609344) | round(2) }}
{% endif %}
tesla_speed_mph:
friendly_name: Speed (MPH)
unit_of_measurement: mph
icon_template: mdi:speedometer
availability_template: "{{ has_value('sensor.tesla_speed') }}"
value_template: >
{{ (states('sensor.tesla_speed') | float / 1.609344) | round(2) }}
{% if has_value('sensor.tesla_speed') %}
{{ (states('sensor.tesla_speed') | float / 1.609344) | round(2) }}
{% endif %}
tesla_elevation_ft:
friendly_name: Elevation (ft)
unit_of_measurement: ft
icon_template: mdi:image-filter-hdr
availability_template: "{{ has_value('sensor.tesla_elevation') }}"
value_template: >
{{ (states('sensor.tesla_elevation') | float * 3.2808 ) | round(2) }}
{% if has_value('sensor.tesla_elevation') %}
{{ (states('sensor.tesla_elevation') | float * 3.2808) | round(2) }}
{% endif %}
tesla_tpms_pressure_fl_psi:
friendly_name: Front Left Tire Pressure (psi)
unit_of_measurement: psi
icon_template: mdi:car-tire-alert
availability_template: "{{ has_value('sensor.tesla_tpms_fl') }}"
value_template: >
{{ (states('sensor.tesla_tpms_pressure_fl') | float * 14.50377) | round(2) }}
{% if has_value('sensor.tesla_tpms_fl') %}
{{ (states('sensor.tesla_tpms_fl') | float * 14.50377) | round(2) }}
{% endif %}
tesla_tpms_pressure_fr_psi:
friendly_name: Front Right Tire Pressure (psi)
unit_of_measurement: psi
icon_template: mdi:car-tire-alert
availability_template: "{{ has_value('sensor.tesla_tpms_fr') }}"
value_template: >
{{ (states('sensor.tesla_tpms_pressure_fr') | float * 14.50377) | round(2) }}
{% if has_value('sensor.tesla_tpms_fr') %}
{{ (states('sensor.tesla_tpms_fr') | float * 14.50377) | round(2) }}
{% endif %}
tesla_tpms_pressure_rl_psi:
friendly_name: Rear Left Tire Pressure (psi)
unit_of_measurement: psi
icon_template: mdi:car-tire-alert
availability_template: "{{ has_value('sensor.tesla_tpms_rl') }}"
value_template: >
{{ (states('sensor.tesla_tpms_pressure_rl') | float * 14.50377) | round(2) }}
{% if has_value('sensor.tesla_tpms_rl') %}
{{ (states('sensor.tesla_tpms_rl') | float * 14.50377) | round(2) }}
{% endif %}
tesla_tpms_pressure_rr_psi:
friendly_name: Rear Right Tire Pressure (psi)
unit_of_measurement: psi
icon_template: mdi:car-tire-alert
availability_template: "{{ has_value('sensor.tesla_tpms_rr') }}"
value_template: >
{{ (states('sensor.tesla_tpms_pressure_rr') | float * 14.50377) | round(2) }}
{% if has_value('sensor.tesla_tpms_rr') %}
{{ (states('sensor.tesla_tpms_rr') | float * 14.50377) | round(2) }}
{% endif %}
tesla_active_route_distance_to_arrival_km:
friendly_name: Active route distance to arrival (km)
unit_of_measurement: km
icon_template: mdi:map-marker-distance
availability_template: "{{ has_value('sensor.tesla_active_route_distance_to_arrival_mi') }}"
value_template: >
{{ (states('sensor.tesla_active_route_distance_to_arrival_mi') | float * 1.609344) | round(2) }}
{% if has_value('sensor.tesla_active_route_distance_to_arrival_mi') %}
{{ (states('sensor.tesla_active_route_distance_to_arrival_mi') | float * 1.609344) | round(2) }}
{% endif %}
```

### binary_sensor.yaml (binary_sensor: section of configuration.yaml)
Expand Down

0 comments on commit f6b354f

Please sign in to comment.