Device usage (duration)

Hi, was wondering if I can get how long a device has been on. The idea is to use this info to calculate the energy consumed by each device based on its energy rating.

Sure. If you just want to look "manually," the "Events" tab on the device detail page will show you recent events, so you could look for the "switch" attribute/event and the "on" and "off" values.

More likely, I'm guessing you want an app to respond to these and calculate something for you. This would be easy-ish to do with a Rule Machine rule. If you are new to the platform, this may seem daunting, and Rule Machine can be, so feel free to ask for help if you need more guidance! But the general idea would be:

  1. Create a hub variable from Settings > Hub Variables of type Number, or create a just a local variable inside this rule if you don't have a need for it elsewhere. (I'm thinking you might.) I'll call it switchOnDuration for the purpose of this example.
  2. Regardless of what you do in step 1, also create three local variables inside the rule (these could also be hub variables, but there's no need). You'll need two of type DateTime, which I'll call these startTime and endTime here; and you'll need one of type Number, which I'll call timeDifference.
  3. Additionally, create another local variable called timeDifference of type Number.
  4. In a new rule, add a trigger event: Switch on (for your switch).
  5. In Actions to Run, create actions like:
Set startTime to current time
Wait for event: Switch off
Set endTime to currentTime
Set timeDifference to minutes of difference between startTime and endTime
Set switchOnDuration to (switchOnDuration + timeDifference)

By the way: All of those "Set..." actions are done via Set Variable, Mode or File, Run Custom Action > Set Variable and the options presented for that action. The "Wait for event" is under Delay, Wait, Exit, or Comment.

When all finished creating this rule, hit either "Done" or (to keep the rule open but do the same thing) "Install Rule." You may wish to consider enabling logging if you want to troubleshoot or see what the rule is doing.

Good luck!

1 Like

Thanks a lot, I thought about the same also. The good question is that I have to this for each device to have an approximate overview of the home consumption.

My idea is to create an app where you define the devices in scope and be able to define the power rating for each device. Then the app can do the job and use it to generate reports which can be added in dashboards.

What is your opinion ?

If you have a lot of devices and want to aggregate all of their times, a custom app might be easier. You could also use multiple rules (can't think of a great way to do it in a single rule and I wouldn't make that a goal without a good reason, but there might be something...).

1 Like

Well I am not an expert on groovy language :frowning:

Then maybe a rule (or few)? :slight_smile: After you create one rule, you can clone it and swap out devices and variables with others needed, if that's helpful to know--could save some time.

How to clone rules ?

From the App Status page for that app: App Status - Hubitat Documentation