Preventing multiple-triggering of template sensors

I am setting up heating controls for my house, and have:

  1. A template sensor for each room that figures out the flow temperature that the room needs from the boiler.
  2. A “combined” sensor that is set to the maximum of all of the above, and determines the flow temperature that is actually demanded from the boiler.
  3. A template sensor for each room that figures out how much to open the TRV based on (1) and (2). i.e. if (2) is > (1) then we don’t open the TRV all the way.

This is generally working well, except that (3) triggers multiple times, resulting in a noisy output. i.e.:

  • (1) updates, which triggers both (2) and (3);
  • (3) updates based upon the new value of (1) but the old value of (2);
  • (2) updates, which triggers (3) again;
  • (3) updates again, based upon the new values of (1) and (2).

Note that if (1) is not the highest value sensor, it won’t result in a change of (2), so I can’t just trigger (3) when (2) changes.

Adding a delay to (3) would be the obvious fix, giving (2) chance to update before (3) is actually executed.

However, there doesn’t seem to be any way to add a delay to a template sensor? Am I going to have to use an automation to drive these templates just so I can add in a delay, or is there a better way?

Not sure what you mean about the automation bit, but you can have a state delay if you make your template sensor trigger-based.

Make your template sensor output the last result unless all 3 entities have updated within a certain time frame of each other. All doable within the state template using last_updated or last_changed.

The other option is to make a trigger based template entity that polls the values of the sensors at a set frequency. No need to worry about updates then.

How do I do that? The documentation only talks about delays on “binary_sensor” entities, not on “sensor” entities.

You can use all of the same triggers available for automations. The trigger is what decides when your template updates; without a trigger, the sensor figures it out on its own based on changes to the entities it depends on.

So, you can use a state trigger with the for option. See this part of the doc about triggers. If you do it right, the template sensor will wait for another sensor to hold its state for the time period you configure before updating its own state.

Probably a good idea not only to avoid multiple triggers in your specific setup but also to avoid driving the valves too much if there are other sources of noise.

1 Like