Last active
May 13, 2019 14:50
-
-
Save OliPassey/03e6e39bedc02461945016fabb80f48f to your computer and use it in GitHub Desktop.
Send metrics from HomeSeer to InfluxDB for display in Grafana
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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. | |
'Script amended by reidfo for Linux Compatibility & greater error / log handling | |
'Script amended by Rob2791 to include TCP Connection Limit lift to 20 | |
'Posted to GitHub by OliPassey for VersionControl - please feel free to PR, Fork, etc | |
'Original release and forum discussion: https://forums.homeseer.com/forum/homeseer-products-services/system-software-controllers/hs3-hs3pro-software/hs3-hs3pro-discussion/107992-a-script-to-log-device-changes-to-influxdb-for-use-with-grafana | |
'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.Core | |
Imports System.Web | |
Imports System.Net | |
Imports System.IO | |
Imports System.Text | |
Public 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 = "homeseer" | |
dim SKIP_LIST = "" '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) | |
ServicePointManager.DefaultConnectionLimit = 20 | |
'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) | |
dim myReader As StreamReader | |
dim myStream as Stream | |
myStream = httpResponse.GetResponseStream() | |
myReader = New StreamReader(myStream) | |
Dim responseText = myReader.ReadToEnd() | |
' hs.WriteLog("L2DB-InfluxDB", "Response: " & responseText) | |
Catch ex As Exception | |
hs.WriteLog("L2DB-InfluxDB", "Error: " & ex.ToString()) | |
hs.WriteLog("L2DB-InfluxDB", "Request was: " & postdata) | |
End Try | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment