Hello guys, as I diverted photovoltaic surplus to a water heater, I need an automation that turn on a pump every time the energy counter increases of a certain amount. As surplus energy is not constant, I’d like using a better solution than simple time pattern.
This is my actual code but it only triggers once at the beginning and not at the subsequent increments.
Sorry, I forgot to explain the reason of that “*8”. As the sensor value is kWh and I want to trigger the automation every 125W, I simplified the original
The trigger is intended to start every time the resulting integer value changes.
Thanks for the different quotations advice that I was ignoring, even if I noticed HA already formatted it in the correct way.
I’m sure there are cleverer ways, but I’d do it by setting up an input_number helper. Then:
on first run (triggered by automation reload or HA restart), run an automation that reads the sensor value in your “number of 125Wh increments” units and store that integer value in the input_number.
then your second automation is triggered by a comparison of a new reading in 125Wh-units not being equal to the stored value. Run the pump and update the value in the input_number.
And you can’t use == 0 instead of > 0 as the number you’re looking at might jump from 124Wh to 126Wh in one step. 2Wh in a 30-second sampling interval is 240W average power.
Actually it seems to work exactly as finity said, when the value is multiple of 125.
But there is the problem you mentioned too: since sensor readings are about one every 10 seconds it often misses the exact value, especially at highest heater rate, and so the trigger does not start.
So tomorrow I’ll try the following in order to achieve less “granularity” and avoid holes in the sequence of values {{ ((states ('sensor.heater_kwh_today') | float*200)|int % 25) >0 }}
In practice I reduced the values to a fifth of the original and cut off decimals
Genuinely interested what the trigger values are. By my testing, your template should trigger for any state update except a multiple of 125Wh and 4Wh after that multiple:
Every time is triggered the pump switch on for 1 min twice, separated by a 1 min delay.
As you can see it seems all fine except for that time in which te pump doesn’t switch off.
I need to investigate further.
That’s probably related to the period when the template in my example is returning false.
If you compare the switch operation against the value of sensor.heater_kwh_today, you’ll probably find it’s triggering every time there is an update, regardless of value except at or shortly after a 125Wh increment.