• Categories
  • Recent
  • Tags
  • Popular
  • Register
  • Login
  • Categories
  • Recent
  • Tags
  • Popular
  • Register
  • Login

Hour Meter

Scheduled Pinned Locked Moved Dashboard
custom widgetscriptjavascripthour metergenerator
11 Posts 3 Posters 4.9k Views 2 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    esawyja
    last edited by jkandasa 14 Mar 2017, 07:10

    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

    J 1 Reply Last reply 15 Mar 2017, 06:44 Reply Quote 1
    • J Offline
      jkandasa @esawyja
      last edited by jkandasa 15 Mar 2017, 06:44

      @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

      E 1 Reply Last reply 15 Mar 2017, 06:47 Reply Quote 1
      • E Offline
        esawyja @jkandasa
        last edited by 15 Mar 2017, 06:47

        @jkandasa BRILLIANT, thank you soooo much!!, I will test this out over the weekend and give some feedback!

        1 Reply Last reply Reply Quote 1
        • E Offline
          esawyja
          last edited by 19 Mar 2017, 13:17

          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

          J 1 Reply Last reply 20 Mar 2017, 04:22 Reply Quote 0
          • J Offline
            jkandasa @esawyja
            last edited by 20 Mar 2017, 04:22

            @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

            1 Reply Last reply Reply Quote 0
            • E Offline
              esawyja
              last edited by 20 Mar 2017, 04:27

              Hi
              Thank you, Let me try that, I just thought that detecting the watts used will be a good enough trigger
              Regards

              1 Reply Last reply Reply Quote 0
              • E Offline
                esawyja
                last edited by 20 Mar 2017, 04:37

                @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

                J 1 Reply Last reply 20 Mar 2017, 12:12 Reply Quote 0
                • J Offline
                  jkandasa @esawyja
                  last edited by 20 Mar 2017, 12:12

                  @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.

                  1 Reply Last reply Reply Quote 0
                  • E Offline
                    esawyja
                    last edited by 20 Mar 2017, 12:29

                    Thanks so much

                    1 Reply Last reply Reply Quote 0
                    • D Offline
                      Daniele
                      last edited by 2 Dec 2017, 15:12

                      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?

                      J 1 Reply Last reply 5 Dec 2017, 12:37 Reply Quote 0
                      • J Offline
                        jkandasa @Daniele
                        last edited by 5 Dec 2017, 12:37

                        @daniele You may need to create a dummy sensor and update sensor value everyday night via a script.

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post

                        0

                        Online

                        618

                        Users

                        531

                        Topics

                        3.4k

                        Posts
                        Copyright © 2015-2025 MyController.org | Contributors | Localization