Defining exactly the same comparison logic into a binary template sensor and triggering off the binary sensor state.
There is something wrong with trigger (and condition) templates that use value comparisons for variables cast as floating point. Integer comparisons seem to work.
I have similar template automations to turn on/off a fan based on a comparison of two humidity sensors. It works great but if I manually turn on the fan it stays on.
Am I correct that it only triggers when the template output changes from true/false and not each time the humidity values change?
I have a template trigger that says when humidityA - humidityB < 5 then turn off the fan.
If I manually turn it on, it stays on even if the humidity values update and the template still renders true.
It looks like the way it works is when the difference goes above 5, it renders false and then back below 5, it renders true again and the fan turns off.
Template triggers work by evaluating a template on every state change for all of the recognized entities. The trigger will fire if the state change caused the template to render ‘true’.
Unfortunately “state change” is a bit ambiguous. I take it to mean if the state of any of the entities changes, or even if an attribute of any of the entities changes.
Next, the docs seem to imply that when any of these state changes occurs, if the template renders true, the trigger will fire. But now I’m pretty sure that is not the case. I think you’re right. If it has “fired”, then the template has to render false and then true before it will fire again.
I think you can find the answer in the code here. Notice in particular the already_triggered variable.
The way I read it is as described above. I.e., after start-up, the first state change that happens that causes the template to render true will make it “fire.” After that, at least one state change has to happen to cause it to render false before it will fire again (which will happen on the next state change that causes it to render true.)
FWIW, I think the way to fix that is to move the template to a condition, and then change the trigger to a state trigger with all of the entities from the template. That way it will trigger whenever any of the entities change (and I mean their states or any of their attributes), but the actions will only run if the template is true.
BTW, same thing for your originally posted template trigger @tom_l.
Thanks, your example clarifies how I am finding it works, at least I can use this now knowing what should trigger it. I guess it makes sense, my humidity sensors update every minute so it would trigger a lot when the switch action is not required if it happened on every change of humidity.
Almost none. When the template is in a template trigger, it will be evaluated for every state change. Now the processing will just move to the condition. It will still have to be evaluated for every state change. There might be some minor extra overhead in having both a trigger and a condition, but I would think that would be “in the noise.”
Ok, I am going to try it. Is the best way to trigger on any state change of my sensors to use humidityA != ‘unknown’ or humidityB != ‘unknown’ like you did above?
Actually, thinking about it this won’t work untill it goes to unknown first, which it never will (it’s the same issue again). I will just use the state platform and list my sensors as entities.