Templates broken after update 2022

After the update to 2021 to 2022 I have found most of my binary sensor templates are now broken…but I can’t make heads or tails of why?

Example:

      basement_main_floor_high_temp_difference:
        friendly_name: "Basement Main Floor High Temp Difference"
        delay_on: '00:05:00'
        delay_off: '00:10:00'
        value_template: >-
          {{ (states('sensor.main_floor_average_temperature')|float(0) - states('sensor.basement_rooms_average_temperature')|float(0)) | abs > 2 }}

The value shows as “unknown”:

But the inputs are most definitely updating (I can watch the values change), and its a simple true false condition?

Template debug:

{{ states('sensor.main_floor_average_temperature')|float(0) }} - {{states('sensor.basement_rooms_average_temperature')|float(0)}} | abs > 2
{{ (states('sensor.main_floor_average_temperature')|float(0) - states('sensor.basement_rooms_average_temperature')|float(0)) | abs}} > 2
{{ (states('sensor.main_floor_average_temperature')|float(0) - states('sensor.basement_rooms_average_temperature')|float(0)) | abs > 2 }}
{{ states('binary_sensor.basement_main_floor_high_temp_difference') }}

Output:

70.17 - 68.2 | abs > 2
1.9699999999999989 > 2
False
unknown

So why is the template stuck at “unknown” when it is most certainly a defined “false”? I don’t understand how a valid input can result in unknown output?

image

But shouldn’t they eventually…go to something other than “unknown” eventually? Its been half an hour with changing inputs and STILL unknown? All evaluate to known trues or falses?

I have a theory why this happening. As an experiment, remove the delay_on and delay_off options then restart Home Assistant. Let us know if the Template Sensor still reports unknown.

Instantly resolves to “off” after removing the delay off/on and reloading.

70.1 - 68.1 | abs > 2
2.0 > 2
False
off

So did something about the update break delay_on and delay_off? That would be most unfortunate given how heavily I use those in templates to get more refined behaviors (and nearly all of my similar binary_sensor templates are “unknown” post update regardless of input source)

Yep your theory is correct. I see that with this sensor:

- name: "Excess Solar"
  icon: "mdi:solar-power"
  state: "{{ ( states('sensor.lp20_inverter_power')|float(0) - states('sensor.hot_water_power')|float(0) ) > 2500 }}"
  delay_off:
    minutes: 10
  delay_on:
    minutes: 10

No, it just takes that amount of time to change from unknown to on or off after a restart.

Except long after the elapsed delay_on and delay_off are over, it is still “unknown”.

Hmm. Mine isn’t. It comes available to either on or off as soon as ten minutes pass after a restart.

Are your temperature sensors slow to update?

Because the template wont update until one of them changes. Then 10 minutes later the state changes.

The temp sensors update every ~15 seconds so I don’t see how that could cause 30+ minutes of being stuck.

I have also tried some stuff and it looks like if I remove delay_on it instantly populates with on or off as soon as the first temp update hits (even if its “off” and I have a delay_off configured)

Ok, it just got more strange…after putting back the code the way I had it with delay on/off (so I don’t forget, I’m calling it a night) and reloading just templates (not all of home-assistant) most of them started working as expected suddenly?

Now I almost wonder if there’s something still churning post-update in the background messing with the results…maybe I should revisit this after its sat overnight or something.

It really should work. I’ve done a few restarts today, you can see the 10 minute unavailable grey sections:

My guess is that the influence of the two options wasn’t anticipated when the decision was made to implement the unknown state for Template Binary Sensors. Alternatively, it was a known factor but considered to be acceptable (i.e. the state value is truly unknown until the template is fully evaluated).

My $0.02 is on the first proposition.
The current code do an update whatever the delay if the state is None. It should very likely be replaced or augmented by “unknown”.

To me it is operating as expected.

A restart causes the state to change from unknown to on or off when the template evaluates. Then the delay_on/off has to expire before the state is updated.

Well yeah, technically it works, but conceptually, it doesn’t really make sense to delay if the “from” state is unknown (or unavailable for that matter). You likely want an actual state asap.
It might make sense if the “to” is, though, but then again…

Probably worth opening an issue to clarify if it’s an omission or “works as designed”.

1 Like

I had the same issue and solved it by adding a template for the delay value.
I’m checking the input state of the used entity. When the state value is ‘None’ there’s no delay, otherwise the delay_off in my case is 15 seconds,

    delay_off: >
      {{ 0 if is_state('binary_sensor.xyz','None') else 15 }}

On restart and startup the sensor is showing directly the correct value (False or True)

4 Likes

I didn’t realise those options supported templates.

@Mariusthvdb has an issue open for this. We were talking about it on the beta discord.

Not really a big deal for the templates that only have delays of a few seconds or minutes but I have a couple that are 4 hours and it really buggers things up for me. It is an issue when reloading templates (which I do A LOT sometimes) as well as restarts. I’ve been having to manually change their state to the correct value after reloading.

2 Likes