Template Pulling Incorrect Date & Time

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 %}

Declare the entities your template uses. So HA knows what to monitor for changes. e.g.

  #Dark Sky Alert Text
  - platform: template
    sensors:
      dark_sky_alert_text:
        entity_id: sensor.dark_sky_alerts
        friendly_name_template: >-
etc...

At the very least you have typos:

            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")}}

For your consideration, a streamlined version:

  #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
          {% else %}
            {% set x = 'expires' ~ ('' if is_state('sensor.dark_sky_alerts','1') else '_0') %}
            Expires at {{ state_attr('sensor.dark_sky_alerts', x) | int | timestamp_custom('%-I:%M %p on %A')}}
          {% endif%}
        value_template: >-
          {% set x = 'title' ~ ('' if is_state('sensor.dark_sky_alerts','1') else '_0' %}
          {{ state_attr('sensor.dark_sky_alerts', x) }}

Thanks. I added that in.

Oh geez. Thanks

May not be able to test until we get more severe weather, but I am sure that should do it.

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.