Announcement

Collapse
No announcement yet.

How To Create A Low Battery Level Event

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

    #31
    Originally posted by TomTom View Post
    And what a Dream Team this forum has!

    This would be great to display on the HSTouch screen too I'm thinking. I get the low alert email and I plan on getting to it, but with a son, his soccer, his school, my work...It gets put on the back burner. But if it's constantly on the hstouch screen, which I walk by all the time, it would be a good reminder.

    So what happens if you have two low battery devices, do they both get displayed or only one at a time?
    The script goes through them sequentially compares the value to a stored value. If the device is lower than the stored value then it becomes the stored value and device. After it finishes the list it writes the stored device name and value to the virtual device. So if you had 4 devices all showing the same and lowest level, the first one the script hit would be the only one displayed.

    The best thing to do would be to replace the batteries in the one that is shown in the virtual device, then tomorrow you would get an update of the next one. I replace batteries when they are below 10% which would give me plenty of time to get the next one.

    You can also go to the Device Manager and filter on a device type of Z-Wave battery, then click on the Status column to sort them and know at a glance if there are more than the one.

    I set an event to send me the monthly battery check email from the script in this post when the Low Battery virtual device is below threshold.
    HS4 Pro, 4.2.19.16 Windows 10 pro, Supermicro LP Xeon

    Comment


      #32
      Originally posted by rprade View Post
      The script goes through them sequentially compares the value to a stored value. If the device is lower than the stored value then it becomes the stored value and device. After it finishes the list it writes the stored device name and value to the virtual device. So if you had 4 devices all showing the same and lowest level, the first one the script hit would be the only one displayed...

      ...I set an event to send me the monthly battery check email from the script in this post when the Low Battery virtual device is below threshold.
      Nice. Now, not only do I get a monthly report of battery levels, regardless of the value, but when any battery falls below the threshold, I get an on-demand battery report by triggering the battery report event. Thanks for the tip. This makes for a very elegant and simple battery monitor that I don't have to manage any more as I add/remove devices with batteries.

      Also, by using some of the logic from that monthly report script, I changed the following line so that my script would pick up any device with the word battery in the device type, not just z-wave battery device types.

      Old line:
      Code:
      If StrComp(dv.Device_Type_String(Nothing), "Z-Wave Battery") = 0 Then  'Only work with devices with a battery
      New line:
      Code:
      If InStr(UCase(dv.Device_Type_String(Nothing)), "BATTERY") > 1 Then  'Only work with devices with a battery
      Attached Files

      Comment


        #33
        Originally posted by slbuck View Post
        Nice. Now, not only do I get a monthly report of battery levels, regardless of the value, but when any battery falls below the threshold, I get an on-demand battery report by triggering the battery report event. Thanks for the tip. This makes for a very elegant and simple battery monitor that I don't have to manage any more as I add/remove devices with batteries.

        Also, by using some of the logic from that monthly report script, I changed the following line so that my script would pick up any device with the word battery in the device type, not just z-wave battery device types.

        Old line:
        Code:
        If StrComp(dv.Device_Type_String(Nothing), "Z-Wave Battery") = 0 Then  'Only work with devices with a battery
        New line:
        Code:
        If InStr(UCase(dv.Device_Type_String(Nothing)), "BATTERY") > 1 Then  'Only work with devices with a battery
        We make a good team - its a pity there's no demand
        On a side note... I was born and raised in Texas, when I moved to Colorado about 30 years ago, a few of the employees at the company I bought referred to me as "that damn Texan", I considered it a badge of honor
        HS4 Pro, 4.2.19.16 Windows 10 pro, Supermicro LP Xeon

        Comment


          #34
          Nice work guys! Now that you're done tweaking the script how about posting the finished product in it's entirety for us lazy lurkers to copy paste.
          https://forums.homeseer.com/forum/de...plifier-plugin

          Comment


            #35
            +1, lol.
            Adding monthly reports to this is great, I had posted about getting a push of the monthly report in the other thread.
            What I really like about your script Stephen is that the device it creates can be displayed on my hstouch screens
            Love It!!

            Originally posted by happnatious1 View Post
            Nice work guys! Now that you're done tweaking the script how about posting the finished product in it's entirety for us lazy lurkers to copy paste.




            Sent from my iPhone
            Tom
            baby steps...starting again with HS3
            HS3Pro: Z-NET & 80 Z wave Devices,
            HSTouch: 4 Joggler (Android Kitkat), 2 iPhone, 3 iPads
            Whole House Audio: 5 SqueezePlay Jogglers w Bose Speakers
            In The Works: 10 Cameras Geovision, new Adecmo/Envisalink Alarm, Arduinos
            System: XP on Fanless Mini-ITX w/ SSD

            Comment


              #36
              Originally posted by rprade View Post
              Texan", I considered it a badge of honor

              Not in TX anymore, but my wife jokes with me about this all the time. Says I have some sort a "nationalistic pride for TX" . I rely don't see it, but...I guess it's a Texas thing!



              Sent from my iPhone
              Tom
              baby steps...starting again with HS3
              HS3Pro: Z-NET & 80 Z wave Devices,
              HSTouch: 4 Joggler (Android Kitkat), 2 iPhone, 3 iPads
              Whole House Audio: 5 SqueezePlay Jogglers w Bose Speakers
              In The Works: 10 Cameras Geovision, new Adecmo/Envisalink Alarm, Arduinos
              System: XP on Fanless Mini-ITX w/ SSD

              Comment


                #37
                Originally posted by happnatious1 View Post
                Nice work guys! Now that you're done tweaking the script how about posting the finished product in it's entirety for us lazy lurkers to copy paste.
                Consider your laziness satiated

                This is the final script that I'm running along with just two events that take care of updating the virtual device and to run a monthly or on-demand battery report when the value drops too low or becomes invalid.

                Thanks to everyone for the great ideas and tweaking.

                Code:
                    ' ==============================================================================================
                    '
                    ' Microsoft VB.NET Source File -- Created With tenScripting3 and Visual Studio Express 2015 for Windows Desktop
                    '
                    ' NAME: LowBatteryCheck.vb
                    '
                    ' AUTHOR: Stephen Buck 
                    ' Date  : 9/3/2015
                    '
                    ' COMMENT: Locates the device with the lowest battery and updates a virtual device with this value and the name of the device
                    '          Must create a virtual status only device and supply the reference ID of this device as a parameter
                    '
                    ' UPDATES: 9/3/2015 - updated string compare to include battery in the device type.  This will match any type of device that is a battery
                    '          9/1/2015 - initial release
                    '
                    ' ==============================================================================================
                    Public Sub Main(ByVal lowBatDvRef As String)
                        Try
                            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("Low Battery Check", "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("Low Battery Check", "No devices found")
                                    Exit Sub
                                End If
                                If InStr(UCase(dv.Device_Type_String(Nothing)), "BATTERY") > 1 Then  'Only work with devices with a battery
                                    If dv.devValue(Nothing) <= lowBatVal Then  'is current device value less than or equal to the current low battery value?
                                        dvLowBat = dv  'set lowbat device to the current device
                                        lowBatVal = dvLowBat.devValue(Nothing)  'set low battery value to current device value
                                    End If
                                End If
                            Loop Until EN.Finished
                
                            hs.SetDeviceValueByRef(lowBatDvRef, dvLowBat.devValue(Nothing), True)  'Set the lowest battery virtual device to the lowest battery value found
                            hs.SetDeviceString(lowBatDvRef, dvLowBat.devValue(Nothing) & "% - " & dvLowBat.Location(Nothing) & " " & dvLowBat.Name(Nothing), True)  'Set the lowest battery virtual device string to the %, location and name of the device with the lowest battery
                            hs.WriteLog("Low Battery Check", "Updated device " & hs.DeviceName(lowBatDvRef) & " with battery value:  " & dvLowBat.devValue(Nothing) & " location: " & dvLowBat.Location(Nothing) & " device name: " & dvLowBat.Name(Nothing))
                        Catch ex As Exception
                            hs.WriteLog("Error", "Exception in script LowBatteryCheck:  " & ex.Message)
                        End Try
                    End Sub
                Attached Files

                Comment


                  #38
                  Thank You, Stephen!!


                  Sent from my iPhone
                  Tom
                  baby steps...starting again with HS3
                  HS3Pro: Z-NET & 80 Z wave Devices,
                  HSTouch: 4 Joggler (Android Kitkat), 2 iPhone, 3 iPads
                  Whole House Audio: 5 SqueezePlay Jogglers w Bose Speakers
                  In The Works: 10 Cameras Geovision, new Adecmo/Envisalink Alarm, Arduinos
                  System: XP on Fanless Mini-ITX w/ SSD

                  Comment


                    #39
                    @slbuck

                    Thanks for this!! Exactly what I was looking for.

                    Comment


                      #40
                      Originally posted by slbuck View Post
                      Consider your laziness satiated

                      This is the final script that I'm running along with just two events that take care of updating the virtual device and to run a monthly or on-demand battery report when the value drops too low or becomes invalid.

                      Thanks to everyone for the great ideas and tweaking.

                      Code:
                          ' ==============================================================================================
                          '
                          ' Microsoft VB.NET Source File -- Created With tenScripting3 and Visual Studio Express 2015 for Windows Desktop
                          '
                          ' NAME: LowBatteryCheck.vb
                          '
                          ' AUTHOR: Stephen Buck 
                          ' Date  : 9/3/2015
                          '
                          ' COMMENT: Locates the device with the lowest battery and updates a virtual device with this value and the name of the device
                          '          Must create a virtual status only device and supply the reference ID of this device as a parameter
                          '
                          ' UPDATES: 9/3/2015 - updated string compare to include battery in the device type.  This will match any type of device that is a battery
                          '          9/1/2015 - initial release
                          '
                          ' ==============================================================================================
                          Public Sub Main(ByVal lowBatDvRef As String)
                              Try
                                  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("Low Battery Check", "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("Low Battery Check", "No devices found")
                                          Exit Sub
                                      End If
                                      If InStr(UCase(dv.Device_Type_String(Nothing)), "BATTERY") > 1 Then  'Only work with devices with a battery
                                          If dv.devValue(Nothing) <= lowBatVal Then  'is current device value less than or equal to the current low battery value?
                                              dvLowBat = dv  'set lowbat device to the current device
                                              lowBatVal = dvLowBat.devValue(Nothing)  'set low battery value to current device value
                                          End If
                                      End If
                                  Loop Until EN.Finished
                      
                                  hs.SetDeviceValueByRef(lowBatDvRef, dvLowBat.devValue(Nothing), True)  'Set the lowest battery virtual device to the lowest battery value found
                                  hs.SetDeviceString(lowBatDvRef, dvLowBat.devValue(Nothing) & "% - " & dvLowBat.Location(Nothing) & " " & dvLowBat.Name(Nothing), True)  'Set the lowest battery virtual device string to the %, location and name of the device with the lowest battery
                                  hs.WriteLog("Low Battery Check", "Updated device " & hs.DeviceName(lowBatDvRef) & " with battery value:  " & dvLowBat.devValue(Nothing) & " location: " & dvLowBat.Location(Nothing) & " device name: " & dvLowBat.Name(Nothing))
                              Catch ex As Exception
                                  hs.WriteLog("Error", "Exception in script LowBatteryCheck:  " & ex.Message)
                              End Try
                          End Sub
                      Will this work with x10 motion sensors?
                      Hector
                      ____________________________________
                      Win.2003 OS, HS3
                      BLDSC,BLstat,BLRadar,BLRamdom,BLOccupied
                      BLups,BLrain8,HSTouch,Ultrajones Weatherbug,
                      MyTrigger,ACRF2,W800,Zwave
                      AP800,Honeywell Stat

                      Comment


                        #41
                        Thank you, everyone

                        You just made my custom UI for the facility I am managing look a lot more professional and clean. You have also made it much easier for maintenance folks to replace the batteries proactively.

                        Thank you!

                        Comment


                          #42
                          Originally posted by Rotech View Post
                          Will this work with x10 motion sensors?
                          As long as the device type has the word battery in it, it should work. That was the update I made to the script which now just checks for battery instead of z-wave battery.

                          Comment


                            #43
                            Updated script

                            I made a few tweaks to the script which now allows you to update the lowest batter on demand as well as send a battery report on-demand.

                            This works nicely in HS Touch since you can now select the device and then choose to update or send report to get the scripts to fire.

                            Here is the immediate script command to enter under the Tools/Control Panel menu for the lowest battery device update button:

                            &hs.devicescriptbutton_addbutton(<refID>,"Update",1001,"LowB atteryCheck.vb","","",1,1,1)

                            And the send report button:

                            &hs.devicescriptbutton_addbutton(<refID>,"Send Report",1002,"BatteryReport.vb","","",1,2,1)

                            the <refID> needs to be replaced with the refID of the lowest battery device to add the button, "Update" is the text on the button, 1001 is a unique value for each button on the device, "LowBatteryCheck.vb" is the script to run, the first "" is the sub which defaults to Main if null, the second "" is an optional variable to pass to the script which shows up as parameter 1 (parameter 0 is already the refID), the final three 1's are the row, column, and span of the button.

                            I had to update the scripts to accept an object instead of a string, and then dim a string as the parms(0) to get the refid.

                            Also, I updated any events that called the scripts to now just set the device to either "update", or "send report" to trigger the script.

                            I've attached the new scripts and some screenshots. To get it working:

                            1) Just put the new scripts into the script folder (the battery report script will send out a monthly battery report and was one that I modified from another thread).
                            2) You will need the refID of the lowest battery virtual device you have created
                            3) Run the script commands above to create the buttons on your virtual device replacing <refID> with your number.

                            Let me know if you have any problems.


                            Here's the new LowBatteryCheck.vb script:

                            Code:
                                ' ==============================================================================================
                                '
                                ' Microsoft VB.NET Source File -- Created With tenScripting3 and Visual Studio Express 2015 for Windows Desktop
                                '
                                ' NAME: LowBatteryCheck.vb
                                '
                                ' AUTHOR: Stephen Buck 
                                ' Date  : 9/3/2015
                                '
                                ' COMMENT: Locates the device with the lowest battery and updates a virtual device with this value and the name of the device
                                '          Must create a virtual status only device and supply the reference ID of this device as a parameter
                                '
                                ' UPDATES: 02/14/2016 - script can now be triggered by pressing an update button. Had to accept an object instead of a string, and then dim a string = parms(0) to get the refID
                                '          9/3/2015 - updated string compare to include battery in the device type.  This will match any type of device that is a battery
                                '          9/1/2015 - initial release
                                '
                                ' ==============================================================================================
                                Public Sub Main(Parms As Object)
                                    Try
                                        Dim lowBatDvRef As String = Parms(0)
                                        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("Low Battery Check", "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("Low Battery Check", "No devices found")
                                                Exit Sub
                                            End If
                                            If InStr(UCase(dv.Device_Type_String(Nothing)), "BATTERY") > 1 Then  'Only work with devices with a battery
                                                If dv.devValue(Nothing) <= lowBatVal Then  'is current device value less than or equal to the current low battery value?
                                                    dvLowBat = dv  'set lowbat device to the current device
                                                    lowBatVal = dvLowBat.devValue(Nothing)  'set low battery value to current device value
                                                End If
                                            End If
                                        Loop Until EN.Finished
                            
                                        hs.SetDeviceValueByRef(lowBatDvRef, dvLowBat.devValue(Nothing), True)  'Set the lowest battery virtual device to the lowest battery value found
                                        hs.SetDeviceString(lowBatDvRef, dvLowBat.devValue(Nothing) & "% - " & dvLowBat.Location(Nothing) & " " & dvLowBat.Name(Nothing), True)  'Set the lowest battery virtual device string to the %, location and name of the device with the lowest battery
                                        hs.WriteLog("Low Battery Check", "Updated device " & hs.DeviceName(lowBatDvRef) & " with battery value:  " & dvLowBat.devValue(Nothing) & " location: " & dvLowBat.Location(Nothing) & " device name: " & dvLowBat.Name(Nothing))
                                    Catch ex As Exception
                                        hs.WriteLog("Error", "Exception in script LowBatteryCheck:  " & ex.Message)
                                    End Try
                                End Sub
                            and here's the BatteryReport.vb script:

                            Code:
                            Dim filename As String = "C:\Program Files (x86)\HomeSeer HS3\BatteryReport.txt" ' full path and filename of report (if sending report as an email attachment)
                                Dim mailTo As String = "user@domain.com" ' email TO address
                                Dim mailFrom As String = "homeseer@domain.com" ' email FROM address
                                Dim ReportAsAttachment As Boolean = False ' set to 'True' if report should be emailed as a file (attachment), set to 'False' if report should be in email body
                                '____ End of User Configuration ____
                            
                                Dim msg As String = ""
                            
                                Public Sub Main(Parms As Object)
                                    Dim myArray(0) As BatteryDevice
                                    Dim MaxNameLen As Integer = 0
                            
                                    Try
                                        Dim dv As Scheduler.Classes.DeviceClass
                                        Dim EN As Scheduler.Classes.clsDeviceEnumeration
                                        EN = hs.GetDeviceEnumerator
                                        If EN Is Nothing Then
                                            hs.WriteLog("Battery Report", "Error getting Enumerator")
                                            Exit Sub
                                        End If
                            
                                        Do
                                            dv = EN.GetNext
                                            If dv Is Nothing Then Continue Do
                            
                                            ' Add to array if device type contains 'battery'
                                            If InStr(UCase(dv.Device_Type_String(Nothing)), "BATTERY") > 1 Then
                                                ReDim Preserve myArray(UBound(myArray) + 1)
                                                myArray(UBound(myArray) - 1) = New BatteryDevice
                            
                                                myArray(UBound(myArray) - 1).refID = ("[" & dv.Ref(Nothing).ToString & "]").PadLeft(6) & vbTab
                                                If dv.devValue(Nothing) > 100 Then ' if battery value is larger than 100(%), battery is invalid state
                                                    myArray(UBound(myArray) - 1).devValue = "  ERROR" & vbTab & vbTab
                                                Else
                                                    myArray(UBound(myArray) - 1).devValue = (dv.devValue(Nothing).ToString & "%").PadLeft(7) & vbTab & vbTab
                                                End If
                                                ' Determine max length of device loc/name string (for formatting)
                                                If (dv.Location2(Nothing) & " " & dv.Location(Nothing) & " " & dv.Name(Nothing)).Length > MaxNameLen Then
                                                    MaxNameLen = (dv.Location2(Nothing) & " " & dv.Location(Nothing) & " " & dv.Name(Nothing)).Length
                                                End If
                                                myArray(UBound(myArray) - 1).devName = dv.Location2(Nothing) & " " & dv.Location(Nothing) & " " & dv.Name(Nothing)
                                                myArray(UBound(myArray) - 1).lastChange = dv.Last_Change(Nothing).ToString.PadRight(22)
                            
                                            End If
                                        Loop Until EN.Finished
                            
                                        ReDim Preserve myArray(UBound(myArray) - 1)
                                        Array.Sort(myArray)
                            
                                        report(Nothing, , MaxNameLen) ' HEADER
                            
                                        Dim battdevice As Object
                                        For Each battdevice In myArray
                                            report(battdevice.refID & battdevice.devValue & battdevice.devName.PadRight(MaxNameLen) & vbTab & battdevice.lastChange, UBound(myArray) + 1)
                                        Next
                            
                                        report(Nothing, UBound(myArray) + 1, , True) ' FOOTER
                            
                                    Catch ex As Exception
                                        hs.WriteLog("Battery Report", "ERROR:  Exception in script: " & ex.Message)
                                    End Try
                                    hs.WriteLog("Battery Report", "Battery report created.")
                            
                                    If ReportAsAttachment Then
                                        hs.SendEmail(mailTo, mailFrom, "", "", "Battery Report", "Battery Report attached.", filename)
                                    Else
                                        hs.SendEmail(mailTo, mailFrom, "", "", "Battery Report", msg, "")
                                    End If
                            
                                End Sub
                            
                                Public Sub report(myRow As String, Optional recCount As Integer = -1, Optional MaxNameLen As Integer = 0, Optional EndReport As Boolean = False)
                                    If EndReport Then ' Add FOOTER
                                        myRow = vbCrLf & " [" & recCount & " Battery Devices] - Report created " & Now.ToString & vbCrLf
                            
                                    ElseIf recCount = -1 Then ' Add HEADER
                                        If ReportAsAttachment Then
                                            Try
                                                System.IO.File.Delete(filename)
                                            Catch ex As Exception
                                            End Try
                                        End If
                                        myRow = "RefID" & vbTab & "Battery" & vbTab & vbTab & "Device Location / Name" & Space(MaxNameLen - 22) & vbTab & "Last Changed" & vbCrLf
                                        myRow = myRow & "-----" & vbTab & "-------" & vbTab & vbTab & "----------------------" & Space(MaxNameLen - 22) & vbTab & "------------"
                                    Else
                            
                                    End If
                            
                                    If ReportAsAttachment Then
                                        Try
                                            My.Computer.FileSystem.WriteAllText(filename, myRow & vbCrLf, True, System.Text.Encoding.Default)
                                        Catch ex As Exception
                                            hs.WriteLog("Battery Report", "REPORT ERROR: " & ex.Message)
                                        End Try
                                    Else
                                        msg = msg & myRow & vbCrLf
                                    End If
                            
                                End Sub
                            
                                Public Class BatteryDevice
                                    Implements IComparable(Of BatteryDevice)
                            
                                    Public refID As String
                                    Public devValue As String
                                    Public lastChange As String
                                    Public devName As String
                            
                                    Public Function CompareTo(ByVal other As BatteryDevice) As Integer Implements IComparable(Of BatteryDevice).CompareTo
                                        Return Me.devValue.CompareTo(other.devValue)
                                    End Function
                                End Class
                            Attached Files

                            Comment


                              #44
                              slbuck, when i run the commands to add the buttons, I do not get an error, but the buttons do not get added to my device.

                              Here's what the log says:
                              Control Panel Immediate Script: &hs.devicescriptbutton_addbutton(544,"Update",1001,"LowBatte ryCheck.vb","","",1,1,1)

                              Comment


                                #45
                                Originally posted by PJJPtx View Post
                                slbuck, when i run the commands to add the buttons, I do not get an error, but the buttons do not get added to my device.

                                Here's what the log says:
                                Control Panel Immediate Script: &hs.devicescriptbutton_addbutton(544,"Update",1001,"LowBatte ryCheck.vb","","",1,1,1)
                                Make sure that your virtual low battery device looks like the attached as far as your status text goes. The value ranges 0-100 and 101-254 and value 255 all need to have status and not the default Both selected. Also, when running the &hs.devicescriptbutton_addbutton command, you may want to type in the entire text. I have seen HS3 do some funny stuff with adding spaces randomly in the script when the text wraps to the next line. When I was testing this just now, I copied and pasted the immediate script text and the button showed up, however, when I attempted to click the Update button, I received the following error:

                                Loading script LowB atteryCheck.vb :File not found: LowB atteryCheck.vb

                                HS3 put in a space in the name of the script and that happened to be where the text wrapped in the input window. I added another test device and this time typed in the command and then it worked.
                                Attached Files

                                Comment

                                Working...
                                X