Announcement

Collapse
No announcement yet.

A script to log device changes to InfluxDB for use with Grafana

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

    #46
    Hello

    I tried the script again , looks like I had something wrong with my startup script

    now I get this error , any help ?

    Running script L2DB-influxdb.vb :Exception has been thrown by the target of an invocation.->Does entry point Main exist in script? at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Obj ect obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at A.c17b105f989efe61e5979e67bec1ef734.cffd66cb0782c50126727e3f a75582d7a()

    Comment


      #47
      After a bit of a struggle I finally have it up and running for 2 days now

      adding new dashboards everyday and figuring out grafana as I go , pretty awesome

      Only think to figure out now is to add a dashboard to reflect device status , like door sensors , I think it need to read the Homeseer device string .

      a few ideas I added :

      - Chromecast anf Android box volumes
      - Mobile phones battery status ( using Life 360 Plugin )
      Attached Files

      Comment


        #48
        Nice graphs. I like the temperature bar graph. Hadn't thought of representing it that way.
        iCore5 Win 10 Pro x64 SSD

        HS3 Pro Edition 3.0.0.435 Windows

        BLOccupied:,Device History:,Yamaha:,UltraMon3:,mcsXap:,Restart:,UltraNetatmo3:, UltraM1G3:,Ultra1Wire3:,BLBackup:,Harmony Hub:,DoorBird:,UltraECM3:,Nanoleaf 3P:,UltraRachio3:,Z-Wave:,SDJ-Health:,BLGarbage:,Blue-Iris:,Chromecast:,Pushover 3P:,EasyTrigger:

        Comment


          #49
          Originally posted by mminehan View Post
          Nice graphs. I like the temperature bar graph. Hadn't thought of representing it that way.
          Thanks

          I managed to add almost all my devices for now , trying with other services as well , Might add my Hassio setup to get other devices

          Only thing I 'm missing now is the option to display the string of the Homeseer device , I can show that Plex is playing or paused but hoping to display the title as well
          Attached Files

          Comment


            #50
            I am struggling with getting this working. When I add the line in startup.vb, this message comes after every status change.

            Running script L2DB-influxdb-include.vb :Exception has been thrown by the target of an invocation.->Does entry point Main exist in script? at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Obj ect obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Scheduler.clsRunVBNetScript.ExecuteScript()
            The line is added near top like this

            Sub Main(Parm As Object)
            hs.RegisterStatusChangeCB("L2DB-influxdb-include.vb","Main")
            hs.WriteLog("Startup", "(Startup.vb script) Scripting is OK and is now running Startup.vb")
            I also tried adding it down near end, but before the End Sub as I figured it would be inside the Sub Main definition. Is that correct?

            When I run the script manually it comes with this error

            Running script C:\Program Files (x86)\HomeSeer HS3\scripts\L2DB-influxdb-include.vb :Exception has been thrown by the target of an invocation.Index was outside the bounds of the array.
            Anyone able to help sort this?

            Comment


              #51
              Brilliant - many thanks to bdc for the original and reidfo for the Linux conversion - it's working perfectly on my HS3/RPi installation.

              TomB - this is what I have in my Startup.vb - it's at the end.

              ' You may add your own commands to this script.
              ' See the scripting section of the HomeSeer help system for more information.
              ' You may access help by going to your HomeSeer website and clicking the HELP button,
              ' or by pointing your browser to the /help page of your HomeSeer system.

              hs.RegisterStatusChangeCB("L2DB-influxdb.vb","Main")

              End Sub


              I guess running it manually the error is as expected because you aren't passing the necessary device parameter.

              Did you try un-commenting the logging to see if it's being called at all?

              Comment


                #52
                One issue I found is that Grafana doesn't play very nice with infrequently updated values like battery status and switch on-off, so I added a recurring event to log the value of all devices every 30 minutes.

                The script is attached.

                I also use a device to control logging on/off, and a group called "Technical" to define devices to not be logged, so those bits are a little different.

                The only concern I have is if HS loses device values when it restarts, this will mess up the timeseries a bit; will have to see. On the other hand, maybe I'll write something to go and get the last values from Influx...
                Attached Files

                Comment


                  #53
                  Originally posted by rmohsen View Post
                  Hello

                  I tried the script again , looks like I had something wrong with my startup script

                  now I get this error , any help ?
                  I'm having the exact same problem (on Ubuntu).
                  How did you fix this?

                  Comment


                    #54
                    Originally posted by ZoRaC View Post

                    I'm having the exact same problem (on Ubuntu).
                    How did you fix this?
                    I don't remember exactly , but it was something inside the syntax of the script , if you want I copy and past mine and send you

                    Comment


                      #55
                      Originally posted by rmohsen View Post
                      if you want I copy and past mine and send you
                      That would be great!

                      Comment


                        #56
                        Originally posted by ZoRaC View Post

                        That would be great!
                        here you go

                        Notice my settings are

                        - I'm running the influxDB on the same computer as HS3 so my server ip is "localhost" , i tried to install it on a different computer but it slowed down my HS3 and became unusable so i went back to the localhost

                        - port is the same

                        - database name is "homeauto"

                        i also disabled the script from writing to HS3 log because it was a mess

                        hope this works with you

                        Code:
                        'VB.Net script to write Homeseer 3 values to InfluxDB
                        'Created by Brian based on code found on the Homeseer forums. No warranty. Use at your own risk.
                        'Uncomment the log statements if you're having problems to try to track down the error.
                        
                        'Installation Instrustions: 
                        '    0: Install InfluxDB & get it running
                        '    1: Modify the variables below to fit your system.
                        '    2: Add this line to Homeseer\Scripts\Startup.vb
                        '    hs.RegisterStatusChangeCB("L2DB-influxdb.vb","Main")
                        '    3: Restart Homeseer
                        
                        Imports System.Web
                        Imports System.Net
                        Imports System.IO
                        Imports System.Text
                        
                        Sub Main(ByVal Parms As Object)
                        
                            '==========================================================
                            'Modify these to fit your system
                            dim INFLUX_DB_SERVER_IP = "localhost"
                            dim INFLUX_DB_SERVER_PORT = "8086"
                            dim INFLUX_DB_DATABASE_NAME = "homeauto"
                            dim SKIP_LIST = "183,3474,3473,346,335,333,345,343,342,3478,3483,349" 'comma separated list of device references to skip logging
                            '==========================================================
                        
                            dim device_name, device_location, device_location2, deviceObj, device_type, problem
                            dim dev_address as string
                            Dim device_value As Double
                            Dim devRef As Integer
                        
                        
                            'hs.WriteLog("L2DB-InfluxDB", "Script running")
                        
                            'Get device info from Homeseer
                            dev_address = Parms(1) 'address of device. 
                            device_value = Parms(2) 'new value of device
                            devRef = Parms(4) 'Device reference of the device 
                            deviceObj = hs.GetDeviceByRef(devRef)
                            device_name = deviceObj.name(hs)
                            device_location = deviceObj.location(hs)
                            device_location2 = deviceObj.location2(hs)
                            device_type = deviceObj.Device_Type_String(hs)
                        
                            If device_type = "Timer" Then 'Don't log timers
                                Exit Sub
                            End If
                        
                            If SKIP_LIST <> "" Then 
                                Dim skips = SKIP_LIST.split(New Char() {","c})
                                Dim skip as String
                                For Each skip in Skips
                                    If devref = skip
                                        Exit Sub
                                    End If
                                Next
                            End If
                        
                            'hs.WriteLog("L2DB-InfluxDB", "Logging to Database: Reference: " & devref & " Address: " & dev_address & ", Device Name: " & device_name & ", New Value: " & device_value)
                        
                        
                            'Build the post data string
                            Dim postdata as String = devref
                            If device_name <> "" Then 
                                postdata = postdata & ",name=" & device_name
                            End If
                            If device_location <> "" Then 
                                postdata = postdata & ",location=" & device_location
                            End If
                            If device_location2 <> "" Then 
                                postdata = postdata & ",location2=" & device_location2
                            End If
                            If device_type <> "" Then 'Types are optional and not all devices have them
                                postdata = postdata & ",type=" & device_type
                            End If
                            If dev_address <> "" Then 'Addresses are optional and not all devices have them
                                postdata = postdata & ",address=" & dev_address
                            End If
                        
                            postdata = postdata.Replace(" ", "\ ")
                            postdata = postdata & " value=" & device_value
                        
                            'hs.WriteLog("L2DB-InfluxDB", postdata)
                        
                            'Set up the Webrequest
                            Dim url = "http://" & INFLUX_DB_SERVER_IP & ":" & INFLUX_DB_SERVER_PORT & "/write?db=" & INFLUX_DB_DATABASE_NAME
                            Dim httpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
                        
                        
                            'Dim httpWebRequest = DirectCast(WebRequest.Create("https://requestb.in/XXXXXXX"), HttpWebRequest)  'For testing- to see what the request contains
                        
                            httpWebRequest.ContentType = "application/x-www-form-urlencoded"
                            httpWebRequest.Method = "POST"
                            Dim encoding As New System.Text.UTF8Encoding
                        
                            'Make the request to the database
                            Try
                                Dim data As Byte() = encoding.GetBytes(postdata)
                                httpWebRequest.ContentLength = data.Length
                                Dim myStream As Stream = httpWebRequest.GetRequestStream()
                        
                                If data.Length > 0 Then
                                    myStream.Write(data, 0, data.Length)
                                    myStream.Close()
                                End If
                            Catch ex As Exception
                                'hs.WriteLog("L2DB-InfluxDB", "Error: " & ex.ToString())
                            End Try
                        
                            Try
                                Dim httpResponse = DirectCast(HttpWebRequest.GetResponse(), HttpWebResponse) 
                                Using streamReader = New StreamReader(httpResponse.GetResponseStream().ToString) 
                                    Dim responseText = StreamReader.ReadToEnd() 
                                    'hs.WriteLog("L2DB-InfluxDB", "Response: " & responseText) 
                                End Using 
                            Catch ex As Exception
                                'hs.WriteLog("L2DB-InfluxDB", "Error: " & ex.ToString())
                                'hs.WriteLog("L2DB-InfluxDB", "Request was: " & postdata)
                            End Try
                        
                        End Sub

                        Comment


                          #57
                          Easy alternative to the above depending on your requirements.
                          I use MQTT plugin in HS3. I have mosquitto broker running on Raspberry Pi zeroW.
                          I have grafana and influxdb/telegraf running on HS3 server.
                          Telegraf subscribes to all topics in mosquitto broker.

                          No programming necessary for HS3. Just publish your devices in MQTT plugin.

                          Again just an alternative.
                          Click image for larger version

Name:	Grafana.png
Views:	617
Size:	510.4 KB
ID:	1250729

                          Comment


                            #58
                            Originally posted by technerd View Post
                            Easy alternative to the above depending on your requirements.
                            I use MQTT plugin in HS3. I have mosquitto broker running on Raspberry Pi zeroW.
                            I have grafana and influxdb/telegraf running on HS3 server.
                            Telegraf subscribes to all topics in mosquitto broker.

                            No programming necessary for HS3. Just publish your devices in MQTT plugin.

                            Again just an alternative.
                            Click image for larger version

Name:	Grafana.png
Views:	617
Size:	510.4 KB
ID:	1250729
                            how did you manage to do that ? I tried to publish devices using MQTT for other apps but I couldn't do it , if you have a step by step guide it would be great , I tried to output the device string to grafana but couldn't as well

                            Comment


                              #59
                              Here you go a simple step by step:

                              1.) Install the following HS3 plugin: https://shop.homeseer.com/products/m...lug-in-for-hs3
                              2.) Install MQTT mosquito broker on Raspberry Pi(Pi) or Windows
                              3.) Install InfluxDB on Pi or windows
                              4.) Install Telegraf on Pi or windows
                              5.) Configure Telegraf to log all MQTT topics from the Mosquitto MQTT broker
                              5.a) In the Inputs section of the Telegraf config file replace servers with your nodename running the MQTT broker. In the Topics section put the # wildcard

                              # Read metrics from MQTT topic(s)
                              [[inputs.mqtt_consumer]]
                              ## MQTT broker URLs to be used. The format should be scheme://host: port,
                              ## schema can be tcp, ssl, or ws.
                              servers = ["tcp://mqttbrokernode:1883"]
                              ## MQTT QoS, must be 0, 1, or 2
                              qos = 2
                              ## Connection timeout for initial connection in seconds
                              connection_timeout = "30s"

                              ## Topics to subscribe to
                              topics = ["#",]

                              6.) Publish HS3 values to MQTT broker
                              7.) Use Grafana to display data from InfluxDB

                              Comment


                                #60
                                I just discovered this script and it looks really promising. I`m currently finding out where to place the

                                "ServicePointManager.DefaultConnectionLimit = 20"

                                Do you think my attached script is correct?

                                I`m also trying to solve the message about dropping callbacks. But it looks like HomeSeer changes something for this also :-)

                                Changes from 3.0.0.478 and 3.0.0.495:

                                * Event callback queue for plugins changed from 500 to 2000 entries to avoid dropping callbacks

                                Note I`m currently running 3.0.0.470 and my log stops working short time after a reboot, So the messages disappear anyhow

                                https://forums.homeseer.com/forum/ho...s-and-download


                                After running for a couple of hours, it looks like not al devices are logged to the influx database.
                                Attached Files

                                Comment

                                Working...
                                X