Announcement

Collapse
No announcement yet.

How can I add values of a device every time they change ?

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

    How can I add values of a device every time they change ?

    I have a device showing kW Hours. This resets sometimes which makes it useless for my application. So I have to create a script, which grabs the value, and then when it goes to 0 and starts counting again adds it to the value it had before it has been zeroed again. (I am thinking maybe take the difference every time the value changes and add it to a virtual device. Would that work ?)
    I will need the daily total kW Hour number at 21.55 every day for another calculation. At or after midnight I want to reset it to 0 again, and start the process of adding up again.

    Not easy I think but I hope that can be done ?

    #2
    How often does the value update?

    I'm thinking you could create two virtual devices. One would track the kW hour device, the other would capture the cumulative value. Your script would run every time the kW hour device value changes.

    If the new kW hour value is greater than the value of the tracking virtual device, then that difference is added to the value of the cumulative virtual device. If the kW hour value is less than the tracking virtual device, then assume the kW hour device has reset to zero and just add the value of the kW hour device to the cumulative virtual device. In either case, assign the new value of the kW hour device to the tracking device before exiting the script.

    Reset the cumulative device to zero at the appropriate time, either with the script or a separate event.
    Mike____________________________________________________________ __________________
    HS3 Pro Edition 3.0.0.548, NUC i3

    HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF | RFXCOM | Vantage Pro | Green-Eye | Edgeport/8 | Way2Call | Ecobee3 | EtherRain | Ubiquiti

    Comment


      #3
      Yes I think that would work. When I have time tomorrow I'll have a go try to put that into a script. If it doesn't work or if I get stuck I'll be back for more help...

      The updates are linked to my PV panel production, so they can update a couple of times a minute when the sun is shining

      Comment


        #4
        Originally posted by Uncle Michael View Post
        How often does the value update?

        I'm thinking you could create two virtual devices. One would track the kW hour device, the other would capture the cumulative value. Your script would run every time the kW hour device value changes.

        If the new kW hour value is greater than the value of the tracking virtual device, then that difference is added to the value of the cumulative virtual device. If the kW hour value is less than the tracking virtual device, then assume the kW hour device has reset to zero and just add the value of the kW hour device to the cumulative virtual device. In either case, assign the new value of the kW hour device to the tracking device before exiting the script.

        Reset the cumulative device to zero at the appropriate time, either with the script or a separate event.
        Ok I think using your idea I have a solution. Instead of saving in a virtual device, I am writing this to an ini file I use to track other variables too. I am setting a virtual device as well for display purposes (ref 966).
        This is what I have come up with:

        Code:
        Const IniFile As String = "MyVariables.ini"
        
        Sub Main(ByVal Parms As Object)
        
                Dim HWkWH As String
                HWkWH = hs.DeviceValueEx(960)
                Dim HWcumulative = hs.GetINISetting("MyVariables", "HWcumulative", "999", IniFile)
        
        
              If HWkWH > HWcumulative
           
                 HWcumulative = (HWkWH - HWcumulative) + HWcumulative
        
              End If
        
             hs.SaveINISetting("MyVariables", "HWcumulative",HWcumulative, IniFile)
             hs.SetDeviceString(966,HWcumulative,True)
             hs.SetDeviceValuebyref(966,HWcumulative,True)
        
            End Sub
        Last edited by mikee123; April 15, 2017, 04:37 AM.

        Comment


          #5
          I may be missing something in your script, but I think you need to track two numbers (using an ini file is a good method). This line:
          HWcumulative = (HWkWH - HWcumulative) + HWcumulative
          is equivalent to this, I think: HWcumulative = HWkWH
          That is, it looks like your ini value will always reflect the kW hour device.

          I do not understand how you handle the case when your kW hour value is reset to zero. I think you need to track both the value of the kW hour device and the cumulative separately so that when the device resets, you do not lose the previous cumulative value.
          Mike____________________________________________________________ __________________
          HS3 Pro Edition 3.0.0.548, NUC i3

          HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF | RFXCOM | Vantage Pro | Green-Eye | Edgeport/8 | Way2Call | Ecobee3 | EtherRain | Ubiquiti

          Comment


            #6
            I see what you mean. Ok back to the drawing board. Have to rethink my logic.

            Comment


              #7
              I might have it

              Code:
              Const IniFile As String = "MyVariables.ini"
              
              Sub Main(ByVal Parms As Object)
              
              
                      Dim HWkWH As String
                      HWkWH = hs.DeviceValueEx(976)
                      Dim HWcumulative = hs.GetINISetting("MyVariables", "HWcumulative", "999", IniFile)
              
              
                    If HWkWH > HWcumulative
                 
                       HWcumulative = HWkWH
              
                    End If
              
                    If HWkWH < HWcumulative
                 
                       HWcumulative = HWcumulative + HWkWH
              
                    End If
              
              
              
                   hs.SaveINISetting("MyVariables", "HWcumulative",HWcumulative, IniFile)
                   hs.SetDeviceString(966,HWcumulative,True)
                   hs.SetDeviceValuebyref(966,HWcumulative,True)
              
              
              End Sub

              Comment


                #8
                I think this segment will result in compounding the results:
                If HWkWH < HWcumulative
                HWcumulative = HWcumulative + HWkWH
                End If
                To get the result you want, you will need to store the total before the reset in a separate variable (I'll call it HWstore) that does not change each time the script runs. To do that, I'd suggest that you initially assign HWstore a value of zero, then when the kW hour value resets, transfer the previous vale of HWcumulative to HWstore.

                Then, your computation would look like:

                HWcumulative = HWstore + HWkWH (nice palindrome, by the way)

                A problem still remains, though. How will the script know that the kW hour value has reset? Because, once it resets, HWkWH will always be less than HWcumulative. The only way I can see is to store the previous value of HWkWH and compare it to the latest value each time the script runs. If you do that, then I think that difference can simply be added to HWcumulative and you won't need HWstore. You'll only need a special case for calculating the difference when the value resets, which I think is to assign the value of zero to the previous value of HWkWH.

                So, in pseudo code, something like this:

                Code:
                Retrieve HWkWHold and HWcumulative from the ini file
                
                If HWkWH < HWkWHold Then HWkWHold = 0
                
                HWcumulative = HWcumulative + HWkWH - HWkWHold
                
                HWkWHold = HWkWH
                
                store HWkWHold and HWcumulative back into the ini file
                Mike____________________________________________________________ __________________
                HS3 Pro Edition 3.0.0.548, NUC i3

                HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF | RFXCOM | Vantage Pro | Green-Eye | Edgeport/8 | Way2Call | Ecobee3 | EtherRain | Ubiquiti

                Comment


                  #9
                  Cant quite get my head around this. Below should work, until after a reset. The first reset still works, but then it wont work.

                  Code:
                  Const IniFile As String = "MyVariables.ini"
                  
                  Sub Main(ByVal Parms As Object)
                  
                  
                          Dim HWkWH As String
                          Dim HWstore As String
                          Dim HWkWHold As String
                          HWkWH = hs.DeviceValueEx(976)
                          Dim HWcumulative = hs.GetINISetting("MyVariables", "HWcumulative", "999", IniFile)
                          Dim HWkWHold = hs.GetINISetting("MyVariables", "HWkWHold", "999", IniFile)
                  
                  
                        If HWkWH < HWkWHold
                     
                           HWcumulative = HWcumulative + HWkWH
                           HWkWHold = HWkWH
                  
                        Else
                  
                           HWcumulative = HWkWH
                           HWkWHold = HWcumulative
                  
                        End If
                  
                  
                       hs.SaveINISetting("MyVariables", "HWcumulative",HWcumulative, IniFile)
                       hs.SaveINISetting("MyVariables", "HWkWHold",HWkWHold, IniFile)
                       hs.SetDeviceString(966,HWcumulative,True)
                       hs.SetDeviceValuebyref(966,HWcumulative,True)
                  
                  
                  End Sub

                  Comment


                    #10
                    Originally posted by mikee123 View Post
                    Cant quite get my head around this. Below should work, until after a reset. The first reset still works, but then it wont work.
                    Two observations:

                    1. HWcumulative = HWcumulative + HWkWH will only work once - when the value has just reset. After that, you will be compounding the accounting of new power use. You need to add the incremental kWH (HWkWH - HWkWHold, where HWkWHold is the value of HWkWH on the previous run). HWkWH is the total accumulated since the last reset.

                    2. If you set HWkWHold = HWcumulative after a reset, then HWkWH < HWkWHold will be true again. So it looks like you will be jumping between the two alternatives in your If statement every other time the script is called after a reset.
                    Mike____________________________________________________________ __________________
                    HS3 Pro Edition 3.0.0.548, NUC i3

                    HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF | RFXCOM | Vantage Pro | Green-Eye | Edgeport/8 | Way2Call | Ecobee3 | EtherRain | Ubiquiti

                    Comment


                      #11
                      Exactly. I noticed that but couldnt get my head around it. Now its getting late and I havent got the concentration any more. Maybe tomorrow.

                      Comment


                        #12
                        Try something like this:
                        Code:
                        Const IniFile As String = "MyVariables.ini"
                        
                        Sub Main(ByVal Parms As Object)
                        
                                Dim HWkWH As Double   ' I don't understand why this is defined as a string, when you are assigning a number to it.  I'm surprised you aren't getting an error.
                                HWkWH = hs.DeviceValueEx(976)
                                Dim HWcumulative As Double = hs.DeviceValueEx(966)
                                Dim HWkWHold = hs.GetINISetting("MyVariables", "HWkWHold", "999", IniFile)
                        
                            If HWkWH < HWkWHold Then HWkWHold = 0     'Assume the value reset to zero
                            HWcumulative = HWcumulative + HWkWH - HWkWHold
                            HWkWHold = HWkWH
                        
                             hs.SaveINISetting("MyVariables", "HWkWHold", HWkWHold, IniFile)
                             hs.SetDeviceString(966,CStr(HWcumulative),True)
                             hs.SetDeviceValuebyref(966,HWcumulative,True)    
                        
                        End Sub
                        Mike____________________________________________________________ __________________
                        HS3 Pro Edition 3.0.0.548, NUC i3

                        HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF | RFXCOM | Vantage Pro | Green-Eye | Edgeport/8 | Way2Call | Ecobee3 | EtherRain | Ubiquiti

                        Comment

                        Working...
                        X