Automations are not triggering intially after turning on

Hi all,
This one has me a bit stumped.
I upgraded my home assistant to 2022.8.6 from 0.116.4.

The following automation was working fine on the old version:

  • id: mobile1heateron
    alias: mobile1 heater on when below min input
    trigger:
    • platform: template
      value_template: “{{ states(‘sensor.mobile_temperature1’)|float < ( states(‘input_number.mobile_temp1_min’)|float - 0.4 ) }}”
    • platform: template
      value_template: “{{ states(‘sensor.mobile_temperature1’)|float < states(‘input_number.mobile_temp1_min’)|float }}”
      condition:
      action:
    • data:
      entity_id: switch.mobileheatersw1
      service: switch.turn_on

After creating a fresh os install, installing new version of HA 2022.8.6 and restoring my backup across, it was found that i needed to update “float” to “float(0)” to allow for this to be rendered properly. This was the only change so far.

  • id: mobile1heateron
    alias: mobile1 heater on when below min input
    trigger:
    • platform: template
      value_template: “{{ states(‘sensor.mobile_temperature1’)|float(0) < ( states(‘input_number.mobile_temp1_min’)|float(0) - 0.4 ) }}”
    • platform: template
      value_template: “{{ states(‘sensor.mobile_temperature1’)|float(0) < states(‘input_number.mobile_temp1_min’)|float(0) }}”
      condition:
      action:
    • data:
      entity_id: switch.mobileheatersw1
      service: switch.turn_on

I have since found that the automation does not fire when turning the automation “on” & the triggers are already “true”.
The triggers shown are intialized in the logs, the triggers themselves would be starting off as “True” where the actual room temp is below input.

I’ve found that if the trigger goes from ‘True’ to ‘False’ & back to ‘True’ again, it will fire for the first time.
This was not an issue on the old version, and worked straight from the start - If the trigger=true it would run immediately.

I’ve also noticed odd behaviour in the “dev tool” > “template” for math operations. If I enter in:

“{{ states(‘sensor.mobile_temperature1’)|float(0) < ( states(‘input_number.mobile_temp1_min’)|float(0) - 0.4 ) }}”
“{{ states(‘sensor.mobile_temperature1’)|float(0) < states(‘input_number.mobile_temp1_min’)|float(0) }}”
“{{ states(‘sensor.mobile_temperature1’)|float(0) > states(‘input_number.mobile_temp1_min’)|float(0) }}”
“{{ states(‘sensor.mobile_temperature1’)|float(0) }}”

“{{ ( states(‘input_number.mobile_temp1_min’)|float(0) ) }}”
“{{ ( states(‘input_number.mobile_temp1_min’)|float(0) - 0.4 ) }}”

I will get the following output:

“True”
“True”
“False”
“15.0”

“19.3”
“18.900000000000002”

The expected value for the last line wouldve been ‘18.9’. I dont think this would be related since my triggers are based on greater/less than, but I felt should be pointed out anyways as a potential bug?

This is one example for why it’s usually poor practice to turn automations on and off instead of designing the conditional logic.

From the Docs:

Template triggers work by evaluating a when any of the recognized entities change state. The trigger will fire if the state change caused the template to render ‘true’ when it was previously ‘false’.

The evaluation of the template must change from “false” to “true” for the trigger to fire.

It’s not a bug. What you have observed is a known phenomenon called machine epsilon.

Thanks for the help.
The want for heating is mostly at whim and not always based off presence or timings, nor even pure temperature. Reasons such as wanting to save electricity for example, not necessarily just being in the room.

Having said that, the linked doc seems to be providing a workaround for this by using a true state duration to enforce the trigger:

Use of the for option will not survive Home Assistant restart or the reload of automations. During restart or reload, automations that were awaiting for the trigger to pass, are reset

I do still find it odd that the same automations worked in previous version of home assistant.

Cheers for that.
A phenomenon then :laughing:
I was leaning more towards “exception” at posting time, but at last minute decided “bug” would be better conveyed.

Ok this is actually strange.
I’ve added

for: “00:00:02”

In each of the automations, yet still the trigger for heater is not firing.
Interestingly I have a basically the exact same syntax but for a humidifier which is firing if trigger=true on initial without requiring any trigger template result changing between true/false:

  • id: mobile1humidifieron
    alias: mobile1 humidifier on when below min input
    trigger:
    • platform: template
      value_template: “{{ states(‘sensor.mobile_humidity1’)|float(0) < ( states(‘input_number.mobile_humidity1_min’)|float(0) - 0.4 ) }}”
      for: “00:00:02”
    • platform: template
      value_template: “{{ states(‘sensor.mobile_humidity1’)|float(0) < states(‘input_number.mobile_humidity1_min’)|float(0) }}”
      for: “00:00:02”
      condition:
      action:
    • data:
      entity_id: switch.mobilehumidifersw1
      service: switch.turn_on
  • id: mobile1heateron
    alias: mobile1 heater on when below min input
    trigger:
    • platform: template
      value_template: “{{ states(‘sensor.mobile_temperature1’)|float(0) < ( states(‘input_number.mobile_temp1_min’)|float(0) - 0.4 ) }}”
      for: “00:00:02”
    • platform: template
      value_template: “{{ states(‘sensor.mobile_temperature1’)|float(0) < states(‘input_number.mobile_temp1_min’)|float(0) }}”
      for: “00:00:02”
      condition:
      action:
    • data:
      entity_id: switch.mobileheatersw1
      service: switch.turn_on

Is there any common knowledge or best practices that might help? Do you always need to restart Home Assistant when defining a new automation? I’ve tried everything, but I still can’t get this to work. Any idea why?

alias: Turn on switch when sensor is larger than 500
description: Turn on the switch if the sensor value exceeds 500
triggers:
  - value_template: "{{ (states('sensor.ams_ec5d_p') | float(0)) >= ( 500 | float(0)) }}"
    trigger: template
conditions: []
actions:
  - type: toggle
    device_id: bdb56bab5f9ef9d9e0d4210ec044185f
    entity_id: 10aa6676e1da35cc7b7327853c5c5938
    domain: switch
  - action: notify.notify
    metadata: {}
    data:
      message: >-
        The sensor value is {{ (states('sensor.ams_ec5d_p') | float(0)) }} and
        has exceeded 500.
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
mode: single


When I force-run the automation, I receive the message ‘The sensor value is 901.8 and has exceeded 500.’ This confirms that the sensor works and returns different values above 500, which should result in the automation running almost every minute.

This works as a trigger, but not my sensor?

trigger: time_pattern
minutes: "1"

What are you basing this on?

Sorry; Background: the value during the last couple of hours will exceed 500 and now its 900.0 and since I have a delay which I right now see is not included in the core. let me edit.

So, Trigger is an event. And most likely my automation already set this to true and will not fire again until tomorrow? It saves that event even if I restart the automation?

Triggers “listen” for events. A Template trigger “listens” for the event of the rendered value of the template changing from false to true.

The automation does not “set anything to true” or “save that event”. If the template already renders true when reloading the automation or restarting HA, the trigger will not fire until it renders false then true.

If you need the automation to run on restart, you can in use the Home Assistant Trigger with event set to start. If you really want it to run every minute you can use a Time Pattern trigger. Time Pattern triggers are rarely necessary and not the recommended pattern for most automations in HA.

Thanks for trying to explain!