Page 1 of 1

Battery level for device error

Posted: Friday 08 December 2017 9:23
by whatisk
I currently have a few Xiaomi sensors off a single gateway - Motion sensor, door sensor and wireless switch.
I recently got a second wireless switch which I have connected to the gateway and shows up in Domoticz.

I have been working on a script to email me when the battery on these gets below a certain level. It work on all but the new wireless switch - which is showing a battery level under devices. It also recognises when the button is clicked, double clicked and long pressed.

When I run the script I get the following error:
2017-12-08 17:55:00.513 Error: dzVents: Error: An error occured when calling event handler batt test2
2017-12-08 17:55:00.513 Error: dzVents: Error: ...icz/var/scripts/dzVents/generated_scripts/batt test2.lua:8: attempt to compare nil with number

Code: Select all

return {
	active = true,
	on = {
		timer = {
		    'at 18:16 on fri'
		},
	},
	execute = function(domoticz,dummy)
		local garagemotion = domoticz.devices('Xiaomi Motion Sensor')
		local garagesensor = domoticz.devices('Xiaomi Door Sensor')
		local switch1 = domoticz.devices('Xiaomi Switch 1')
		local switch2 = domoticz.devices('Xiaomi Switch 2')
		if (garagemotion.batteryLevel < 10) then
			domoticz.email('Garage Motion Sensor Battery', 'Garage Motion Sensor battery is low.', '<email address>')
		end
		if (garagesensor.batteryLevel < 10) then
			domoticz.email('Garage Door Sensor Battery', 'Garage Door Sensor battery is low.', '<email address>')
		end
		if (switch1.batteryLevel < 10) then
			domoticz.email('Switch 1 Battery', 'Switch 1 battery is low.', '<email address>')
		end
		if (switch2.batteryLevel < 97) then
		    domoticz.email('Switch 2 Battery', 'Switch 2 battery is low.', '<email address>')
		end
	end
}
I am just not sure why it works for one switch and not the other which is identical.
I realise there are probably nicer ways to do it, but I am just playing around at the moment.

I also got the following to work, but it just ignores the new wireless switch.

Code: Select all

return {
	active = true,
	on = {
		timer = {'at 18:07 on fri'},
	},
	execute = function(domoticz,dummy)
        local BATTERY_THRESHOLD = 99
		local message

		-- first filter on low battery level
		local lowOnBat = domoticz.devices().filter(function(device)

			local level = device.batteryLevel
			return (level ~= nil and -- not all devices have this attribute
					level ~= 255 and -- probably means something like 'not applicable'
					level <= BATTERY_THRESHOLD)

		end)

		-- then loop over the results
		lowOnBat.forEach(function(lowDevice)
            domoticz.log('Low battery warning: ' .. lowDevice.name, domoticz.LOG_ERROR)
		end)
	end
}
If I need to provide anything further for assistance let me know.

Cheers.

Re: Battery level for device error

Posted: Wednesday 03 January 2018 10:32
by dannybloe
Interesting. Have you restarted Domoticz after you added the new switch? If so, you can turn dzVents in to debug logging mode for a little (Domoticz settings). It will create a file domoticzData.lua in scripts/dzVents. That file is a dump of all the data that Domoticz sends to dzVents. Find the new switch and see if it has batter level information in there.

Re: Battery level for device error

Posted: Wednesday 03 January 2018 11:24
by whatisk
Thanks for the reply. I had forgotten about this.

I have just installed Domoticz beta on a Raspberry Pi and imported all my settings, etc. It now seems to be working fine. I am sure I restarted the old Domoticz (stable) setup on my NAS when testing.