Announcement

Collapse
No announcement yet.

Calculate time between device updates

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calculate time between device updates

    Attached Files

  • #2
    I haven't tested this, but I think it should work:

    Code:
            Dim devRefNow As Integer = DEV_REF_TO_THE_CURRENT_KWH
            Dim devRefLast As Integer = DEV_REF_TO_THE_PREVIOUS_KWH
    
            Dim dateNow As Date = hs.DeviceLastChangeRef(devRefNow)
            Dim dateLast As Date = hs.DeviceLastChangeRef(devRefLast)
            Dim valueNow As Double = hs.DeviceValueEx(devRefNow)
            Dim valueLast As Double = hs.DeviceValueEx(devRefLast)
    
            Dim watts As Double = 1000 * (valueNow - valueLast) / dateNow.Subtract(dateLast).TotalHours
            hs.SetDeviceValueByRef(DEV_REF_TO_WATTS_DEVICE, watts, True)
    HSPro 3.0.0.458, Z-NET with Z-wave plugin 3.0.1.190, RFXCOM + 2x RFXtrx433E, HSTouch, Squeezebox plugin, iTach IP/WF2IR & GC-100-6 with UltraGCIR, BLDenon, NetcamStudio, Jon00s Webpage builder, Harmony Hub plugin, SCSIP (with FreePBX), Arduino plugin, IFTTT, Pushalot plugin, Device History plugin.
    Running on Windows 10 (64) virtualized
    on ESXi (Fujitsu Primergy TX150 S8).
    WinSeer (for Win10) - TextSeer - FitbitSeer - HSPI_MoskusSample

    Are you Norwegian (or Scandinavian) and getting started with HomeSeer? Read the "HomeSeer School"!

    Comment


    • #3
      The last line in the code suggested by Moskus has me confused, but I think he's captured the essence. You will need two devices, one for the current value and one for the previous value. Both actually store the last kWH value most of the time, but when the value updates, only the current device has its value updated. Then you would run a script to calculate the difference between the two device values and their last update times. Once that calculation is complete, the script would set the 'previous' device to be the same as the 'current' device.
      Mike____________________________________________________________ __________________
      HS3 Pro Edition 3.0.0.548

      HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF, Rain8Net+ | RFXCOM | QSE100D | Vantage Pro | Green-Eye | X10: XTB-232, -IIR | Edgeport/8 | Way2Call | Ecobee3

      Comment


      • #4
        I'm not sure what you are saying, Uncle. To me it seems like he has all he needs.
        HSPro 3.0.0.458, Z-NET with Z-wave plugin 3.0.1.190, RFXCOM + 2x RFXtrx433E, HSTouch, Squeezebox plugin, iTach IP/WF2IR & GC-100-6 with UltraGCIR, BLDenon, NetcamStudio, Jon00s Webpage builder, Harmony Hub plugin, SCSIP (with FreePBX), Arduino plugin, IFTTT, Pushalot plugin, Device History plugin.
        Running on Windows 10 (64) virtualized
        on ESXi (Fujitsu Primergy TX150 S8).
        WinSeer (for Win10) - TextSeer - FitbitSeer - HSPI_MoskusSample

        Are you Norwegian (or Scandinavian) and getting started with HomeSeer? Read the "HomeSeer School"!

        Comment


        • #5
          Originally posted by Moskus View Post
          I'm not sure what you are saying, Uncle. To me it seems like he has all he needs.
          I agree. I realize now that the last line is assigning the result of the watts calculation to another device value.

          What I was suggesting is that the script could also copy the device value and last change time from the "now" device to the "previous" device to prepare for the next calculation.
          Mike____________________________________________________________ __________________
          HS3 Pro Edition 3.0.0.548

          HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF, Rain8Net+ | RFXCOM | QSE100D | Vantage Pro | Green-Eye | X10: XTB-232, -IIR | Edgeport/8 | Way2Call | Ecobee3

          Comment


          • #6
            Originally posted by Moskus View Post
            I haven't tested this, but I think it should work:

            Code:
                    Dim devRefNow As Integer = DEV_REF_TO_THE_CURRENT_KWH
                    Dim devRefLast As Integer = DEV_REF_TO_THE_PREVIOUS_KWH
            
                    Dim dateNow As Date = hs.DeviceLastChangeRef(devRefNow)
                    Dim dateLast As Date = hs.DeviceLastChangeRef(devRefLast)
                    Dim valueNow As Double = hs.DeviceValueEx(devRefNow)
                    Dim valueLast As Double = hs.DeviceValueEx(devRefLast)
            
                    Dim watts As Double = 1000 * (valueNow - valueLast) / dateNow.Subtract(dateLast).TotalHours
                    hs.SetDeviceValueByRef(DEV_REF_TO_WATTS_DEVICE, watts, True)
            Thanks a lot for this. I got it exactly like I want it now. I've also added a daily high and a daily low (I need to write a script that resets the values at midnight).

            Regarding the Previous kWh (kWhPrev) I update the value to the latest but let the string keep the previous value. In this way it's easy to see the previous kwH value in the GUI.

            Below is the code I ended up with.

            Code:
            Sub Main(params as Object)
            
               Dim devRefNow as Integer = 680
               Dim devRefPrev as Integer = 684
               Dim devRefWatts as Integer = 685
               Dim devRefWattsDayHi as Integer = 686
               Dim devRefWattsDayLo as Integer = 687
               
               Dim dateNow As Date = hs.DeviceLastChangeRef(devRefNow)
               Dim datePrev As Date = hs.DeviceLastChangeRef(devRefPrev)
               Dim kWhNow as Double = hs.DeviceValueEx(devRefNow)
               Dim kWhPrev As Double = hs.DeviceValueEx(devrefPrev)
               Dim wattsDayHi As Integer = hs.DeviceValueEx(devRefWattsDayHi)
               Dim wattsDayLo As Integer = hs.DeviceValueEx(devRefWattsDayLo)
               
               Dim watts As Integer = Math.Round(1000 * (kWhNow - kWhPrev) / dateNow.Subtract(datePrev).TotalHours, 0)
            
               hs.writelog("NorthQ","NorthQ VP - Current kWh: " & kWhNow & " Previous kWh: " & kWhPrev & " Current W: " & watts)
            
               hs.SetDeviceValueByRef(devRefWatts, watts, True)
               hs.SetDeviceString(devRefWatts, watts & " W", False)
               hs.SetDeviceValueByRef(devRefPrev, kWhNow, True)
               hs.SetDeviceString(devRefPrev, kWhPrev & " kWh", False)
            
               if wattsDayHi < watts then
                  hs.SetDeviceValueByRef(devRefWattsDayHi, watts, True)
                  hs.SetDeviceString(devRefWattsDayHi, watts & " W", False)
               End if
            
               if wattsDayLo > watts then
                  hs.SetDeviceValueByRef(devRefWattsDayLo, watts, True)
                  hs.SetDeviceString(devRefWattsDayLo, watts & " W", False)
               End if
            
            End Sub
            Attached Files

            Comment


            • #7
              How did you set the virtual device that stores the previous kWh value and also updates the "Last Change" value?
              I have the same power reader as you and is really struggeling to set something up so that I can see "daily usage"?
              I have spend hours on the forum, but as a newbie it is really hard for me to connect the dots...

              Comment


              • #8
                These are the rows that updates the "Previous kWh" virtual device:

                hs.SetDeviceValueByRef(devRefPrev, kWhNow, True)
                hs.SetDeviceString(devRefPrev, kWhPrev & " kWh", False)

                The first row updates the VALUE of the device with the current kWh. This will be the previous value once NorthQ sends a new report and is used to calculate the amount of kWh since last update.

                The second row updates the DEVICE STRING of the device. This is for display only in the web gui as I like to know what the actual last kWh reading was.

                By setting the third argument to "True" the "Last change" value is updated.

                Hope that makes some sense.

                Comment


                • #9
                  Thank you very much!
                  I got the first part to work now.
                  My problem was not being able to pass the value to the virtual device without a error message in the log.
                  With your help I figured out that I needed to declare the value as Double, so that that I was able to use something else than whole numbers.
                  I learn something new every day here
                  Code:
                  Sub Main(params as Object)
                     Dim devRefNow as Integer = 132
                     Dim devRefPrev as Integer = 133
                     Dim kWhNow as Double = hs.DeviceValueEx(devRefNow)
                     Dim kWhPrev As Double = hs.DeviceValueEx(devrefPrev)
                  hs.SetDeviceValueByRef(devRefPrev, kWhNow, True)
                  hs.SetDeviceString(devRefPrev, kWhPrev & " kWh", False)
                  End Sub

                  Comment


                  • #10
                    Glad to hear you got it working.

                    Comment


                    • #11
                      can you plz explain how you did make that virtual device. its some time since i did do this and a got som trouble to make this work.

                      Comment

                      Working...
                      X