Battery level check

Please use template to report bugs and problems. Post here your questions when not sure where else to post
Only for bugs in the Domoticz application! other problems go in different subforums!
Forum rules
Before posting here, make sure you are on the latest Beta or Stable version.
If you have problems related to the web gui, clear your browser cache + appcache first.

Use the following template when posting here:

Version: xxxx
Platform: xxxx
Plugin/Hardware: xxxx
Description:
.....

If you are having problems with scripts/blockly, always post the script (in a spoiler or code tag) or screenshots of your blockly

If you are replying, please do not quote images/code from the first post
Post Reply
pgas37
Posts: 25
Joined: Wednesday 06 December 2017 20:44
Target OS: -
Domoticz version:
Contact:

Battery level check

Post by pgas37 » Friday 15 December 2017 16:30

version: 3.8153
platform: raspberrypi V3. Raspbian-Jesse
RFXcom433e


I copied the script (domoticz wiki/ battery level check Alt. in More/options/Events and adjusted the user configuration (also report time)
Battery Treshold set to 100 to get a report.( see below)
This script failed. No output, no email
Did i missed something?

To get at least a list of all devices i isolated the part __ GET a list of all devices__ and runned that part with an odd output:
==
----------------092080822165192926933052
Content-type: text/plain; charset=utf-8; format=flowed
Content-transfer-encoding: 7bit
=== I do not understand that output.

Can anyone help??

Thanks
Paul


====
-- =========================================
-- Check battery level for all used devices
-- =========================================

-- User Configuration
BatteryThreshold = 100
WeeklySummary = true
SummaryDay = 6 -- Sunday is 1
EmailTo = "[email protected]"
ReportHour =15
ReportMinute = 05
Domoticz = "192.168.178.59"
DomoticzPort = "8080"
Message = ''

json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- linux
--json = (loadfile "C:\\Domoticz\\scripts\\lua\\json.lua")() -- windows
commandArray = {}
time = os.date("*t")

-- Weekly Device Battery Summary
if WeeklySummary == true and time.wday == SummaryDay and time.hour == ReportHour and time.min == ReportMinute then

-- Get a list of all devices
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 < 100 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
commandArray['SendEmail']='Domoticz Battery Levels#'.. Message .. '#' .. EmailTo

-- Daily Low Battery Report
elseif time.hour == ReportHour and time.min == ReportMinute then

-- Get a list of all devices
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
commandArray['SendEmail']='Domoticz Battery Levels#'.. Message .. '#' .. EmailTo

end

return commandArray

====================

SweetPants
Posts: 1751
Joined: Friday 12 July 2013 21:24
Target OS: Linux
Domoticz version: V3.8820
Location: The Netherlands
Contact:

Re: Battery level check

Post by SweetPants » Saturday 16 December 2017 18:02

Please use code sections when posting code/scripts, makes it much easier to read.
I use this script using dzVentz and telegram to report all devices with low battery, but you can easily adapt it to send emails.

Code: Select all

local BATTERY_THRESHOLD = 20

return {
	active = true,
	on = {
		timer = { 'every hour' }
	},
	
	execute = function(domoticz)
	    
	local function telegram(msg)
            domoticz.openURL('https://api.telegram.org/<yourbot>/sendMessage?chat_id=<your chat_id>&text=' .. msg)
        end

        domoticz.log('Low battery warning fired:')
		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 battery (' .. tostring(lowDevice.batteryLevel) .. '), '

		end)

		if (message ~= '') then
			telegram(message)
		end
	end
}
PS: I'm using latest Beta with dzVentz 2.3.0
Ubuntu 16.04.3 LTS on Intel NUC, MySensors 2.2.0, Pro Mini 3.3 8M with RFM69, ESP8266 (SDK2.1.0), Sonoff, RFLink, RFXcom, MQTT, NodeRed

pgas37
Posts: 25
Joined: Wednesday 06 December 2017 20:44
Target OS: -
Domoticz version:
Contact:

Re: Battery level check

Post by pgas37 » Saturday 16 December 2017 18:32

Thanks for your advice. I did not know. Very new here.
i will look into your script. Sorry, but what is “ telegram”?

I did some test with the script from wiki and published here;
When i set the batterythreshold above the measured level by domoticz then the script sends a correct readable emailmessage!
Otherwise i get the unreadable output.
Maybe someone else can make sense of this.

Regard
Paul

SweetPants
Posts: 1751
Joined: Friday 12 July 2013 21:24
Target OS: Linux
Domoticz version: V3.8820
Location: The Netherlands
Contact:

Re: Battery level check

Post by SweetPants » Saturday 16 December 2017 18:52

pgas37 wrote:
Saturday 16 December 2017 18:32
i will look into your script. Sorry, but what is “ telegram”?
Telegram is the Russian version of Whatsapp. Messages are send almost instantly to you phone or PC. Take a look at http://www.domoticz.com/wiki/Telegram_Bot
Ubuntu 16.04.3 LTS on Intel NUC, MySensors 2.2.0, Pro Mini 3.3 8M with RFM69, ESP8266 (SDK2.1.0), Sonoff, RFLink, RFXcom, MQTT, NodeRed

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests