Announcement

Collapse
No announcement yet.

Script to change a virtual device based on temperature range

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

    Script to change a virtual device based on temperature range

    I am pulling hourly temperatures for a set period and wanting to change a device's value based on the lowest value in that period. I thought I would run an event to trigger the script whenever the hourly temps updated.

    Since my VB skills are pretty rudimentary, I thought I would put my code on here for feedback to both a) see if it will even work and b) see if there is a better way to accomplish this.

    I'd appreciate any feedback y'all want to give.

    Code:
    Sub Temps(parms As Object)
    
    ' Get Hourly Temps from Devices
    
    Dim Temp0, Temp1, Temp2, Temp3, Temp4, Temp5, Temp6, Temp7, Temp8, Temp9, Temp10 as Integer
    
    Temp0 = hs.DeviceValueEx(889)
    Temp1 = hs.DeviceValueEx(894)
    Temp2 = hs.DeviceValueEx(38)
    Temp3 = hs.DeviceValueEx(904)
    Temp4 = hs.DeviceValueEx(909)
    Temp5 = hs.DeviceValueEx(914)
    Temp6 = hs.DeviceValueEx(919)
    Temp7 = hs.DeviceValueEx(924)
    Temp8 = hs.DeviceValueEx(929)
    Temp9 = hs.DeviceValueEx(935)
    Temp10 = hs.DeviceValueEx(939)
    
    ' Put Hourly Temps into Array
    Dim Temps(11) {Temp0, Temp1, Temp2, Temp3, Temp4, Temp5, Temp6, Temp7, Temp8, Temp9, Temp10}
    
    ' Find minimum temp
    
    Dim Temp_min as Integer
    Temp_min = 100
    
    For i = 0 to 11
    If Temp_min > Temps(i) Then
    Temp_min = Temps(i)
    End If
    Next
    
    ' Set virtual device value based on min temp
    
    If Temp_min < 0 Then
    hs.SetDeviceValueByRef(885, 10, False)
    ElseIf Temp_min < 10 Then
    hs.SetDeviceValueByRef(885, 11, False)
    ElseIf Temp_min < 20 Then
    hs.SetDeviceValueByRef(885, 12, False)
    ElseIf Temp_min < 30 Then
    hs.SetDeviceValueByRef(885, 13, False)
    ElseIf Temp_min < 40 Then
    hs.SetDeviceValueByRef(885, 14, False)
    ElseIf Temp_min < 50 Then
    hs.SetDeviceValueByRef(885, 15, False)
    Else
    hs.SetDeviceValueByRef(885, 0, False)
    End If
    
    End Sub

    #2
    i think you are headed in the right direction. There is a vb min function to simply your code a bit. see the following example

    https://www.dotnetperls.com/math-max-min-vbnet

    at the top, instead of getting all 11 temp values and saving to variables, you might just create an array of the dv.ref ids and then use the DeviceValueEx function to get the temp value in your loop. something like the following

    untested

    Code:
    dim temprefs as integer() = {889, ...}
    
    mintemp = 1000
    
    for each ref in temprefs
    
    mintemp = math.min(mintemp, hs.DeviceValueEx(ref))
    
    next
    Mark

    HS3 Pro 4.2.19.5
    Hardware: Insteon Serial PLM | AD2USB for Vista Alarm | HAI Omnistat2 | 1-Wire HA7E | RFXrec433 | Dahua Cameras | LiftMaster Internet Gateway | Tuya Smart Plugs
    Plugins: Insteon (mine) | Vista Alarm (mine) | Omnistat 3 | Ultra1Wire3 | RFXCOM | HS MyQ | BLRadar | BLDenon | Tuya | Jon00 Charting | Jon00 Links
    Platform: Windows Server 2022 Standard, i5-12600K/3.7GHz/10 core, 16GB RAM, 500GB SSD

    Comment


      #3
      I've cleaned up the code a bit and tried to run the script. When I run the code now, I'm getting the following error...

      Running script, script run or compile error in file: VB_temp_check.txt1025:Expected end of statement in line 4 More info: Expected end of statement

      New code:

      Code:
      Sub Temps()
      
      ' Get Hourly Temps from Devices
      Dim Temp_Array as Integer() = {889, 894, 899, 904, 909, 914, 919, 924, 929, 935, 939}
      
      ' Find minimum temp
      
      Dim Temp_min as Integer
      Temp_min = 1000
      
      For Each ref in Temp_Array
      Temp_min = = math.min(mintemp, hs.DeviceValueEx(ref))
      Next
      
      ' Set virtual device value based on min temp
      
      If Temp_min < 0 Then
      hs.SetDeviceValueByRef(943, 10, False)
      ElseIf Temp_min < 10 Then
      hs.SetDeviceValueByRef(943, 11, False)
      ElseIf Temp_min < 20 Then
      hs.SetDeviceValueByRef(943, 12, False)
      ElseIf Temp_min < 30 Then
      hs.SetDeviceValueByRef(943, 13, False)
      ElseIf Temp_min < 40 Then
      hs.SetDeviceValueByRef(943, 14, False)
      ElseIf Temp_min < 50 Then
      hs.SetDeviceValueByRef(943, 15, False)
      Else
      hs.SetDeviceValueByRef(943, 0, False)
      End If
      
      End Sub
      I've also tried

      Code:
      Dim Temp_Array() as Integer = {889, 894, 899, 904, 909, 914, 919, 924, 929, 935, 939}
      and

      Code:
      Dim Temp_Array as Integer()
      Temp_Array = New Integer () {889, 894, 899, 904, 909, 914, 919, 924, 929, 935, 939}

      Comment


        #4
        a few syntax and logic issues

        Code:
        For Each ref as integer in Temp_Array
          Temp_min =  math.min(Temp_min, hs.DeviceValueEx(ref))
        Next

        i tested the following and it works for me

        Code:
         Sub Main(parm As Object)
        
        Dim Temp_Array As Integer() = {889, 894, 899, 904, 909, 914, 919, 924, 929, 935, 939}
        
        Dim Temp_min As Integer = 1000
        
        For each dvref as integer in Temp_Array
        
        Temp_min = Math.Min(Temp_min, hs.DeviceValueEx(dvref))
        
        Next
        
        hs.writelog("test", temp_min)
        
        End Sub
        Mark

        HS3 Pro 4.2.19.5
        Hardware: Insteon Serial PLM | AD2USB for Vista Alarm | HAI Omnistat2 | 1-Wire HA7E | RFXrec433 | Dahua Cameras | LiftMaster Internet Gateway | Tuya Smart Plugs
        Plugins: Insteon (mine) | Vista Alarm (mine) | Omnistat 3 | Ultra1Wire3 | RFXCOM | HS MyQ | BLRadar | BLDenon | Tuya | Jon00 Charting | Jon00 Links
        Platform: Windows Server 2022 Standard, i5-12600K/3.7GHz/10 core, 16GB RAM, 500GB SSD

        Comment


          #5

          Temp_min = = math.min(mintemp, hs.DeviceValueEx(ref))



          should be
          Temp_min = math.min(Temp_min, hs.DeviceValueEx(ref))

          2 equals sign and wrong variable name?
          tenholde

          Comment


            #6
            Originally posted by mnsandler View Post
            a few syntax and logic issues

            Code:
            For Each ref as integer in Temp_Array
            Temp_min = math.min(Temp_min, hs.DeviceValueEx(ref))
            Next
            Thanks. I should probably slow down a bit.


            i tested the following and it works for me

            Code:
             Sub Main(parm As Object)
            
            Dim Temp_Array As Integer() = {889, 894, 899, 904, 909, 914, 919, 924, 929, 935, 939}
            
            Dim Temp_min As Integer = 1000
            
            For each dvref as integer in Temp_Array
            
            Temp_min = Math.Min(Temp_min, hs.DeviceValueEx(dvref))
            
            Next
            
            hs.writelog("test", temp_min)
            
            End Sub
            I'm still getting errors. I even did a copy/paste of the code you provided and got this:

            Running script, script run or compile error in file: VB_temp_check.txt1006:Expected ')' in line 1 More info: Expected ')'

            I know this should be a pretty simple script. I'd love to figure this out because this is the first time I'm calling one on HS, and I figure it might open up a whole 'nother world of home automation for me.

            Comment


              #7
              What is the extension of your script file? .vb or .txt ?
              tenholde

              Comment


                #8
                .txt

                Comment


                  #9
                  My code should be in a .vb file. Looks like yours is .txt
                  Mark

                  HS3 Pro 4.2.19.5
                  Hardware: Insteon Serial PLM | AD2USB for Vista Alarm | HAI Omnistat2 | 1-Wire HA7E | RFXrec433 | Dahua Cameras | LiftMaster Internet Gateway | Tuya Smart Plugs
                  Plugins: Insteon (mine) | Vista Alarm (mine) | Omnistat 3 | Ultra1Wire3 | RFXCOM | HS MyQ | BLRadar | BLDenon | Tuya | Jon00 Charting | Jon00 Links
                  Platform: Windows Server 2022 Standard, i5-12600K/3.7GHz/10 core, 16GB RAM, 500GB SSD

                  Comment


                    #10
                    How are you starting/ running your script?
                    Mark

                    HS3 Pro 4.2.19.5
                    Hardware: Insteon Serial PLM | AD2USB for Vista Alarm | HAI Omnistat2 | 1-Wire HA7E | RFXrec433 | Dahua Cameras | LiftMaster Internet Gateway | Tuya Smart Plugs
                    Plugins: Insteon (mine) | Vista Alarm (mine) | Omnistat 3 | Ultra1Wire3 | RFXCOM | HS MyQ | BLRadar | BLDenon | Tuya | Jon00 Charting | Jon00 Links
                    Platform: Windows Server 2022 Standard, i5-12600K/3.7GHz/10 core, 16GB RAM, 500GB SSD

                    Comment


                      #11
                      Originally posted by rsc2a View Post
                      .txt
                      change it to .vb
                      tenholde

                      Comment


                        #12
                        Changed it to .vb and that fixed it. Thanks a ton!

                        To answer the last question, I'm running an event that triggers whenever the hour changes and is set to fire off the script.

                        Comment

                        Working...
                        X