Why is this trigger based binary template 'unknown' after reloading

using:

template:

  - trigger:
      - platform: time_pattern
        hours: 0
        minutes: 0
      - platform: homeassistant
        event: start
      - platform: event
        event_type: event_template_reloaded

    binary_sensor:

      - unique_id: zomertijd_binary
        name: Zomertijd
        picture: /local/season/summer.png
        state: >
          {{now().timetuple().tm_isdst == 1}}

      - unique_id: wintertijd_binary
        name: Wintertijd
        picture: /local/season/winter.png
        state: >
          {{now().timetuple().tm_isdst == 0}}

      - unique_id: christmas_time
        name: Christmas time
        state: >
          {% set today = now().day %}
          {% set month = now().month %}
          {{month == 12 and today >= 24 or month == 1 and today <= 2}}
        icon: >
          mdi:{{iif(is_state('binary_sensor.christmas_time','on'),'string-lights','pine-tree')}}

    sensor:

      - unique_id: utc_offset
        name: Utc offset
        state: >
          {{now().utcoffset().total_seconds()/3600}}
        icon: >
          {% set offset = now().utcoffset().total_seconds()/3600 %}
          mdi:numeric-{{offset|int|abs}}

# many more sensors here

only the binary_templates show up as unknown, all other sensors, and sensors using another set of triggers before this are correct.

I moved all of these from being perfectly functional state based template sensors to the trigger based section, and have made sure there are no _2 suffixes, or .template_ variants because of that move :wink:

and as far as I can see compliant with the examples just above Template - Home Assistant

must be going blind. What am I missing here?
please have a look? thanks!

Could be that the sensor hasn’t triggered yet and therefore doesn’t have a value.
Have it past midninght or have you done a restart of HA or the template?

thanks, but nope, reloading the template entities should update it but doesnt, and even a manual update of the entity doesnt help…

note the entities are created, and are not ‘unavailable’

You know im far far from fully understanding templates here :slight_smile: , but i was just thinking, try with varchar instead of int

hmm. I guess I had the wrong homeassistant_start event.

Changing from

- platform: event
  event_type: homeassistant_start

as it is listed in the dev events tab

to

      - platform: homeassistant
        event: start

at least makes them available with a state on startup…

however, the Christmas binary turns unknown on reloading template entities (the other 2 keep a correct state)
Mystery.

FYI:

The following PR will change the behavior of a Trigger-based Template Sensor on startup:

Hi Taras,

Yes I am eagerly awaiting those PR’s, much is going to improve with these restores.

Still, that wont solve the issue I am experiencing currently I fear, because startup behaves correctly now on these binaries (not yet on a nested template btw: nested trigger based templated is unavailable · Issue #70036 · home-assistant/core · GitHub)

I truly dont understand why the Christmas time template wont reload correctly on the event_template_reloaded, where all others do.

btw, This is going to be sweet too!

am I suffering Enhance Trigger-based Template Sensors? then why is this binary the only template suffering that…

and, even when the state is actually correct, the icon template remains fixed on the pine-tree. See what happens after I flipped the icons to use, reloaded template entities, and a manual update entity:

        icon: >
          {% set today = now().day %} {% set month = now().month %}
          {% if month == 12 and today >= 24 or month == 1 and today <= 2 %} mdi:pine-tree
          {% else %} mdi:string-lights
          {% endif %}

which should result in the string-lights:

a restart results in:

note this was merely for testing purposes, to check the icon template itself.

@Mariusthvdb
Sorry for resurrecting this old thread.
Did you get new insights on this? I also have issues with the template reload event and binary sensors.
What I figured out is that the template value is simply restored by the template reloaded event, in stead of processing the template state logic. So always the state before reloading.
This is super annoying if you try to debug a template, as changes made to the sensor state logic simply don’t have any effect when reloading the templates. Only other triggers ( e.g. the restart) will process the new state logic,

I think this is perfect default behavior, unless I explicitly add the template reloaded event as a trigger.

tbh, not sure what you are saying…