Bodyweight Script

Forum over Homeseer scripts (DUTCH forum)

Moderators: TANE, Ruud

Post Reply
User avatar
Irritanterik
Member
Member
Posts: 82
Joined: Tue Apr 19, 2011 1:00 pm
Location: Amersfoort
Contact:

Bodyweight Script

Post by Irritanterik »

I don't take weight lightly, so I use a Oregon Scientific BWR102 weight scale with RFXCOM and Homeseer.
RFXCharts is not my cup of tea, so I wrote this script for assigning a measured weight to a person in my household.

Code: Select all

' FIRST: 1. Create new device type and set cWeightDeviceType in script
'        2. Create a device for each person in the household with the newly created device type
'        3. Use direct control to set a first value for each person in (kilo's * 10):
'              e.g. &hs.SetDeviceValue ("W1", 800) 

Const cLogLevel As Integer = 1                  '0 = no logging, 1 = normal logging, 2 = debug logging
Const cScriptName As String = "TON_Weegschaal"
Const cWeightDevice As String = "]14"
Const cWeightDeviceType As String = "Gewicht"   'All virtual counters must have this custom device type

Public Sub Main(ByVal Parms As String)
	Dim iWeightMeasured AS Integer
	Dim sMessage As String
	Dim oDeviceList
	Dim oDevice
	Dim iAbsoluteDifferenceMatch As Integer
	Dim iAbsoluteDifferenceDevice As Integer
	Dim sDeviceMatch As String
	
    'Read measured weight
	iWeightMeasured  = hs.DeviceValue(cWeightDevice)
	'Compose message
	sMessage = (iWeightMeasured / 10) & " kg gewogen op weegschaal"
	Log(sMessage,1)
	
	'Find best match in weight devices
	oDeviceList = hs.GetDeviceEnumerator
	iAbsoluteDifferenceMatch = 1000
	Do While Not oDeviceList.Finished
		oDevice = oDeviceList.GetNext
		If Not oDevice Is Nothing Then
			If oDevice.dev_type_string = cWeightDeviceType And oDevice.hc & oDevice.dc <> cWeightDevice Then
				iAbsoluteDifferenceDevice = Math.Abs(hs.DeviceValue(oDevice.hc & oDevice.dc) - iWeightMeasured)
				If iAbsoluteDifferenceDevice < iAbsoluteDifferenceMatch Then
					iAbsoluteDifferenceMatch = iAbsoluteDifferenceDevice 
					sDeviceMatch = oDevice.hc & oDevice.dc
				End If
			End If
		End If
	Loop
	
	'If best match is acceptable: then register
	If iAbsoluteDifferenceMatch < 40 Then
		Log("Gevonden match: " & sDeviceMatch)
	    hs.SetDeviceValue (sDeviceMatch, iWeightMeasured)
		hs.SetDeviceString (sDeviceMatch, "<img src=RFXCOM\images\weight.gif align=absmiddle border=0> " & FormatNumber(iWeightMeasured / 10, 1) & "kg")
	Else
		Log("Gevonden match: " & sDeviceMatch & ", maar verschil is te groot.")
	End If
	
	'Send push notification
	hs.plugin("Pushover (3P)").PushMessage(sMessage)
End Sub


Private Sub Log(sMessage As String, Optional iMessageLevel As Integer = 1)
	If cLogLevel >= iMessageLevel Then
		hs.WriteLog(cScriptName, sMessage)
	End if
End Sub
Devices:
gewicht_devices.png
gewicht_devices.png (24.4 KiB) Viewed 2906 times
Event:
gewicht_event.png
gewicht_event.png (13.45 KiB) Viewed 2906 times
HP t5630w with Homeseer 2 > Will migrate to Homey | RFXLAN | IRTrans | Opentherm Gateway | ITHO ECO-FAN integration | Harrison Curtain | Z-wave
Post Reply

Return to “Homeseer Scripts Forum”