Hour Meter
-
Hi all,
Is it possible to display a hour meter on the dashboard? I have a standby generator and would love to log the hours that the generator runs, I can detect the AC with a CT clampon, so if I can send the start of the generator to MyController and start the hourmeter and stop when the generator turns off?
Any ideas on how I could get this to work please?
Regards -
@esawyja To achieve this we can create a script, rules, and template, repository variable, uid tag.
-
Create Uid tag for your generator status variable.
-
Create repository variable to store and calculate usage. Note: when you want to reset your calculation, update all these values as
0
-
Create script,
//To run this script you need to pass the following bindings // {"deviceUid":"generator-status", "repositoryKey":"generator-hour-meter"} var myImports = new JavaImporter(java.lang, java.util, org.mycontroller.standalone.utils.McUtils); with(myImports) { var generator = mcApi.uidTag().getByUid(deviceUid).getResource(); var meter = mcApi.variable().get(repositoryKey); var status = {}; var saveChanges = false; // If user requested to reset. if(meter.value2 === '0'){ status.enabled = 0; status.start = 0; status.end = 0; meter.value = 0; meter.value2 = Date.now(); meter.value3 = JSON.stringify(status); meter.save(); } //Copy caclulation data from repository status = JSON.parse(meter.value3); if(generator.value === '1' && status.enabled == 0){ status.start = Date.now(); status.enabled = 1; saveChanges = true; }else if(generator.value === '0' && status.enabled == 1){ status.end = Date.now(); status.enabled = 0; meter.value = parseInt(meter.value) + (status.end - status.start); saveChanges = true; } //Save changes to repository if(saveChanges){ meter.value3 = JSON.stringify(status); meter.save(); } //Calculate usage and update to display var time = {}; time.raw = (status.enabled == 0 ? 0 : Date.now() - status.start) + parseInt(meter.value); time.hour = 0; time.minute = 0; time.second = 0; var timeTemp = time.raw; //Update hour if(timeTemp > McUtils.HOUR){ time.hour = (timeTemp / McUtils.HOUR) | 0; timeTemp %= McUtils.HOUR; } //Update minute if(timeTemp > McUtils.MINUTE){ time.minute = (timeTemp / McUtils.MINUTE) | 0; timeTemp %= McUtils.MINUTE; } //Update second if(timeTemp > McUtils.SECOND){ time.second = (timeTemp / McUtils.SECOND) | 0; } time.str = {}; //Update time as string value time.str.hour = (time.hour < 10 ? '0' : '') + time.hour.toString(); time.str.minute = (time.minute < 10 ? '0' : '') + time.minute.toString(); time.str.second = (time.second < 10 ? '0' : '') + time.second.toString(); }
-
Create an
Operation
with bindings to execute this script, Bindings:{"deviceUid":"generator-status", "repositoryKey":"generator-hour-meter"}
-
Create Rule to trigger this script, when there is a change on your generator,
Rule#1:
Rule#2:
-
Create
HTML template
to display on dashboard,
<div class="adf-myct"> <div class="adf-myct-time">${time.str.hour}:${time.str.minute}:${time.str.second}</div> <div class="adf-myct-date"><small><I>Since:</I> {{ ${(meter.value2)!"Never"} | date:mchelper.cfg.dateFormat:mchelper.cfg.timezone}}</small></div> </div>
- Configure your template and script on Dashboard,
Displaying time format:
hour:minute:second
-
-
@jkandasa BRILLIANT, thank you soooo much!!, I will test this out over the weekend and give some feedback!
-
I must be doing something wrong, I have a CT on my water heater and I tried to use that as a trigger, I see the clock, but does not run
If I look at the rules I can see that they were triggered, but the clock does not want to update
Please would you check my implementation and let me know what I'm doing wrong
Regards -
Please would you check my implementation and let me know what I'm doing wrongUse Geyser
Status
NOTWatt
-
Hi
Thank you, Let me try that, I just thought that detecting the watts used will be a good enough trigger
Regards -
@jkandasa It works when I use status, but any reason why my logic on watts used does not work please? I would like to understand, is this written only for state?
Regards -
so if I can send the start of the generator to MyController and start the hourmeter and stop when the generator turns off?
Yes, based on your requirement. You have asked to do with status of generator. However you can modify this script as like you want.
-
Thanks so much
-
This is really great!
Would it be possible to track a status usage like this, but on a daily basis, so that I can monitor the daily usage in a chart? -
@daniele You may need to create a dummy sensor and update sensor value everyday night via a script.