Example script "check battery levels.lua" fails

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Example script "check battery levels.lua" fails

Post by BakSeeDaa »

The example script "check battery levels.lua" fails in dzVents 2.1 with the error message

Code: Select all

check battery levels.lua:28: attempt to concatenate upvalue 'message' (a nil value)
EDIT: Another issue, The script also reports wrong battery levels (0)

Cheers!
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Example script "check battery levels.lua" fails

Post by dannybloe »

Ah, indeed. However it is batteryLevel (lower-case b). But this should be the example:

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 -- level is 0-100
			return (level ~= nil and -- not all devices have this attribute
					level <= BATTERY_THRESHOLD)

		end)

		-- then loop over the results
		lowOnBat.forEach(function(lowDevice)

			message = message .. 'Device ' ..
				lowDevice.name .. ' is low on batteries (' .. tostring(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
}
I have fixed this in 2.2.0.
If you have some time then please can you test this 2.2.0 version (see my other post in the this forum).

Danny
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Example script "check battery levels.lua" fails

Post by BakSeeDaa »

Thanks!

I'm trying your script.

It reports low battery levels (0) for a couple of devices who has plenty of juice left. For example this device:

Code: Select all

{
   "ActTime" : 1501069616,
   "ServerTime" : "2017-07-26 13:46:56",
   "result" : [
      {
         "AddjMulti" : 1.0,
         "AddjMulti2" : 1.0,
         "AddjValue" : 0.0,
         "AddjValue2" : 0.0,
         "BatteryLevel" : 85,
         "CustomImage" : 0,
         "Data" : "Off",
         "Description" : "",
         "Favorite" : 0,
         "HardwareID" : 5,
         "HardwareName" : "MySensors",
         "HardwareType" : "MySensors Gateway USB",
         "HardwareTypeVal" : 41,
         "HaveDimmer" : false,
         "HaveGroupCmd" : false,
         "HaveTimeout" : false,
         "ID" : "00000006",
         "Image" : "Light",
         "IsSubDevice" : false,
         "LastUpdate" : "2017-07-26 13:30:03",
         "Level" : 100,
         "LevelInt" : 100,
         "MaxDimLevel" : 100,
         "Name" : "A580 IP",
         "Notifications" : "false",
         "PlanID" : "0",
         "PlanIDs" : [ 0 ],
         "Protected" : false,
         "ShowNotifications" : true,
         "SignalLevel" : "-",
         "Status" : "Off",
         "StrParam1" : "",
         "StrParam2" : "",
         "SubType" : "AC",
         "SwitchType" : "On/Off",
         "SwitchTypeVal" : 0,
         "Timers" : "false",
         "Type" : "Light/Switch",
         "TypeImg" : "lightbulb",
         "Unit" : 5,
         "Used" : 1,
         "UsedByCamera" : false,
         "XOffset" : "0",
         "YOffset" : "0",
         "idx" : "596"
      }
   ],
   "status" : "OK",
   "title" : "Devices"
}
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Example script "check battery levels.lua" fails

Post by dannybloe »

Can you put dzVents in debug mode logging and then check domoticzData.lua in /path/to/domoticz/scripts/dzVents?
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Example script "check battery levels.lua" fails

Post by BakSeeDaa »

At first I had some MySensors nodes that I needed to recreate because they were inaccessible due to that their type changed.

After replacing them I ran your script and it reported the old devices (that I already deleted), not the new ones that I created.

After setting debug mode the domoticzData.lua was updated with fresh data and the battery levels reported seems OK now.

Maybe you'd need to change this

Code: Select all

if (message) then
to

Code: Select all

if (message ~= '') then
Thanks!
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Example script "check battery levels.lua" fails

Post by dannybloe »

Indeed!
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Example script "check battery levels.lua" fails

Post by BakSeeDaa »

dannybloe wrote:If you have some time then please can you test this 2.2.0 version (see my other post in the this forum).

Danny
Sure thing! I'm still struggling though to migrate all my scripts from 1.0. I will give it a try soon.

Cheers!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest