Announcement

Collapse
No announcement yet.

Script to copy Status Value and Status Graphics Pairs from one device to another

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

  • Script to copy Status Value and Status Graphics Pairs from one device to another

    Have you ever modified an existing device's Status Value and Status Graphics pairs and then want to do the same for other devices? It can be a lot of work, so I wrote this script to make that easier. Just create an event to run the script and then pass the reference IDs of the source and target devices to it:

    Click image for larger version  Name:	Capture.PNG Views:	1 Size:	73.2 KB ID:	1298845

    The script will find all Status Value pairs (only those that are Status or Both, not Control only) and copy them to another device. The corresponding Status Graphics pairs are also copied with some caveats (see the notes in the beginning of the script). Note that any existing Status Value and Status Graphics pairs on the target device are first removed. IF you don't want that to happen, then put an apostrophe (') in front of these two lines in the script:

    Code:
            hs.DeviceVSP_ClearAll(targetDev, True)
            hs.DeviceVGP_ClearAll(targetDev, True)
    The script has only been tested under Windows. If it does not work under linux for some reason, please let me know and provide the error messages you get in the log.

    NOTE: I wrote this script to update some virtual devices that I had created. It is not intended to be used to update devices owned by plugins. Although it may work for some plugins, it may also "break" some plugin-owned target devices. If you do want to try it for a device owned by a plugin, it is at your own risk. Ensure you have a good backup prior to attempting that.

    Code:
    'This script copies Status Value and Status Graphics Pairs from one device to another
    '
    'Version 1.1
    'by sparkman @ homeseer forum
    'https://forums.homeseer.com/forum/developer-support/scripts-plug-ins-development-and-libraries/script-plug-in-library/1298844-script-to-copy-status-value-and-status-graphics-pairs-from-one-device-to-another
    '
    'Call the script from and event with method Main and the reference ID of the source device and the reference ID of the target device separated by a comma
    'This only copies pairs that are "Status" or "Both", not "Control" and only copies graphics that match a status or an exact range
    'Therefore, if you have a Status Graphics pair for which there is no corresponding Status Value pair, it won't get copied
    'Also, if you have a Status Value range, but have split that range into a number of Status Graphics ranges, only the one where the range start matches, will get copied
    'The following VS class members are not dealt with by the script.  If you use them in the source device, then their settings will not be copied over
                'Public Shared Function AddDataReplace(ByVal Index As Integer) As String
                'Public Property StringList As String()
                'Public WriteOnly Property StringListAdd As String
                'Public Const ScaleReplace As String = "@S@"
    
    Sub Main(ByVal Parms As String)
        Dim Debug As Boolean = False
        Dim logName As String = "VSVG Copy"
        Dim ParmArray() As String
        ParmArray = Parms.ToString.Split(",")
        Dim sourceDev As Integer = CInt(ParmArray(0))        'reference ID of the source device to use for existing valuess
        Dim targetDev As Integer = CInt(ParmArray(1))        'reference ID of the target device to change
        hs.writelog(logName,"<font color='#000080'>" & CStr(hs.DeviceVSP_CountStatus(sourceDev)) & "<Font Color='#000000'> Status Value Pairs to be copied from ref id: <font color='#008000'>" & CStr(sourceDev) & "<Font Color='#000000'> to ref id: <font color='#008000'>" & CStr(targetDev) & "</font>")
    
        Try
    
            'Clear existing Status Value / Status Graphic pairs on the target Dev
            hs.DeviceVSP_ClearAll(targetDev, True)
            hs.DeviceVGP_ClearAll(targetDev, True)
    
            Dim ControlCount As Integer = hs.DeviceVSP_CountControl(sourceDev)
            Dim StatusCount As Integer = hs.DeviceVSP_CountStatus(sourceDev)
            Dim ControlOnlyCount As Integer = StatusCount - ControlCount
            If Math.Abs(ControlOnlyCount) > 0 Then hs.WriteLogEx(logName,"Not Copying " & CStr(Math.Abs(ControlOnlyCount)) & " control only Value Pairs","#FF0000")
    
            'Get a listing of all Status Value pairs
            Dim ValueStatusPairStatusListing As HomeSeerAPI.VSVGPairs.VSPair() = hs.DeviceVSP_GetAllStatus(sourceDev)
            Dim ValueStatusPairs As HomeSeerAPI.VSPair
            Dim SPair As HomeSeerAPI.VSPair
            Dim newGPair As HomeSeerAPI.VGPair
            Dim oldGPair As HomeSeerAPI.VGPair
            Dim AddVSPairSuccess As Boolean = False
            Dim AddVGPairSuccess As Boolean = False
            Dim TempValue As Double = 0
    
            For Each ValueStatusPairs In ValueStatusPairStatusListing
    
                AddVSPairSuccess = False
                AddVGPairSuccess = False
    
                If ValueStatusPairs.ControlStatus = 1 Then
                    SPair = New VSPair (HomeSeerAPI.ePairStatusControl.Status)
                ElseIf ValueStatusPairs.ControlStatus = 3 Then
                    SPair = New VSPair (HomeSeerAPI.ePairStatusControl.Both)
                End If
    
                Spair.Render_Location.Row = ValueStatusPairs.Render_Location.Row
                Spair.Render_Location.Column = ValueStatusPairs.Render_Location.Column
                Spair.Render_Location.ColumnSpan = ValueStatusPairs.Render_Location.ColumnSpan
                Spair.Render = ValueStatusPairs.Render
                Spair.PairButtonImageType = ValueStatusPairs.PairButtonImageType
                Spair.PairButtonImage = ValueStatusPairs.PairButtonImage
                Spair.ControlUse = ValueStatusPairs.ControlUse
    
                If ValueStatusPairs.PairType = 1 Then
                    SPair.PairType = VSVGPairType.SingleValue
                    TempValue = ValueStatusPairs.Value
                    Spair.Value = TempValue
                    Spair.Status = hs.DeviceVSP_GetStatus(sourceDev,ValueStatusPairs.Value,HomeSeerAPI.ePairStatusControl.Status)
                    If Debug Then hs.writelog(logName,"Value/Status: " & CStr(TempValue) & " / " & hs.DeviceVSP_GetStatus(sourceDev,TempValue,HomeSeerAPI.ePairStatusControl.Status))
    
                    oldGPair = hs.DeviceVGP_Get(sourceDev, TempValue)
                    If Not oldGPair Is Nothing Then
                        newGPair = New VGPair
                        newGPair.PairType = VSVGPairType.SingleValue
                        newGPair.Set_Value = TempValue
                        newGPair.Graphic = hs.DeviceVGP_GetGraphic(sourceDev, TempValue)
                        If Debug Then hs.writelog(logName,"&nbsp;&nbsp;Value/Graphics: " & CStr(TempValue) & " " & Chr(34) & hs.DeviceVGP_GetGraphic(sourceDev, TempValue) & Chr(34))
                        AddVGPairSuccess = hs.DeviceVGP_AddPair(targetDev, newGPair)
                        If Not AddVGPairSuccess Then hs.writelogEx(logName, "Addition of Status/Graphics " & TempValue & " not successful" ,"#FF0000")
                    End If
    
                ElseIf ValueStatusPairs.PairType = 2 Then
                    SPair.PairType = VSVGPairType.Range
                    TempValue = ValueStatusPairs.RangeStart
                    SPair.RangeStart = TempValue
                    SPair.RangeEnd = ValueStatusPairs.RangeEnd
                    SPair.RangeStatusPrefix = ValueStatusPairs.RangeStatusPrefix
                    SPair.RangeStatusSuffix = ValueStatusPairs.RangeStatusSuffix
                    SPair.RangeStatusDecimals = ValueStatusPairs.RangeStatusDecimals
                    SPair.RangeStatusDivisor = ValueStatusPairs.RangeStatusDivisor
                    SPair.IncludeValues = ValueStatusPairs.IncludeValues
                    SPair.ValueOffset = ValueStatusPairs.ValueOffset
                    SPair.HasAdditionalData = ValueStatusPairs.HasAdditionalData
                    SPair.HasScale = ValueStatusPairs.HasScale
                    SPair.ZeroPadding = ValueStatusPairs.ZeroPadding
                    If Debug Then hs.writelog(logName,"Value/Status: " & CStr(TempValue) & " To " & CStr(ValueStatusPairs.RangeEnd))
    
                    oldGPair = hs.DeviceVGP_Get(sourceDev, TempValue)
                    If Not oldGPair Is Nothing Then
                        newGPair = New VGPair
                        newGPair.PairType = VSVGPairType.Range
                        newGPair.RangeStart = oldGPair.RangeStart
                        newGPair.RangeEnd = oldGPair.RangeEnd
                        newGPair.Graphic = hs.DeviceVGP_GetGraphic(sourceDev, oldGPair.RangeStart)
                        If Debug Then hs.writelog(logName,"&nbsp;&nbsp;Value/Graphics: " & CStr(oldGPair.RangeStart) & " To "  & CStr(oldGPair.RangeEnd) & " " & Chr(34) & hs.DeviceVGP_GetGraphic(sourceDev, oldGPair.RangeStart) & Chr(34))
                        AddVGPairSuccess = hs.DeviceVGP_AddPair(targetDev, newGPair)
                        If Not AddVGPairSuccess Then hs.writelogEx(logName, "Addition of Status/Graphics " & TempValue & " not successful" ,"#FF0000")
                    End If
    
                End If
    
                AddVSPairSuccess = hs.DeviceVSP_AddPair(targetDev, SPair)
                If Not AddVSPairSuccess Then hs.writelogEx(logName, "Addition of Status/Value " & TempValue & " not successful" ,"#FF0000")
    
            Next
            ValueStatusPairStatusListing = Nothing
            ValueStatusPairs = Nothing
            SPair = Nothing
            newGPair = Nothing
            oldGPair = Nothing
            AddVSPairSuccess = Nothing
            AddVGPairSuccess = Nothing
            TempValue = Nothing
            ControlCount = Nothing
            StatusCount = Nothing
            ControlOnlyCount = Nothing
    
            hs.writelogEx(logName, "Completed @ " & CStr(Now),"#008000")
    
        Catch ex As Exception
            hs.writelogEx(logName, ex.Message.ToString,"#FF0000")
        End Try
    
        Debug = Nothing
        logName = Nothing
        ParmArray = Nothing
        sourceDev = Nothing
        targetDev = Nothing
    
    End Sub
    HS 3.0.0.548: 1965 Devices 1146 Events
    Z-Wave 3.0.1.262: 122 Nodes on one Z-Net

  • #2
    Oh, so many times I've wished for this functionality. Thanks, can't wait to give it a try!
    -Wade

    Comment


    • #3
      Just tested, perfect result. Brilliant! Thanks again!
      -Wade

      Comment


      • #4
        Originally posted by cc4005 View Post
        Just tested, perfect result. Brilliant! Thanks again!
        Great, glad it worked for you!
        HS 3.0.0.548: 1965 Devices 1146 Events
        Z-Wave 3.0.1.262: 122 Nodes on one Z-Net

        Comment


        • #5
          Made some minor changes to the script to put a warning in the log about "Control Only" VS pairs not getting copied if the source device has those.
          HS 3.0.0.548: 1965 Devices 1146 Events
          Z-Wave 3.0.1.262: 122 Nodes on one Z-Net

          Comment


          • #6
            Sorry to ask...but how are you using this script in home automation? Always looking to learn something new!
            Thanks!


            Sent from my iPhone using Tapatalk
            HS3 3.0.0.500 &HSTouch Designer 3.0.71 with 782 Devices, 479 Events
            Plugin's:
            BLBackup, BLOccupied, BLShutdown, EasyTrigger, Ecobee,
            EnvisaLink DSC, PHLocation, Pushover, SONOS, Blue Iris, UltraRachio3,
            weatherXML, Jon00 Alexa Helper, Network Monitor, Z-Wave 3.0.1.252

            Comment


            • #7
              Originally posted by The Profit View Post
              Sorry to ask...but how are you using this script in home automation? Always looking to learn something new!
              Thanks!
              The script is not an "automation" script. I've created virtual devices in the past an not really happy with the icons I had used, etc. So rather than manually changing each device to how I know want them, I change one device and then use the script to copy those changes to similar devices. As an example, I have 7 devices that track daily rainfall over the last week and I used the script to copy the changes on one to the other 6.
              HS 3.0.0.548: 1965 Devices 1146 Events
              Z-Wave 3.0.1.262: 122 Nodes on one Z-Net

              Comment


              • #8
                To take this script concept a bit further, I'm curious whether the following is of interest to you to add. I certainly don't have the scripting chops to do it.

                1. Allow for mass copying. I.e., pass a series of device numbers wherein the first is the source and following are destination devices.
                2. Allow for mass copying of device thumbnail image.

                On second thought, the thumbnail image would need to be separate--either by setting an option or separate script entirely. I'd like to use the thumbnail for quick visual identification of categories of virtual devices but it would take so long to individually change them I've never taken it on.

                Thanks.
                -Wade

                Comment


                • #9
                  cc4005

                  Hi Wade, I'll take a look at item 1. For item 2, there's a plugin called DTSDeviceImages that allows the bulk setting of device images/thumbnails: https://forums.homeseer.com/forum/ho...images-at-once

                  Cheers
                  Al
                  HS 3.0.0.548: 1965 Devices 1146 Events
                  Z-Wave 3.0.1.262: 122 Nodes on one Z-Net

                  Comment


                  • #10
                    Originally posted by sparkman View Post
                    cc4005

                    Hi Wade, I'll take a look at item 1. For item 2, there's a plugin called DTSDeviceImages that allows the bulk setting of device images/thumbnails: https://forums.homeseer.com/forum/ho...images-at-once

                    Cheers
                    Al
                    Thanks for both!
                    -Wade

                    Comment

                    Working...
                    X