Is RM an appropriate way to calculate device state duration data?

If I want to track the duration of certain states (eg: how long a contact is opened, how long is a switch in each state?, etc) and under specific scopes (today, this week, yesterday, last week, etc), is RM the way to go? I'm guessing I want to store the results in global variables so it can be referenced in other rules or displayed in dashboard tiles. I don't need stats for all my devices but only specific ones.

One approach is to run a rule whenever a device state changes (eg: contact opened) and use a lot of rules involving variable math on date/time in epoch seconds. But this seems overly complicated for a rule (or actually, a few rules) and perhaps there is an app that does it better.

And perhaps it would be easier if I can just query the eventlog of each device and get the state history that way. So either have the query run at intervals to update stats or have a device state change to trigger the query. But I guess I need to learn Java to do this?

My specific use case is for a humidifier with on a contact sensor on a output vent flap so that when it's running, the contact is open and when idle the contact is closed. I don't want the humidifier to burn out from running for too long and if it runs for more than x hours per day, I cut power to it using a smartplug.

See also:

Using Rule Machine Legacy, I created a simple rule that ran every 1 minute (the trigger). If a switch was on and the hub was in a certain mode (the "if" conditional action), it just added +1 to a variable (the "then"). I just went to find it and discovered that apparently 1) Rule Machine Legacy variables aren't available to Rule Machine 5.1 (which is stupid, IMO, and 2) now we apparently can't see RM 5.1 variables in Devices (same comment). But now that I've got that griping out of my system, perhaps this simple Conditional Action will work for you (checks for both my office heater switch being on and my HE hub being in work mode, incrementing the variable "Heater On - Work Hours":

IF (Office Heater(off) is on(F) AND
Mode is Work Hours(F) [FALSE]) THEN
Add 1 to Heater On - Work Hours

I'd suggest that a good way to make RM / HE a lot more useful would be to add a "Start/Stop Timer" function. If that exists, I don't know how to use it. That said, I'm not the world's biggest fan of Rule Manager, but need it from time to time, so...