Announcement

Collapse
No announcement yet.

Get hs.devicename without Location?

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

    Get hs.devicename without Location?

    Hi, I currently have a script that gets the device name of my alarm and speaks it when a zone is opened. It works great, but I want it to only speak the name of the zone without the location. In my case, I want it to "Front Door" instead of "Security DSC Front Door". Can this be done? If so, how?

    Thanks in advanced!

    Code:
    Sub Main(parms As Object)
    	
    	Dim zonerefs() As Integer = {58,59,60,61,62}
    	
    	hs.CreateVar("zonestatus")
    	
    	For each zoneref As Integer in zonerefs
    	If hs.devicevalue(zoneref) = "609" then
    		hs.SaveVar("zonestatus",hs.devicename(zoneref))
    		hs.Speak ("$$GLOBALVAR:zonestatus:")
    	End if
    	Next
    End Sub

    #2
    Assuming that all of the zones have the same prefix, I would do something like this...

    Code:
        Sub Test(parms As Object)
    
            Dim zonerefs() As Integer = {58, 59, 60, 61, 62}
            Const DeviceNamePrefix As String = "Security DSC"
    
            hs.CreateVar("zonestatus")
    
            For Each zoneref As Integer In zonerefs
                If hs.DeviceValue(zoneref) = "609" Then
                    hs.SaveVar("zonestatus", hs.DeviceName(zoneref).Replace(DeviceNamePrefix, String.Empty))
                    hs.Speak("$$GLOBALVAR:zonestatus:")
                End If
            Next
    
        End Sub
    Are you using the global variable for anything else outside of this event/script? The reason I ask is because you could use a local variable and streamline thing a bit if the global var isn't needed for something else.
    Last edited by Cleavitt76; January 4, 2015, 12:46 AM. Reason: formatted code.

    Comment


      #3
      Thanks Cleavitt76, that worked! This script is the only thing the Global Variable is being used for. How would this look like with a local variable instead?

      Comment


        #4
        You would declare a local variable of type String to store the data and then pass that variable to the hs.Speak method. For example...

        Code:
         Sub Main(parms As Object)
                 
           Dim zonerefs() As Integer = {58, 59, 60, 61, 62}
           Const DeviceNamePrefix As String = "Security DSC"
          
            For Each zoneref As Integer In zonerefs
              If hs.DeviceValue(zoneref) = "609" Then
                 Dim SpeakText As String = hs.DeviceName(zoneref).Replace(DeviceNamePrefix, String.Empty)
                 hs.Speak(SpeakText)
              End If
           Next
          
         End Sub

        Comment


          #5
          Great! That worked too. What if I wanted to add that variable in an email. I tried adding the following, but it didn't work. BTW, I renamed the variable to ZoneStatus

          hs.SendEmail("user@domain.com","hs@domain.com","","","Zone Opened!", "The $$ZoneStatus: was just opened!","")

          This is that format that worked when I was using a Global Variable.

          Thanks again,

          Comment


            #6
            That format ($$variable) is the built in substitution variable functionality that is specific to HomeSeer. As you already noticed, it will only work with variables that HomeSeer knows about such as HomeSeer Global variable functionality. All of that is useful if you need to share a variable between multiple events/scripts, but it isn't needed otherwise. Here is how you would do it with variables defined in your script...

            Code:
             hs.SendEmail("user@domain.com","hs@domain.com","","","Zone Opened!", "The " & ZoneStatus & " was just opened!", "")
            The & character is used to concatenate Strings together in VB.Net. The strings can be named variables or hardcoded text or a combination thereof.

            Comment


              #7
              Cleavitt76, is it possible to define this variable to accept any integer, instead of specifying them. The thing is I have tons of devices and sometimes the Ref ID changes if I reinstall the plugin. I would then have to go back and modify the script.

              Dim zonerefs() As Integer = {58, 59, 60, 61, 62}

              Comment


                #8
                Are you asking if you can pass the ZoneRef values as a parameter from an event rather than hardcode them? If so, this is one way to do it...

                The event parameter field would contain the values separated by a comma like so: 58, 59, 60, 61, 62

                Code:
                     Sub Main(ByVal params As Object)
                 
                        Const DeviceNamePrefix As String = "Security DSC"
                        Dim ZoneRefs As Collections.Generic.List(Of Integer) 'Create a generic list to store integer values.
                 
                         'Parse, convert, and store the parameter values.
                        For Each Param As String In params.ToString.Split(","c) 'Split the parameter string by the comma character and process each value.
                            Param = Param.Trim 'Remove any leading or trailing white space from the string value.
                            ZoneRefs.Add(Convert.ToInt32(Param)) 'Convert the string value to an Integer type and add it to the list.
                        Next
                 
                        For Each ZoneRef As Integer In ZoneRefs
                            If hs.DeviceValue(ZoneRef) = "609" Then
                                Dim SpeakText As String = hs.DeviceName(ZoneRef).Replace(DeviceNamePrefix, String.Empty)
                                hs.Speak(SpeakText)
                            End If
                        Next
                 
                     End Sub
                I added comments behind some of the code to explain the purpose of that line, but you can remove it in your saved code.

                Comment


                  #9
                  Same concept, alternate method...

                  Code:
                       Sub Main(ByVal params As Object)
                   
                           Const DeviceNamePrefix As String = "Security DSC"
                  
                           'Process each parameter. Split the parameter string by the comma character.
                           For Each Param As String In params.ToString.Split(","c)
                               
                               'Remove any leading or trailing white space from the string value.  
                               Param = Param.Trim 
                    
                               Try
                                  'Convert the parameter string value to an Integer type.
                                  Dim ZoneRef As Integer = Convert.ToInt32(Param) 
                    
                                   If hs.DeviceValue(ZoneRef) = 609 Then
                                      Dim SpeakText As String = hs.DeviceName(ZoneRef).Replace(DeviceNamePrefix, String.Empty)
                                      hs.Speak(SpeakText)
                                  End If
                    
                               Catch ex As Exception
                                  hs.WriteLog("Error", "Script error: The value " & Param & " could not be processed.")
                              End Try
                    
                           Next
                    
                       End Sub

                  Comment

                  Working...
                  X