Battery Notifications

Moderator: leecollings

Post Reply
getSurreal
Posts: 19
Joined: Friday 31 July 2015 0:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Battery Notifications

Post 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.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Battery Notifications

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
getSurreal
Posts: 19
Joined: Friday 31 July 2015 0:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Battery Notifications

Post 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.
getSurreal
Posts: 19
Joined: Friday 31 July 2015 0:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Battery Notifications

Post 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.
bimse
Posts: 21
Joined: Sunday 05 February 2017 12:09
Target OS: Raspberry Pi / ODroid
Domoticz version: Model 3
Contact:

Re: Battery Notifications

Post 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
Translated with google
getSurreal
Posts: 19
Joined: Friday 31 July 2015 0:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Battery Notifications

Post 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.
bimse
Posts: 21
Joined: Sunday 05 February 2017 12:09
Target OS: Raspberry Pi / ODroid
Domoticz version: Model 3
Contact:

Re: Battery Notifications

Post by bimse »

without password it works
thanks
Translated with google
Derik
Posts: 1602
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Battery Notifications

Post by Derik »

Is this working for all sensors? Zwave include?
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
L2v2P
Posts: 19
Joined: Sunday 29 November 2015 9:45
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Battery Notifications

Post 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?
L2v2P
Posts: 19
Joined: Sunday 29 November 2015 9:45
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Battery Notifications

Post 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
CLEMENT99
Posts: 36
Joined: Friday 26 January 2018 9:18
Target OS: Windows
Domoticz version: BETA
Location: Brussels
Contact:

Re: Battery Notifications

Post 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 5983 times
CLEMENT99
Posts: 36
Joined: Friday 26 January 2018 9:18
Target OS: Windows
Domoticz version: BETA
Location: Brussels
Contact:

Re: Battery Notifications

Post 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. :(
CLEMENT99
Posts: 36
Joined: Friday 26 January 2018 9:18
Target OS: Windows
Domoticz version: BETA
Location: Brussels
Contact:

Re: Battery Notifications

Post 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:
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest