Battery level for device error
Posted: Friday 08 December 2017 9:23
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
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.
If I need to provide anything further for assistance let me know.
Cheers.
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 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
}
Cheers.