Announcement

Collapse
No announcement yet.

Multiple if in a script ?

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

    Multiple if in a script ?

    Hi

    i created a script but nothing happens,
    if i use only one it works but i need more than 20




    i want like more if commands in my script,


    Code:
    Sub Main(ByVal parm as Object) 
    If (hs.devicevalue(2556) = 0) Then
    hs.TriggerEvent ("set 1")
    If (hs.devicevalue(2556) = 1) Then
    hs.TriggerEvent ("set 2")
    else 
    hs.WriteLog("AV", "Device Not Selected") 
    End If 
    End If 
    End Sub
    In my other script i have multiple IF statements and only 1 End IF
    like this but i cant get it to work , when i execute nothing happens no error or nothing

    Code:
    If hs.DeviceStringByName ("test").contains ("test1") Then hs.blabla
    If hs.DeviceStringByName ("test").contains ("test2") Then hs.blabla
    If hs.DeviceStringByName ("test").contains ("test3") Then hs.blabla
    If hs.DeviceStringByName ("test").contains ("test4") Then hs.blabla
    If hs.DeviceStringByName ("test").contains ("test5") Then hs.blabla 
    if hs.DeviceStringByName ("test").contains ("test6") Then hs.blabla
    End If



    regards
    Last edited by Malosa; July 14, 2016, 12:10 PM.
    Preferred -> Jon's Plugins, Pushover, Phlocation, Easy-trigger,
    Rfxcom, Blade Plugins, Pushbullet, homekit, Malosa Scripts




    HS3Pro 4.1.14.0 on windows 10 enterprise X64 on hp quadcore laptop 8 GB.

    #2
    For that many conditions, it may be best to use the Select-Case statement: https://msdn.microsoft.com/en-us/library/cy37t14y.aspx.

    Cheers
    Al
    HS 4.2.8.0: 2134 Devices 1252 Events
    Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

    Comment


      #3
      If you want to use If statements, structure it like this:

      Code:
      Sub Main(ByVal parm as Object) 
        If hs.devicevalue(2556) = 0 Then
           hs.TriggerEvent ("set 1")
        ElseIf hs.devicevalue(2556) = 1 Then
           hs.TriggerEvent ("set 2")
        Else 
           hs.WriteLog("AV", "Device Not Selected") 
        End If 
      End Sub
      Cheers
      Al
      HS 4.2.8.0: 2134 Devices 1252 Events
      Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

      Comment


        #4
        Thanks Al

        this one works , but is it possible to use to trigger on specific device status text? like when 1=On

        Like this example but hs.devicestatus doesnt work..
        if hs.DeviceStatus ("2556").contains ("On") Then hs.TriggerEvent "test 1"


        Originally posted by sparkman View Post
        If you want to use If statements, structure it like this:

        Code:
        Sub Main(ByVal parm as Object) 
          If hs.devicevalue(2556) = 0 Then
             hs.TriggerEvent ("set 1")
          ElseIf hs.devicevalue(2556) = 1 Then
             hs.TriggerEvent ("set 2")
          Else 
             hs.WriteLog("AV", "Device Not Selected") 
          End If 
        End Sub
        Cheers
        Al
        Preferred -> Jon's Plugins, Pushover, Phlocation, Easy-trigger,
        Rfxcom, Blade Plugins, Pushbullet, homekit, Malosa Scripts




        HS3Pro 4.1.14.0 on windows 10 enterprise X64 on hp quadcore laptop 8 GB.

        Comment


          #5
          Yes, it's possible, but there's no command called hs.devicestatus. Use CAPIGetStatus intead: http://homeseer.com/support/homeseer...igetstatus.htm.

          Cheers
          Al
          HS 4.2.8.0: 2134 Devices 1252 Events
          Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

          Comment


            #6
            I have taken your script as a blueprint and tried to adapt it for my needs, but get errors:

            Jul-15 08:27:45 Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\HeatingControl.vb: 'ElseIf' must be preceded by a matching 'If' or 'ElseIf'. Jul-15 08:27:45 Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\HeatingControl.vb: Namespace or type specified in the Imports 'System.Core' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.




            Sub Main(ByVal parm as Object)
            If hs.devicevalue(224) = 14 Then hs.TriggerEvent ("Heat 14")
            ElseIf hs.devicevalue(224) = 18 Then hs.TriggerEvent ("Heat 18")
            ElseIf hs.devicevalue(224) = 20 Then hs.TriggerEvent ("Heat 20")
            ElseIf hs.devicevalue(224) = 18 Then hs.TriggerEvent ("Heat 21")
            End If
            End Sub

            Comment


              #7
              Originally posted by mikee123 View Post
              I have taken your script as a blueprint and tried to adapt it for my needs, but get errors:

              Jul-15 08:27:45 Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\HeatingControl.vb: 'ElseIf' must be preceded by a matching 'If' or 'ElseIf'. Jul-15 08:27:45 Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\HeatingControl.vb: Namespace or type specified in the Imports 'System.Core' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.




              Sub Main(ByVal parm as Object)
              If hs.devicevalue(224) = 14 Then hs.TriggerEvent ("Heat 14")
              ElseIf hs.devicevalue(224) = 18 Then hs.TriggerEvent ("Heat 18")
              ElseIf hs.devicevalue(224) = 20 Then hs.TriggerEvent ("Heat 20")
              ElseIf hs.devicevalue(224) = 18 Then hs.TriggerEvent ("Heat 21")
              End If
              End Sub
              It needs to be structured as I posted above. Anything after Then needs to be on a new line. For an If statement, if the action is on the same line, it considers it a complete If Then End If statement (the End If is implied at the end of the line), so when it sees the first ElseIf, it's not matched with your first If and that's why you get that error. See https://msdn.microsoft.com/en-us/library/752y8abs.aspx for details.

              Cheers
              Al
              HS 4.2.8.0: 2134 Devices 1252 Events
              Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

              Comment


                #8
                I think that your syntax for devicevalue isn't correct
                This is how i do it

                Code:
                 Imports System.Threading
                  
                 Sub Main(byVal params As Object)
                  
                   'Device indentifier-DeviceCode as found on properties screen of the device 
                   Dim DEVICEID As String = TEST-X1" 
                   Dim dvRef
                  
                   'Get device reference
                   dvRef = hs.GetDeviceRef(DEVICEID)
                  
                   Select Case hs.devicevalue(dvRef)
                     Case 18
                       hs.TriggerEvent ("Heat 18") 
                     Case 20
                       hs.TriggerEvent ("Heat 20") 
                     Case 21
                       hs.TriggerEvent ("Heat 21") 
                     Case Else 'Optional
                        ' Do something else when none found
                   End Select
                  
                 End Sub
                - Bram

                Send from my Commodore VIC-20

                Ashai_Rey____________________________________________________________ ________________
                HS3 Pro 3.0.0.534
                PIugins: ZMC audio | ZMC VR | ZMC IR | ZMC NDS | RFXcom | AZ scripts | Jon00 Scripts | BLBackup | FritzBox | Z-Wave | mcsMQTT | AK Ikea

                Comment


                  #9
                  Sub Main(ByVal parm as Object)

                  If hs.devicevalue(224) = 14 Then
                  hs.TriggerEvent ("Heat 14")
                  If hs.devicevalue(224) = 18 Then
                  hs.TriggerEvent ("Heat 18")
                  If hs.devicevalue(224) = 20 Then
                  hs.TriggerEvent ("Heat 20")
                  If hs.devicevalue(224) = 18 Then
                  hs.TriggerEvent ("Heat 21")

                  End Sub

                  does not work, I am still getting errors:

                  Jul-15 12:11:02 Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\HeatingControl.vb: 'If' must end with a matching 'End If'. Jul-15 12:11:02 Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\HeatingControl.vb: Namespace or type specified in the Imports 'System.Core' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.

                  might try to modify the case one from AshaiRey for me
                  is that correct:
                  Dim DEVICEID As String = TEST-X1"
                  or should it be Dim DEVICEID As String = "TEST-X1"

                  also DEVICEID is 224, my device ref ?
                  Text-X1 is my device name, correct ?

                  Comment


                    #10
                    I had a look at the vb manual, and changed my script. No errors any more, but I am not sure its triggering any event:

                    Jul-15 12:39:50 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/HeatingControl.vb Jul-15 12:39:50 Event Event Trigger "HVAC Heating control trigger" Jul-15 12:39:50 Device Control Device: First Floor HVAC Heating Temp Control to 14 (0) by/from: CAPI Control Handler


                    this is my script now:

                    Sub Main(ByVal parm as Object)

                    If hs.devicevalue(224) = 14 Then
                    hs.TriggerEvent ("Heat 14")
                    ElseIf hs.devicevalue(224) = 18 Then
                    hs.TriggerEvent ("Heat 18")
                    ElseIf hs.devicevalue(224) = 20 Then
                    hs.TriggerEvent ("Heat 20")
                    ElseIf hs.devicevalue(224) = 18 Then
                    hs.TriggerEvent ("Heat 21")
                    End If

                    End Sub


                    I found the problem, its all working now with the above script. The virtual device had value 0 as status 14, value 1 as status 18. Obviously that's why the script did not trigger anything. When I changed the value to status, so 14 to 14 etc it all started working.
                    Last edited by mikee123; July 15, 2016, 07:18 AM. Reason: solution found !

                    Comment


                      #11
                      Code:
                      Sub Main(ByVal parm as Object) 
                      
                      If hs.devicevalue(224) = 14 Then 
                      hs.TriggerEvent ("Heat 14")
                      ElseIf hs.devicevalue(224) = 18 Then 
                      hs.TriggerEvent ("Heat 18") 
                      ElseIf hs.devicevalue(224) = 20 Then 
                      hs.TriggerEvent ("Heat 20") 
                      ElseIf hs.devicevalue(224) = 18 Then 
                      hs.TriggerEvent ("Heat 21") 
                      End If
                      
                      End Sub
                      Not sure if it was intentional but you have = 18 triggering 2 different events.

                      Probably won't have a lot of impact with a small script like this but I also prefer to either declare a variable and get the value from HS once:

                      Code:
                      Dim Value As Integer
                      Value = hs.devicevalue(224)
                      If Value = 18 ....
                      ElseIf Value = 20 ....
                      End If

                      or as previously posted

                      Code:
                      Select Case hs.devicevalue(224)
                      Case 18 ....
                      Case 20 ....
                      End Select
                      Paul..

                      Comment


                        #12
                        I did not notice I have to check my script.
                        What is the advantage of declaring value rather than querying the device directly ?

                        Comment


                          #13
                          Originally posted by mikee123 View Post
                          What is the advantage of declaring value rather than querying the device directly ?
                          Generally speaking, if you are re-using the same value multiple times within your script it will be more efficient both in terms of the amount of code you have to write and that you are only asking HS for the value once.

                          Code writing:
                          If for example at a later date you change the source device ref from 224 to 123, then you will only need to modify your script in one place. Not such an issue if (as in your example) you're only using it 3 times but it can become a pain in a large script where you are using the value many times.

                          General:
                          With a function like DeviceValue() it probably doesn't gain much but if you were using a more complex function that takes more processing, then it would be a waste of resources calling the function multiple times just to get the same result.

                          There are of course cases where you may need to call the function multiple times.

                          Code:
                          Dim MyValue as Integer = hs.DeviceValue(123)
                          
                          If MyValue = 18 Then
                          hs.SetDeviceValueByRef(123, 10, True)
                          End If
                          
                          If MyValue = 10 Then
                          .....
                          End If
                          In the above, the second If would evaluate to false because it is using the value of "MyValue" which was obtained before it was modified with the first If.

                          In summary:
                          If you don't expect the value to change throughout the duration of your sub or function, then it is probably more efficient to call it once.

                          Paul..
                          Last edited by sooty; July 16, 2016, 02:35 AM.

                          Comment


                            #14
                            Ok yes that makes perfect sense now.

                            Comment


                              #15
                              Originally posted by sooty View Post
                              Generally speaking, if you are re-using the same value multiple times within your script it will be more efficient both in terms of the amount of code you have to write and that you are only asking HS for the value once.

                              Code writing:
                              If for example at a later date you change the source device ref from 224 to 123, then you will only need to modify your script in one place. Not such an issue if (as in your example) you're only using it 3 times but it can become a pain in a large script where you are using the value many times.

                              General:
                              With a function like DeviceValue() it probably doesn't gain much but if you were using a more complex function that takes more processing, then it would be a waste of resources calling the function multiple times just to get the same result.

                              There are of course cases where you may need to call the function multiple times.

                              Code:
                              Dim MyValue as Integer = hs.DeviceValue(123)
                              
                              If MyValue = 18 Then
                              hs.SetDeviceValueByRef(123, 10, True)
                              End If
                              
                              If MyValue = 10 Then
                              .....
                              End If
                              In the above, the second If would evaluate to false because it is using the value of "MyValue" which was obtained before it was modified with the first If.

                              In summary:
                              If you don't expect the value to change throughout the duration of your sub or function, then it is probably more efficient to call it once.

                              Paul..
                              I want to extend this a bit further.
                              Imagion that the processing would be slow for this examples sake.

                              The script get the value at hs.devicevalue(224) and it is 18. the event will fire but in the meantime hs.devicevalue(224) becomes 20. What will happen then?
                              So it's better to get one reading and process the result of that reading.

                              Another point.
                              Magic numbers? In 6 months do you still know which which device is 224?
                              And you need to change that device to another device could you just change 224 to another number with or is that 224 number somewhere in your code just a value you test for?

                              Your life will be much easier if you get the habit to define these at the start of the program, before main() that is.
                              Something like this

                              const DEVICEID = 224 'Use descriptive names where you can
                              const MIDLEVEL = 18
                              cosnt HIGHLEVEL = 21

                              Constants are variables that are defined and can not be changed throughout your script when running. They are safe from unaspected altering. By using all caps for the name you will make it more recognizable for yourself that they are constants. VB isn't case sensative.

                              Use it like
                              Code:
                               int Value = hs.devicevalue(DEVICEID)
                               If Value = MIDLEVEL Then 
                                    hs.TriggerEvent ("Heat 18")
                                ElseIf Value = HIGHLEVEL Then 
                                     hs.TriggerEvent ("Heat 21")
                              End If
                              However even this little piece of code can be further improved.
                              - Bram

                              Send from my Commodore VIC-20

                              Ashai_Rey____________________________________________________________ ________________
                              HS3 Pro 3.0.0.534
                              PIugins: ZMC audio | ZMC VR | ZMC IR | ZMC NDS | RFXcom | AZ scripts | Jon00 Scripts | BLBackup | FritzBox | Z-Wave | mcsMQTT | AK Ikea

                              Comment

                              Working...
                              X