Is this the best way to calculate time switch is on? Seems awkward

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.


Purifier Bedroom Elapsed Time is a hub numeric variable to accumulate the total.

Seems very concise to me, given the extreme limitations of "variable arithmetic" available within RM. Can't think of anything more efficient. Nicely done!

I'm curious, though... what Triggers this rule? I see the Condition(s) tucked within an IF-THEN-ELSE, but my brain is telling me, "Won't the IF part always be TRUE so long as the Purifier is "On"?" (In other words, is the IF-THEN-ELSE construct truly necessary? Depends on the Trigger, lol.)

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.

1 Like

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.

1 Like

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.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.