Announcement

Collapse
No announcement yet.

Change device names by event

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

    Change device names by event

    Is it possible to change a device name and maybe the location by an event? I've go some plug in modules that change their "assignment" by season. I already have a virtual device and associated events that tracks the holiday seasons. Some of my plugin modules usually control a lamp. But when the holidays come around, the lamp gets put away and some decoration plugs in in its place. So I'm looking for a way for the system to change the name of the device from "table lamp" to "Christmas tree". It would be great if I could also change the device icon and location too so these devices would appear in the "holiday" groupings as opposed to the family room group.
    Also, there are some modules that are only used in certain seasons. It would also be handy to set the "hide device" flag from an event so they don't appear on the list when they're not in season"
    I'm not sure if the device type matters or not but these are mostly Insteon devices controlled by mark's plugin.



    --Jon Chuchla--

    Sent from my iPhone using Tapatalk

    #2
    Only by script, this should get you started. If you call this with the parameters of Reference|New Name|New Location then it should change these to the new values. Be careful though there is little in the way of error checking, you could do with more error checking ideally.

    Code:
    Sub Main(ByVal Parm As Object)
    
        Try
            Log("ChangeName Script Parameters: " & Parm.ToString)
    
            If Parm.ToString IsNot Nothing Then
                Dim ParmsSplit() As String = Parm.ToString.Split("|")
                If hs.deviceexistsref(convert.toint32(parmsSplit(0))) Then
                    Log("Device Reference: " & parmsSplit(0))
                    Dim dv As Scheduler.Classes.DeviceClass = hs.getdevicebyref(convert.toint32(parmsSplit(0)))
                    Log("Device Existing Name: " & dv.Name(hs))
                    dv.name(hs) = parmsSplit(1)
                    dv.Location(hs) = parmsSplit(2)
    
                    hs.saveeventsdevices()
                    Log("Device New Name: " & dv.Name(hs))
                Else : Log("Device Does Not Exist At Provided Reference")
                End If
            Else : Log("Parameters Empty")
            End If
        Catch ex As Exception
            Log("Exception: " & ex.Message.ToString)
        End Try
    
    End Sub
    
    Sub Log(ByVal ParStr As String)
        hs.writelog("ChangeName", ParStr)
    End Sub

    Comment


      #3
      Wow! That's tremendously complicated for a simple name change. Funny you mention not enough error checking. There's already too much error checking in there for my liking.
      Where does the reference parameter come from? Is it a GUID or something for that device? Is there no getDeviceByName function?


      --Jon Chuchla--

      Sent from my iPhone using Tapatalk

      Comment


        #4
        Originally posted by Jon View Post
        Wow! That's tremendously complicated for a simple name change. Funny you mention not enough error checking. There's already too much error checking in there for my liking.
        Where does the reference parameter come from? Is it a GUID or something for that device? Is there no getDeviceByName function?


        --Jon Chuchla--

        Sent from my iPhone using Tapatalk
        Every device has a reference so it is using that, you can get a device by it's name yes but you also have to specify a location, no issues doing either but it is easier to check a device exists on the back of the reference than it is by the name (no deviceexistsname). If you run the script in the winter to change the name then you would have to do "Old Device Name|New Device Name" and then "New Device Name|Old Device Name" whereas if you used a reference then your first parameter would always be constant as the reference so less likelihood of a error on the case of the device changing or something...you can do it any number of ways (could do it by address also).

        Comment


          #5
          I'm finally getting around to making this work. I'll be honest, I'm not well versed in homeseer scripting, or VB for that matter. But I do follow what your sample code is doing.
          I tried a simple copy/paste of your code sample into a script file that I named setDeviceNameByRef.txt. In the event, I'm calling Main and using ref|NewName as the parameter.
          When I go to execute it, I get an error in the log:
          Running script, script run or compile error in file: C:/Program Files/HomeSeer HS3/scripts/SetDeviceNameByRef.txt1006:Expected ')' in line 1 More info: Expected ')'
          The script I used is EXACTLY the same as the sample you posted above.
          Any ideas? I'm sure it's just a syntax issue, but I'm getting a bit lost in searching help files.

          Comment


            #6
            Did you put a .txt extension on the file? If so try changing it to .vb
            💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

            Comment


              #7
              Thank you Rupp. That took care of that problem.

              I assume the extension tells the engine which interpreter/compiler to use for a given script. Can I script in any language or just vb and vb.net? Could I be lucky and all of the .net languages are supported? C, C++, C#?

              Now the next thing to overcome: variable numbers of parameters. Is this possible? I use Location and Location2 fields in my devices. Sometimes I'll want to change them, but other times I don't want them to change. It would be convenient to make some optional. I guess the next thing to do is play with is validating params(2) and parms(3) and if they're not null, then set location and location2.

              Comment


                #8
                That does it. In the interest in helping others who find this by search, here's what I came up with. It requires the ref and new name, but the location2 and location are optional but need to be defined cumulatively in that order. (because that's how I use them) If not defined, it retains the existing locations.

                Code:
                Sub Main(ByVal Parm As Object)
                    Try
                        Log("Running setDeviceNameByRef(" & Parm.ToString & ")" )
                        If Parm.ToString IsNot Nothing Then
                            Dim ParmsSplit() As String = Parm.ToString.Split(",")
                            Log("Found " & cstr(ParmsSplit.length)& " parameters.")
                            If hs.deviceexistsref(convert.toint32(parmsSplit(0))) Then
                                Log("Device Reference: " & parmsSplit(0))
                                Dim dv As Scheduler.Classes.DeviceClass = hs.getdevicebyref(convert.toint32(parmsSplit(0)))
                                Log("Device Old Name: " & dv.Name(hs))
                                dv.name(hs) = parmsSplit(1)
                                Log("Device New Name: " & dv.Name(hs))
                                if parmsSplit.Length = 3 
                                    dv.Location2(hs) = parmsSplit(2)
                                    Log("Device New Location2: " & dv.Location2(hs))
                                else
                                    Log("New Location2 not specified. Keeping old Location2: " & dv.Location2(hs))
                                end if 
                                if parmsSplit.Length = 4 
                                    dv.Location(hs) = parmsSplit(3)
                                    Log("Device New Location: " & dv.Location(hs))
                                else
                                    Log("New Location not specified. Keeping old Location: " & dv.Location(hs))
                                end if 
                                hs.saveeventsdevices()
                            Else : Log("Device Does Not Exist At Provided Reference")
                            End If
                        Else : Log("Parameters Empty")
                        End If
                    Catch ex As Exception
                        Log("Exception: " & ex.Message.ToString)
                    End Try
                End Sub
                 Sub Log(ByVal ParStr As String)
                    hs.writelog("ChangeName", ParStr)
                End Sub

                Comment

                Working...
                X