Saturday, December 16, 2017

Hack to fix bug in the BLAN Homeseer plugin

For some reason a couple versions back the BLAN plugin stopped updating the value with the status of a monitored device changed. At least as far as switching the value back to on when the device comes back online. The following script looks at all the devices with location2 set to BLAN and sets the value based on the status string. I have an event set up to run it every 10 minutes.

' Hack to make Blan devices have a status value
' call from a set or value changed event
Public Sub statusBlan(ByVal refID As Object)
    Dim status As String = hs.DeviceString(refID)
    Dim oldVal = hs.DeviceValue(refID)

    If (InStr(status, "Offline") > 0) Then
        hs.SetDeviceValueByRef(refID, 0, False)
    ElseIf (InStr(status, "Online") > 0) Then
        hs.SetDeviceValueByRef(refID, 100, False)
        hs.SetDeviceValueByRef(refID, 50, False)
    End If

    Dim newVal = hs.DeviceValue(refID)

    If Not oldVal = newVal Then
        hs.WriteLog("statusBlan", "Fixed value of BLAN device " & refID & " with status of " & status & " from " & oldVal & " to " & newVal)
    End If
End Sub

Public Sub fixBlan(Parms As Object)
    Dim label As String = "fixBlan"
        Dim lowBatVal As Double = 100  'start at 100% max for low battery value
        Dim dv, dvLowBat As Scheduler.Classes.DeviceClass
        Dim EN As Scheduler.Classes.clsDeviceEnumeration = hs.GetDeviceEnumerator  'Get all devices

        If EN Is Nothing Then
            hs.WriteLog(label, "Error getting Enumerator")
            Exit Sub
        End If

        Do  'check each device that was enumerated
            dv = EN.GetNext
            If dv Is Nothing Then  'No device, so quit
                hs.WriteLog(label, "No devices found")
                Exit Sub
            End If
            'hs.WriteLog(label, "location:" & dv.Location(Nothing) & ":location2:" & dv.Location2(Nothing) & ":InStr:" & InStr(UCase(dv.Location2(Nothing)), "BLAN"))
            If InStr(UCase(dv.Location2(Nothing)), "BLAN") > 0 Then  'Only work with devices in location BLAN
                'hs.WriteLog(label, "ref:" & dv.ref(Nothing) & ":Address:" & dv.Address(Nothing) & ":value:" & dv.devValue(Nothing) & ":location:" & dv.Location(Nothing) & ":location2:" & dv.Location2(Nothing) & ":device name:" & dv.Name(Nothing) & ":type:" & dv.Device_Type_String(Nothing))
            End If
        Loop Until EN.Finished

    Catch ex As Exception
        hs.WriteLog("Error", "Exception in script " & label & ":  " & ex.Message)
    End Try
End Sub

No comments:

Post a Comment