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


  • ADMIN

    @esawyja To achieve this we can create a script, rules, and template, repository variable, uid tag.

    • Create Uid tag for your generator status variable.
      0_1489559343501_upload-bfb87465-6aca-485a-b3eb-6c4641dae107

    • Create repository variable to store and calculate usage. Note: when you want to reset your calculation, update all these values as 0
      0_1489559393152_upload-6285b400-2d4f-4520-9a93-a0f786da66be

    • Create script,
      0_1489559482727_upload-c30a6a6e-0506-40ad-bb9b-949a97cbd388

    //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"}
      0_1489559628728_upload-86906c91-f2de-4e16-8c09-315907a64230

    • Create Rule to trigger this script, when there is a change on your generator,
      Rule#1:
      0_1489559828355_upload-b44da7d4-1604-45e5-99ef-7fcb9f2d6ba3
      Rule#2:
      0_1489559903375_upload-ecc69498-0327-409f-9274-1cb8ead7f92e

    • Create HTML template to display on dashboard,
      0_1489559972617_upload-311776a0-ebe1-4b7d-9a3c-00064af31708
    <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,
      0_1489560051207_upload-96b76abe-ab4b-4f90-8e9e-0ee4fe98a756

    0_1489560138288_upload-05bc75a0-d4a3-49d9-93c1-1e303d482948

    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
    0_1489929047443_image.png
    0_1489929075370_image.png
    0_1489929101621_image.png
    0_1489929123342_image.png
    0_1489929216819_image.png
    0_1489929246220_image.png
    0_1489929290177_image.png
    0_1489929324956_image.png

    If I look at the rules I can see that they were triggered, but the clock does not want to update
    0_1489929376722_image.png

    Please would you check my implementation and let me know what I'm doing wrong
    Regards


  • ADMIN

    @esawyja

    0_1489929047443_image.png
    Please would you check my implementation and let me know what I'm doing wrong

    Use Geyser Status NOT Watt



  • 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


  • ADMIN

    @esawyja

    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



5
Online

309
Users

206
Topics

1216
Posts

Looks like your connection to MYCONTROLLER.ORG was lost, please wait while we try to reconnect.