Announcement

Collapse
No announcement yet.

Using wscript in vbscript (want to use Quit, and dictionaries)

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

    Using wscript in vbscript (want to use Quit, and dictionaries)

    So, I'm more of a python guy than a vbscripter. Two seemingly simple things I'd like to do in vbscript for a homeseer script:

    1. simply exit a script mid-script. Everything seems to point to Wscript.quit. Except that Wscript doesn't seem to be declared and I can't seem to declare it.

    2. I'd like to use a dict for some stuff, and it seems that Wscript can do this, but same problem applies.

    Anyone know how to make it work, or have an alternate method for the above two things?

    regards,

    Paul

    #2
    For the first one, seems like 'return' should do the trick.

    Comment


      #3
      For 1, typically the Exit statement is the one to use: https://docs.microsoft.com/en-us/dot...exit-statement (Exit Sub in your case, although Return is equivalent to it inside a Sub)

      For 2, can you clarify what exactly you want to do?
      HS 3.0.0.548: 1990 Devices 1172 Events
      Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

      Comment


        #4
        As it turns out, doing some more digging i found non wscript ways to do both - Indeed, exit sub works, I don't know why my initial searches didn't find that. For #2, I think I've figured out how do do dicts without wscript - again, not sure why my initial searching only found wscript approaches.

        What I'm doing is taking sparkman's handywork for getting a switch parameter and doctoring it for my own use. The initial script takes a handful of parameters and splits them into strings, pipe separated, ie - "NodeID|ParameterNum". I'm making the switch take the result from the parameter and instead of just returning the result, I want it to set a value for a Virtual Device.

        By that token, I would have done something like "NodeID|ParameterNum|VirtualID|Value1|Set1|Value2|Set2|Value 3|Set3", the number of value/set pairs correlating to the number of values available for the virtual ID to show the things I want (If the Value is X then Set the virtual id to Y). The idea is that I can use a single universal script for getting the param and setting a value, for different switches that may have different value/set combinations, without having to write a separate script for each one.

        However, this includes the fact that each device not only might have different value/set combinations, but a different total number of value/set combinations. So I thought I could do it by way of sending the script something like "NodeID|ParameterNum|VirtualID|Value1^Set1^Value2^Set2^Value 3^Set3......." . The idea is that I split four values out with the pipe consistently, the fourth being the variable number of value/set pairs. Then take the fourth value and split it by carrot instead of pipe and dump them into a dict, then iterate over the dict to determine if the returned value matches one of the defined values, and set the virtual device to whatever set value it matches.

        Does that make sense the way I described it?

        I've been doing some vbscript digging and I think I've figured out what most of the moving parts are, I'm probably going to sit down later today and see if I can make it happen....

        regards,

        Paul

        Comment


          #5
          Thanks, I guess I’m still not clear on what exactly the end goal is. I think I have a script snippet somewhere that deals with a variable number of parameters. Let me know if you think that would help and I can dig it up. One thing on your searching, if you are searching for vbscript, you likely end up with deprecated information. Search for “VB.NET script” instead. They are similar, but not the same.
          HS 3.0.0.548: 1990 Devices 1172 Events
          Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

          Comment


            #6
            Originally posted by sparkman View Post
            Thanks, I guess I’m still not clear on what exactly the end goal is. I think I have a script snippet somewhere that deals with a variable number of parameters. Let me know if you think that would help and I can dig it up. One thing on your searching, if you are searching for vbscript, you likely end up with deprecated information. Search for “VB.NET script” instead. They are similar, but not the same.
            That's good to know - indeed a couple of things I've found have ended up being outdated. I'll keep that in mind. I think I'm making progress, but to clarify what the end goal is:

            I have some Inovelli light switches for which I want to use the LED on the switch to provide various notifications - you can set it to RGB values, make it blink quickly, pulse, etc. Since HS doesn't yet support the Inovelli, there are no child devices for it for notification control. I can set the parameter directly on the switch to achieve the desired notification. However, I've found that when you set the parameter for the LED control, once in awhile it doesn't take. I want to be able to identify what the LED is currently showing, so I can determine if I need to reset it or other-wise reapply the parameter.

            So, I've created a Virtual Device for the switch, and configured String/image/value sets on them to cover the different notifications the switch might be used for. The end goal of the script is to poll the switch periodically (or whenever I explicitly call it), take whatever value is set in the parameter, map it to the appropriate value for the Virtual Device, and set the Virtual Device, so that I know what the Switch is actually showing, rather than just believing what I previously tried to set it to.

            With that in place I can, at a minimum, see that something isn't notifying correctly. Ideally I may be able to automate self correction for what the switch should be displaying right now.

            The reason for wanting the variable number of value/set pairs in the script is so that I can send the script the value/set pairs that are relevant for the Virtual Device I want to query, so that I don't need to create a separate script for each Virtual Device.

            Does that make more sense?

            regards,

            Paul

            Comment


              #7
              For anyone who's interested, I have this much working so far. I can send the values, with any number of value/set pairs, and it correctly reads them in, then gets the param from the switch and returns the appropriate result. Next step is to stuff it into the virtual device.

              If there's a more efficient way to do what I've already done I'm certainly open to improvement.....

              Code:
              Imports System.Text
              
              Sub Main(parms As String)
                  Dim logName As String = "Z GetParam"                'set log name for HS log
                  Dim Debug As Boolean = True
              
                  If Debug Then hs.WriteLog(logName, "DEBUG: Doing Stuff.")
                  Dim ConfigResult as Integer = 0
                  Dim HomeID as String = "DB46E657"
                  Dim param as String() = parms.Split("|")
                  Dim NodeID as String = param(0)
                  Dim ParamNumber as String = param(1)
                  Dim VirtualID as String = param(2)
                  Dim ValuesRaw as String() = param(3).Split("^")
              
                  Dim Values = CreateObject("Scripting.Dictionary")
                  Dim Key, Thing
                  Dim Toggle as Integer = 0
                  For Each Thing in ValuesRaw
                      If Toggle = 0 Then
                          Key = Thing
                          Toggle = 1
                      Else
                          Values(Key) = Thing
                          Toggle = 0
                      End If
                  Next
                  If Debug Then
                      hs.WriteLog(logName, "DEBUG: HomeID = " & HomeID & " NodeID = " & NodeID & " ParamNumber = " & ParamNumber & " VirtualID = " & VirtualID)
                      For Each Thing in Values.keys
                          hs.WriteLog(logName, "DEBUG: Values('" & Thing & "') = " & Values(Thing))
                      Next
                  End If
              
                  Try
                      ConfigResult = hs.PluginFunction("Z-Wave", "", "Configuration_Get", New Object(){HomeID, Convert.ToByte(NodeID), Convert.ToByte(ParamNumber)})
                      If Debug Then hs.WriteLog(logName, "DEBUG: Result of Get Parameter " & ParamNumber & " on Node " & NodeID & " is " & ConfigResult)
                  Catch ex As Exception
                      hs.writelog(logName, "ERROR:  " & ex.Message.ToString)
                      Exit Sub
                  End Try
              
              End Sub

              Comment


                #8
                Ok, I have a working script. The script as written below does exactly what I need it to do. Now the only question is, could I make this more efficient? I may as well try to improve my vbscript skills while I'm at it....

                Code:
                Imports System.Text
                
                'get a parameter from a z-wave device and stuff an associated value into a virtual device
                'call Main with NodeID|ParameterNum|VirtualID|Value1^Set1^Value2^Set2^Value3^Set3.....
                'arbitrary number of Value/Set pairs OK
                'Thanks to Sparkman for initial code to get me rolling
                
                Sub Main(parms As String)
                    Dim logName As String = "Z GetParam"                'set log name for HS log
                    Dim Debug As Boolean = True
                
                    If Debug Then hs.WriteLog(logName, "DEBUG: Doing Stuff.")
                    Dim ConfigResult as Integer = 0
                    Dim Match as String = 0
                    Dim HomeID as String = "DB46E657"
                    Dim param as String() = parms.Split("|")
                    Dim NodeID as String = param(0)
                    Dim ParamNumber as String = param(1)
                    Dim VirtualID as String = param(2)
                    Dim ValuesRaw as String() = param(3).Split("^")
                
                    Dim Values = CreateObject("Scripting.Dictionary")
                    Dim Key, Thing
                    Dim Toggle as Integer = 0
                
                
                    ' Stuff Value/Set pairs into dict
                    For Each Thing in ValuesRaw
                        If Toggle = 0 Then
                            Key = Thing
                            Toggle = 1
                        Else
                            Values(Key) = Thing
                            Toggle = 0
                        End If
                    Next
                    If Debug Then
                        hs.WriteLog(logName, "DEBUG: HomeID = " & HomeID & " NodeID = " & NodeID & " ParamNumber = " & ParamNumber & " VirtualID = " & VirtualID)
                        For Each Thing in Values.keys
                            hs.WriteLog(logName, "DEBUG: Values('" & Thing & "') = " & Values(Thing))
                        Next
                    End If
                
                
                    ' Get parameter value from z-wave device
                    Try
                        ConfigResult = hs.PluginFunction("Z-Wave", "", "Configuration_Get", New Object(){HomeID, Convert.ToByte(NodeID), Convert.ToByte(ParamNumber)})
                        If Debug Then hs.WriteLog(logName, "DEBUG: Result of Get Parameter " & ParamNumber & " on Node " & NodeID & " is " & ConfigResult)
                    Catch ex As Exception
                        hs.writelog(logName, "ERROR:  " & ex.Message.ToString)
                        Exit Sub
                    End Try
                
                
                    ' Match parameter value to Value/Set pair
                    For Each Thing in Values.keys
                        If Debug Then hs.writelog(logName, "DEBUG: Thing = " & Thing & " ConfigResult = " & ConfigResult)
                        If Thing = ConfigResult Then
                            If Debug Then hs.writelog(logName, "DEBUG: Matches, so setting Match to " & ConfigResult)
                            Match = ConfigResult
                        End If
                    Next
                
                
                    ' Set Virtual Device to Value/Set pair, unless no match in which case set to 254
                    If Match = 0
                        hs.writelog(logName, "WARNING: Switch with NodeID " & NodeID & " Param " & ParamNumber & " has a Value of " & ConfigResult & " which does not match any Value/Set Pairs.")
                        For Each Thing in Values.keys
                            hs.WriteLog(logName, "WARNING: Provided Value/Set Pairs: Values('" & Thing & "') = " & Values(Thing))
                        Next
                        hs.writelog(logName, "WARNING: Setting Virtual ID Ref " & VirtualID & " to 254 (Non-Standard).")
                        hs.SetDeviceValueByRef(VirtualID, 254, True)
                    Else
                        if Debug Then hs.writelog(logName, "DEBUG: Value/Set pair is " & Match & "/" & Values(Match) & ". Setting VirtualID Ref " & VirtualID & " to " & Values(Match))
                        hs.SetDeviceValueByRef(VirtualID, Values(Match), True)
                    End If
                End Sub

                Comment

                Working...
                X