Is there a recommended way to use the highest/lowest/average value from a device over time within a RM5.1 rule?
Use case
I've got an espresso machine connected to an Iris Smart Plug. I've got a rule to turn it on (mornings, different times for daily/weekends and to keep it off if the HE "Mode" is set to "away") -- that's the easy part.
The Smart Plug reports power usage about every minute.
When heating up, the machine uses ~1100W+, as reported via the smart plug. The power usage is essentially 100% during the initial heating phase (~15 minutes).
After the machine reaches operating temperature, it automatically cycles power. At this point, the power usage would look like a square wave, with peaks at 100% (~1100W).
If the machine is idle for 30 minutes, it will go into a low-power mode, using only one heating element. During this phase, the power usage is still a square wave, but with a lower amplitude (~500W) and longer intervals between applying power and shorter periods when the heaters are on.
I've already got a rule that turns the machine off with the following logic:
WHILE the machine has been on for at least 1:45
IF the last reported power usage was <50W THEN
PAUSE 5 minutes
IF the last reported power usage is <50W THEN
turn off the machine
end the rule, cancel the loop
ENDIF
ENDIF
DONE
That works, but it often turns off the machine when it is not in standby -- if the heaters were simply idle during the 2 instantaneous checks.
Can anyone suggest the best way to write a rule in RM5.1 that will record the highest usage over time and take action on that value, as in this logic to turn off the machine if it has been in standby mode for 15 minutes:
HighestUsage=0
Intervals=0
WHILE the machine has been on for at least 1:45
IF Intervals == 15
IF HighestUsage < 500W THEN
turn off switch, end rule
ELSE
# reset the counter & the high-water mark
Intervals=0
HighestUsage=0
FI
ELSE
# if possible, trigger a poll/refresh of the smart plug to get the InstantaneousWattage,
# or use the last reported value
IF InstantaneousWattage > HighestUsage THEN
HighestUsage = InstantaneousWattage
FI
Intervals++
PAUSE 1 minute
ENDIF
DONE