Is there a simple way to track how long something is turned on in a day?"

I'm not trying to track power use by using a smart plug that monitors usage. I'm trying to find out how long certain lights and fans and things are on. (that have smart switches that report back to Hubitat if they are on or off)

Is there an easy way to do this?

Not necessarily what you'd call 'easy', but doable. In Rule Machine you'd setup some local variables, or perhaps use some Hub Variables. You can trigger a rule off a switch changing its state, and depending on the new state, add some time to a total. You can set a variable to current time and date. So we can set one before this even starts, or at midnight or something. When the switch turns on, we grab the time. When it turns off we also grab the time, and then see how many minutes have passed. Add those minutes into a running total, etc. Each of these elements is available:

The approach shown here has a resolution of minutes. It would also be possible to do this with a resolution of milliseconds, were one so inclined.


I feel like I am almost there, I think I am missing some of your picture on the top. How do I grab the time of it turning on and off? Was there a rule above your picture of "set now to current date and time) rule?

Right now my version of this was seeming to just add to a running total of time on time since "start" but isn't resetting the start.

There's more to it than what I showed above. That was just to illustrate a bit. I'm not going to solve this tonight. I was actually thinking it might be fun to write a small app to do this, for any number of switches, and show them in a little table. I may take that on tomorrow as a small project, because there isn't much to it.

When switch turns on:  set on-start to now
When switch turns off: 
   set on-stop to now
   set elapsed to difference between on-start and on-stop
   add elapsed to total

Those could be the two sides of an IF-THEN-ELSE, with a trigger of switch changed.


Someone is planning on writing an app to monitor energy use and cost system-wide or per device. That would - or could - probably cover what you want as well Energy Tariff - similar to Modes Let them know you're interested - to encourage their effort :smiley:


That would be a perfect sample of the new tables UI if published on HubitatPublic ! :slight_smile:


That's what I was thinking...

1 Like

See this post: @ryanrwalker

1 Like

This is really slick Bruce. I will be using it all the time. Thanks so much. :ok_hand:

Thank you, Bruce! Exactly what I was looking for!

Interesting. I'm tracking all this via influxdb/grafana. I log all switch statuses and have a dashboard I can go back and look at a certain timeframe how many hours something was on.

A beautiful piece of programming Bruce.

The app is very handy. I would like to be able to reset all the variables (timers) via Rule Machine. That would allow me to record the days on-time, and reset the timers at midnight or whenever.

Probably the easiest way to do this would be another Hub Variable, like a Boolean. When it changes to true, reset the timers. The rule would need to set it back to false. I will poke at it...

Actually, the app can reset the variable when it is triggered.

See here and GitHub.


Excellent. Thank you so much. I need to send you a donation for all the things you do on here. Pm me a paypal address/user and I'll get it right out.

I Figured out how to do this by having it set the start variable only when the light in question is turned on and set the Now variable for any action then subtract the difference and add to the total variable. Have been testing it for a little bit, but I see that you have made something infinitely more useful. Thanks!

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