Hello,
My home assistant is connected to an external community sensor, but the sensor is sometimes offline (unavailable). I want to create my template sensor, who save last state of external sensor and show the last state if the external sensor is unavailable. If the external sensor works I want to show value from it.
I build my first template in my configuration.yaml:
template:
- sensor:
- unique_id: sensor_pm10_LastState
name: >
{% if "unavailable" != states("sensor.sensor_pm10") | string %}
{{ "Wrocław PM 10" }}
{% else %}
{{ "Wrocław PM 10 (offline)" }}
{% endif %}
unit_of_measurement: "μg/m³"
icon: "mdi:molecule"
state: >
{% set pm10 = states("sensor.sensor_pm10") | string %}
{% if pm10 != 'unavailable' %}
{% set pm10_last_state = pm10 | float %}
{{ pm10 | float }}
{% else %}
{{ pm10_last_state | float }}
{% endif %}
What am I doing wrong? What can I do better?
Please help.
Regards
Rafal