Page 1 of 1

Battery Notifications

Posted: Friday 03 March 2017 21:02
by getSurreal
Added a new script option for getting notifications for low battery status of devices.

https://www.domoticz.com/wiki/Battery_level_check

This script will execute once a day at the time that you specify. It will check all your used devices and send an email with a list of all devices below the battery threshold. Optionally you can also receive a weekly summary of all your battery devices and their current battery level regardless of the threshold.

This script will run on Linux or Windows and does not require an external json parsing library. It uses the json library provided with Domoticz. It also doesn't need to write any temporary json files to disk.

Re: Battery Notifications

Posted: Friday 03 March 2017 21:56
by waaren
@getSurreal,

I am using the dzVent LUA script supplied as an example of the ease of use of dzVents to check the battery status of all relevant devices. Would that be something to add to the wiki ?

Code: Select all

local BATTERY_THRESHOLD = 10

return {
	active = true,
	on = {
		['timer'] = 'every hour'
	},
	execute = function(domoticz)

		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)

			message = message .. 'Device ' ..
					lowDevice.name .. ' is low on batteries (' .. lowDevice.batteryLevel .. '), '

		end)

		if (message) then
			domoticz.notify('Low battery warning', message, domoticz.PRIORITY_NORMAL)
			domoticz.log('Low battery warning: ' .. message, domoticz.LOG_ERROR)
		end
	end
}

Re: Battery Notifications

Posted: Friday 03 March 2017 22:11
by getSurreal
Posting it there could help others in the future. I don't know if it needs to be run differently, but right now it doesn't work for me. I get the following error.
Error: EventSystem: Lua script test did not return a commandArray

If it works, it could have saved me a lot of time vs rewriting the other example that's on the wiki.

Re: Battery Notifications

Posted: Friday 03 March 2017 22:52
by getSurreal
I didn't know what DZvents was at first. In this case I don't think that script should go in the same place on the wiki and just kept with DZvents.

I do need to look more into DZvents though. Looks nicer.

Re: Battery Notifications

Posted: Sunday 09 April 2017 18:23
by bimse
I use 'Alternate LUA script' but get this message.
To require password, how insert this.

Error: Event System: In script_time_battery: /home/pi/domoticz/scripts/lua/JSON.lua:660: html passed two JSON: Decode ():
401 UNAUTHORIZED

Some suggestions

Re: Battery Notifications

Posted: Monday 10 April 2017 0:57
by getSurreal
Not sure how to pass username and password through the script and not typically good practice to add credentials into a script. I suggest adding an exception for your domoticz server to be able to connect to itself without a password. This is in the domoticz settings system tab.

Re: Battery Notifications

Posted: Wednesday 12 April 2017 10:50
by bimse
without password it works
thanks

Re: Battery Notifications

Posted: Wednesday 12 April 2017 18:37
by Derik
Is this working for all sensors? Zwave include?

Re: Battery Notifications

Posted: Wednesday 19 April 2017 9:42
by L2v2P
The daily low battery report sends an empty e-mail every day (because all battery levels are above the threshold). Is there a way to prevent the e-mail from sending when all battery levels are above the threshold?

Re: Battery Notifications

Posted: Wednesday 19 April 2017 9:54
by L2v2P
L2v2P wrote:The daily low battery report sends an empty e-mail every day (because all battery levels are above the threshold). Is there a way to prevent the e-mail from sending when all battery levels are above the threshold?
Think I fixed it by checking if the Message variable contains any data:

Code: Select all

if Message ~= '' then
	commandArray['SendEmail']='Domoticz Battery Levels#'.. Message .. '#' .. EmailTo
end

Re: Battery Notifications

Posted: Wednesday 21 February 2018 13:35
by CLEMENT99
getSurreal wrote: Friday 03 March 2017 21:02 This script will run on Linux or Windows and does not require an external json parsing library. It uses the json library provided with Domoticz. It also doesn't need to write any temporary json files to disk.
Hi,
I'm running this script under windows but I get an error :
EventSystem: in Battery_check: [string "-- ========================================= ..."]:49: attempt to index global 'devices' (a nil value)

Capture.JPG
Capture.JPG (25.89 KiB) Viewed 6002 times

Re: Battery Notifications

Posted: Wednesday 21 February 2018 19:02
by CLEMENT99
I found the problem, it was a bad copy of quotes in the io.popen command but now, i get this error:

Error: EventSystem: in Battery_check: [string "-- ========================================= ..."]:49: attempt to index global 'devices' (a nil value)
That's at the line :
for i,device in ipairs(devices.result) do ....

Code: Select all

    handle = io.popen("curl 'http://" .. Domoticz .. ":" .. DomoticzPort .. "/json.htm?type=devices&order=name'")
    devicesJson = handle:read('*all')
    handle:close()
    devices = json:decode(devicesJson)
    BattToReplace = false
    for i,device in ipairs(devices.result) do
            if device.BatteryLevel < BatteryThreshold and device.Used == 1 then
            Message = Message .. device.Name .. ' battery level is ' .. device.BatteryLevel .. '%<br>'
            print(device.Name .. ' battery level is ' .. device.BatteryLevel .. '%')
        end
    end
This time, I don't find the issue. :(

Re: Battery Notifications

Posted: Thursday 22 February 2018 11:21
by CLEMENT99
I just found that the option to a send message when battery low level is low is already implemented in Domoticz.
Just go to settings/Other tab and change the Battery low level value.
Definitively, great product ..
:oops: