I have a template sensor set up to convert the dark_sky_alerts sensor state into text. The template extracts the “title” attribute from the from the sensor. This is working perfectly. I am trying to a template as the “friendly name” for the template sensor. The goal is to extract the “expires” time & date from the alerts sensor. The is in Unix time format. For some reason the template is not pulling the correct time or date.
There is a current alert that expires at 1568422800 Unix time (this comes from the “expires” attribute) which is Sep 14 1:00 AM GMT. The template is returning Sep 12 11:00 PM GMT. The interesting thing is, when I first set up this template, there was an alert that expired at that time. It is almost as if that time got cached somehow and the template is not updating. This is happening in both the “states” page and the template editor.
Code is below. Thoughts?
#Dark Sky Alert Text
- platform: template
sensors:
dark_sky_alert_text:
friendly_name_template: >-
{% if is_state('sensor.dark_sky_alerts','0') %}
No Weather Alerts
{% elif is_state('sensor.dark_sky_alerts','1') %}
Expires at {{ states.sensor.dark_sky_alert_attributes_expires | int | timestamp_custom("%-I:%M %p on %A")}}
{% else %}
Expires at {{ states.sensor.dark_sky_alert_attributes_expires_0 | int | timestamp_custom("%-I:%M %p on %A")}}
{% endif%}
value_template: >-
{% if is_state('sensor.dark_sky_alerts','1') %}
{{ states.sensor.dark_sky_alerts.attributes.title }}
{% else %}
{{ states.sensor.dark_sky_alerts.attributes.title_0 }}
{% endif %}
Expires at {{ states.sensor.dark_sky_alert_attributes_expires | int | timestamp_custom("%-I:%M %p on %A")}}
{% else %}
Expires at {{ states.sensor.dark_sky_alert_attributes_expires_0 | int | timestamp_custom("%-I:%M %p on %A")}}
should be:
Expires at {{ states.sensor.dark_sky_alerts.attributes.expires | int | timestamp_custom("%-I:%M %p on %A")}}
{% else %}
Expires at {{ states.sensor.dark_sky_alerts.attributes.expires_0 | int | timestamp_custom("%-I:%M %p on %A")}}
Actually that shouldn’t be necessary. It will find sensor.dark_sky_alerts automatically.
Why? Just go to the States page and manually change the state of sensor.dark_sky_alerts. It will only be temporary since whatever you write will eventually get overwritten the next time the sensor updates itself.
In this case sure, but it’s a good habit to get into. HA tries to detect the entities used in the templates. It can fail. Including the entities just in case is wise.