Event_template_reloaded not updating sensor

Since the recent updates I’ve been having the issue for some of my template sensor that they stay unknown for a long time since the triggers are rare. The preferred solution apparently is to listen for home assistant restarts and template reloads, but for some reason this doesn’t work for me.
Actually, I’ve had it work for one sensor, but that is about it and it wouldn’t always work. I’m mystified as to why. See below for my simplest example.

- trigger:
  - platform: state
    entity_id: person.me
    id: "person"

  - platform: state
    entity_id: person.gf
    id: "person"

  # template entities reloaded or hass restarted
  - platform: event
    event_type: event_template_reloaded
    id: "reload"

  - platform: homeassistant
    event: start

  binary_sensor:
  - name: "Anyone Home"
    state: >
      {{ is_state('person.me', 'home') or is_state('person.gf', 'home') }}
    device_class: presence

Surely I can’t be the only one? I just ran into another example of the same problem. Reloading this template also puts the binary_sensor into unknown where I would expect off.

- trigger:
  - platform: numeric_state
    entity_id: sensor.badkamer_humidity_change_per_minute
    above: 0.5
    id: "rising"
  - platform: state
    entity_id: binary_sensor.auxiliary_dhw_state
    id: "hot_water_long_on"
    to: "on"
    for: "00:03:00"
  - platform: state
    entity_id: binary_sensor.auxiliary_dhw_state
    id: "hot_water_off"
    to: "off"
    
  # template entities reloaded or hass restarted
  - platform: event
    event_type: event_template_reloaded
    id: "reload"
  - platform: homeassistant
    event: start
    id: "reload"

  binary_sensor:
  - name: "douchen"
    state: >
      {% if is_state('binary_sensor.douchen', 'on') and not trigger.id == "hot_water_off" %}
      on      
      {% elif is_state('binary_sensor.auxiliary_dhw_state', 'on') and
        ( 
          trigger.id == "rising"
          or
          (states('sensor.humidity_badkamer_2e')|float(0) > 80 and trigger.id == "hot_water_long_on")
          or
          (states('sensor.humidity_badkamer_2e')|float(0) > 80 and trigger.id == "reload")
        )
      %}
      on
      {% else %}
      off
      {% endif %}

Hi,

If your issue is waiting for triggers to re-fire during debugging then I have a suggestion.

I’ve been working a lot on my templates.yaml to add custom template entities that only need to be loaded once a day (e.g. sun times, garbage collection, etc). Debugging was a pain, as originally I had to wait for a Restart HASS invoked from the File Editor.

The much quicker alternative to reload just template entities via the console:

  • Hit c on any screen
  • Type template
  • Select Reload Template Entities

This worked, but initially the reload event didn’t cause my templates to be recalculated as it fired a separate event.

The fix to allow easy debugging with all templates recalculated is to add event_template_reloaded to your triggers:

# templates.yaml
# no "template:" here as in configuration.yaml

- trigger:
    - event: start
      platform: homeassistant
    - platform: event
      event_type: event_template_reloaded
    - platform: time_pattern
      # only needs to change once a day
      hours:   05
      minutes: 05
<add your entities here>

This makes debugging easy, but I am seeing some inconsistencies with values calculated, such as as_timestamp() returning non-integers on some but not all dates. The unexpected values do appear on the Developer Tools.

Hope this helps,

James

Thanks for the help, but the issue is that I already use this, but for some reason it doesn’t work. See the example yaml I give above. I use the event_template_reloaded trigger, but it seems to be completely ignored, resulting in unkown states until there’s a ‘real’ trigger.

Code review suggestions:

  • I’d suggest creating a stripped-back template with state: set to all the input variables concatenated to check is the states are as expected.
    Something like (not tested…)
    state: "{{ trigger.id }} {{ is_state('binary_sensor.douchen', 'on') }} {{ s_state('binary_sensor.auxiliary_dhw_state', 'on') }} {{ (states('sensor.humidity_badkamer_2e')|float(0) > 80) }}"

  • You have a lot of state variables driving the template logic. My hunch is either a restart/reload executes before all are set to expected values, and/or there’s not enough {% elif %} to catch all the permutations.

  • Rewrite as automations to allow tracing with the visual flow tool?

I created a test trigger to check the behaviour of id:. On core-2022.4.3, a template reload initially showed reload, then the time trigger changed it to time - as expected.

# templates.yaml
# no "template:" here as in configuration.yaml

- trigger:
    - event: start
      platform: homeassistant
      id: "start"
    - platform: event
      event_type: event_template_reloaded
      id: "reload"
    - platform: time_pattern
      # change to a few mins in the future for testing
      hours:   16
      minutes: 16
      id: "time"
  sensor:
    - name: Trigger Test
      # Test to use id: in a trigger
      state: "{{ trigger.id }}"
      icon: "mdi:test-tube"

I think there’s still some misunderstanding. I’m not trying to get to work the above examples per se, some of them can also be achieved in other ways. The issue is that the event_template_reloaded trigger is not working for any of my templates even the simple ones (see the first example). All templates work fine otherwise, they just stay on unknown for too long. So I know for sure that the template part of the sensors is correct, there’s something wrong on the trigger side.

Hi Again,
As event_template_reloaded just works on my HASS install, I’m going to go back to suggesting creating a separate test trigger just for debug. There may be some complex parser issue with the code you already have, hence the idea of stripping things right back to the simplest example, and building back up again in stages.
This comes from years of dealing with vendor 3d line support who demanded a minimal code example to demonstrate an issue before escalation. Creating the test case often explained what was really going on!

Q: Does my test code above work in your install? If no, can you remove other triggers until it does?

As an aside, note that 2022.04 adds an alternative to the id: syntax - Variables on Trigger. This does suggest that the trigger code is changing behind the scenes.

I’m not quite sure in how many ways I can explain that I’ve already confirmed it is just the event_template_reloaded trigger that is not working. Frankly, I’ve a hard time imagining a simpler example than my first one anyway.

All your example does is show me what I already know, that all sensors are working fine in every other way.

Having said that, on recent tests it has gotten better, but in that weird “sometimes it suddenly works” kinda way. I suspect there’s some sort of race condition going on for this reason.

I’d rather not bump old topics, but after all this time I’m still having this issue with most of my template entities. I was just making a new very simple one, and it again shows as unknown on reloading the templates where it should just read off. Since I can’t find any bug report on this, I feel like I’m going crazy, I have to be doing something wrong, but I can’t figure out what.

The yaml is as follows:

- trigger:
  - platform: numeric_state
    entity_id: sensor.power_consumption_phase_l3
    above: 0.4
    id: "on"

  - platform: numeric_state
    entity_id: sensor.power_consumption_phase_l3
    below: 0.1
    for: 
      seconds: 180
    id: "off"

  # template entities reloaded or hass restarted
  - platform: event
    event_type: event_template_reloaded
    id: "reload"
  - platform: homeassistant
    event: start
    id: "reload"

  binary_sensor:
    name: "koken"
    state: >
      {{ trigger.id == "on" and states('sensor.power_consumption_phase_l1')|float(0) < 0.5 }}

Old topic, but you are right!
For trigger based entities, at template reload, the previous entiteit state is restored. That also happens without adding the related trigger.

But for new entities there is no previous state, so it remains unknown, till another trigger kicks in.

When you add the reload trigger, it won’t change that behavior. In my opion that is a bug. When the reload trigger is defined, the entity state should be recalculated. The same as for none trigger based template entities.

I will make a ticket.

I’d argue strongly against that. Recalculating the template when not specifically triggered could cause unexpected values to be resolved by the template. e.g. I only want to calculate a value just before midnight in many templates.

Reloaded @tom_l
What is the purpose of being able to add the reloaded trigger of it doesn’t trigger?

When I add that trigger, I deliberately want that template to be re-evaluated every time the templates are reloaded. Otherwise I would not add it.

1 Like

Geez. I even quoted you saying that. Sorry it’s late and I somehow thought you meant all triggered templates.

Apologies.

No problem👍
If the reloaded trigger, as the name suggests, triggers after the reload, all values should be settled , good night Eric

I found a simple work around!
Just create simple automation to monitor the event_template_reloaded event and then fire a my_event_template_reloaded event. That event will trigger your template entity, if you add the my_ prefix.

The automation:

alias: Template reloaded
description: ""
trigger:
  - platform: event
    event_type: event_template_reloaded
condition: []
action:
  - event: my_event_template_reloaded
    event_data: {}
mode: single

The trigger in the template entity:

  - platform: event
    event_type: my_event_template_reloaded

Thanks, I will try this out next time I run into the issue. I don’t understand your explanation of the bug though and I’m surprised your solution works, because in the end it really is the same thing isn’t it? The event is just ‘renamed’ this way.

@sophof The bug is basically that the reload event is ignored when declared as a trigger. Just relaying it to another event, does work.
Default behavior for trigger based templates is that the previous value is restored at a reload. Most of the time, that will be equal to doing a recalculation. But since this change, the event declared as an explicit trigger, seems disabled (the bug). During developing new templates, we need these recalculations to evaluate without a restart. Fixed with my workaround :slight_smile:

It’s not a bug. The template integration is reloading when the event is fired, therefor the template entities that rely on the event don’t see the trigger because they are reloading themselves. Your work around seems to work because the automation is forwarding the event, you can still run into issues if you don’t add a slight delay to your automation between the trigger and action.

e.g.

alias: Template reloaded
description: ""
trigger:
  - platform: event
    event_type: event_template_reloaded
condition: []
action:
  - delay: "00:00:01"
  - event: my_event_template_reloaded
    event_data: {}
mode: single

That sounds like it is correct to me, since it would also explain the ‘race condition’ nature of this issue. I do wonder if it isn’t a bug though, surely the event should wait until the reload is complete? Or is there no way to know when that has happened?

I would assume that is the case once all entities are created (still starting with an unknown status in that case, but very quickly evaluating based on the trigger).

1 Like

The name is ‘reloaded’. So if that is named correctly, there will be no race condition and there is no reason not to pass the event to the template entities as a trigger.
@petro thanks for the suggested delay. Note: I didn’t see any race conditions yet without this delay. But I’m not using any other template entities in the sensors that use this reloaded trigger. If people do that, the delay probably is a good idea