I want to accumulate elapsed time an air purifier is on to note when to replace the filter. Seems like the math is a bit cumbersome. Is there a more compact way to do this? Am I missing some hidden math capabilities? See copy of rule below.
It's run by other rules after they change one or the other of those two variables. This was written before RM gained capability to use variable change to trigger.
Turns out I missed some subtleties in my setup, as the "on" or "off" action can be triggered multiple times in a row. Particularly when the off action is triggered without a preceding on, it causes the cumulative time to be updated by the difference between last on and current off, which is incorrect, as the purifier has been off for a while. @LibraSun 's question was appropriate. Here's the full replacement, which monitors the outlet switch instead of a rule, and has a test for filter expiration (6 months run time). That sets a virtual switch on my dashboard ("Filter Due"). When I turn off the switch on the dashboard, another rule resets the cumulative time to zero. I also added a test to see if the switch was really turned on or off, as I've had some spurious trigger events on "switch changed" when it really hadn't changed state.
I did one more edit, to only change the hub variable once, updating it with the correct increment. This is just in case, sometime in the future, I will use the hub variable for a trigger. It's not a good thing in principle to use hub variables for calculations other than final values.