Template Trigger Not Triggering

Can anyone see why this isn’t triggering on the second condition? First condition appears to be triggering fine.

platform: template
value_template: >-
  {{states('sensor.device_04_power')|int < 40 or
  states('sensor.house_average_humidity_ground_floor') |float - 4 | float |int > 60 }}
for:
  hours: 12
  minutes: 0
  seconds: 0

Have you tried putting the above into Developer Tools > Templates to see what it returns with different levels of humidity?

I’m wondering if the precedence of operations is a problem.

I think you may be missing some brackets here:

(states('sensor.house_average_humidity_ground_floor') |float - 4 ) | float |int > 60
^                                                                ^

Also, I think you can just use |int instead of |float |int.

Yes - both conditions are returning expected value in developer tools.

Are you sure that the humidity was above 64% for 12 hours…? All it takes is one reading of 64% or lower (or the sensor going unavailable) to reset the clock.

I’ve checked in Grafana - lowest value over the past 12hrs has been 66.6% i.e 62.6% after the 4% correction is applied hence the automation should have triggered?

Does it work if you separate your template trigger into two separate template triggers? So, rather than use the ‘or’ operation, use the natural ‘or’ from how multiple triggers are handled.

No, if i separate them out and then change the value of the average humidity sensor using set state within the Developers Tools it still doesn’t trigger.

Changing the state in Developer Tools won’t necessarily trigger your automation. The state needs to be below 64 and then above 64 for 12 hours. The state is likely to change the next time your sensor sends a reading to HA. This could be any time. The 12 hours may not be reached.

Maybe try removing the 12 hour condition and try Dev Tools again. Set the state to below 64 and then above 64.

1 Like

EDIT: deleted my post, as I totally misread the question! Sorry! :slight_smile:

OP’s example is a Template trigger for an automation, not a sensor configuration. Template triggers do support the use of the for: key to define a duration they must render as true before the trigger fires.

Do you realize the trigger will only occur exactly 12 hours after the moment that the template result changes to true (and remains true for exactly 12 hours)? If the situation has been that the template was true for longer: no trigger. If you reboot inbetween, no trigger. So you need the template to evaluate false, keep HA running, and then when it goes to true and stays there for exactly 12 hours, then it will trigger.

Worse, if you have other conditions in the automation too: If condition in the automation is blocking the situation, then the automation won’t run. You’ll need to wait for the condition to evaluate false again, then change to true, stay that way for 12 hours AND all other conditions need to be met at that point in time.

Ok - that explains it! The sensor value was already above the trigger value when I implemented the automation hence why it’s never triggered based on the humidity trigger. Makes perfect sense as the template was triggering based on the power sensor condition. I may need to re-think the 12hr trigger logic. For those of you interested, here’s the final trigger template taking all comments into account. Thanks to everyone for your help on this.

platform: template
value_template: >-
  {{states('sensor.device_04_power')|int < 40 or
  (states('sensor.house_average_humidity_ground_floor') |float - 4 ) |int > 60 }}
for:
  hours: 12
  minutes: 0
  seconds: 0

Oh, I totally misread that! Sorry! :blush: For automations this is something totally different…