Hi pblacky,
mit dem letzten Beispiel bist Du auf dem richtigen Weg gewesen. Habe noch kleine Anpassungen gemacht. Hier das komplette Script:
--[[
%% autostart
%% properties
%% globals
--]]
-- file: Batteriestatus.lua
-- v.0.02
-- Batteriestatus checken und mailen
-- Copyleft 03-2017 {jeep}
local sourceTrigger = fibaro:getSourceTrigger();
local oldnodeId = nil ;
local subject = "Batterien Status";
local userId = 2
Debug = function ( color, message )
fibaro:debug(string.format('<%s style="color:%s;">%s', "span", color, message, "span"))
end
devices = fibaro:getDevicesId({interfaces = {"battery"}, visible = true, enabled = true})
--print(json.encode(devices))
function checkBatteries()
local status = ' ' ;
local n = 0;
Debug('withe', os.date('%c'));
for id = 1, #devices do
local batteryLevel = fibaro:get(devices[id], 'batteryLevel')
local nodeId = fibaro:get(devices[id], 'nodeId')
if batteryLevel ~= nil and nodeId ~= nil
then
local name = fibaro:getName(devices[id])
if oldnodeId ~= nodeId then
local room = fibaro:getRoomNameByDeviceID(devices[id])
if not(room == "unassigned") then
n = n+1
if tonumber(batteryLevel) >= 50 and tonumber(batteryLevel) <=100 then
Debug('green', "Batterie " ..name..' ('..room..') - Batterie: '..batteryLevel..' % OK')
status = status .. n .. ") " ..name..' ('..room..') Bat.: '..batteryLevel..' % OK\n';
elseif
tonumber(batteryLevel) >= 30 and tonumber(batteryLevel) <= 50 then
Debug('yellow', "Batterie " ..name..' ('..room..') - Batterie: '..batteryLevel..' % Warnung')
status = status .. n .. ") " ..name..' ('..room..') Bat.: '..batteryLevel..' % Warnung\n';
elseif
tonumber(batteryLevel) < 30 then
Debug('red', "Batterie " ..name..' ('..room..') - Batterie: '..batteryLevel..' %')
status = status .. n .. ") " ..name..' ('..room..') Bat.: '..batteryLevel..' % Kritisch\n';
elseif
tonumber(batteryLevel) > 100 then
Debug('red', "Batterie " ..name..' ('..room..') - Batterie: '..batteryLevel..' % vermutlich leer!')
status = status .. n .. ") " ..name..' ('..room..') Bat.: '..batteryLevel..' % Fehler\n';
end
end
end
oldnodeId = nodeId
end
end
status = status .. 'Am :' .. os.date('%c') ..'\n'
status = status .. 'Gecheckt wurden ' ..n.. ' devices.'
Debug('withe', os.date('%c'));
Debug('withe', '------------------------------------------------------------')
Debug('withe', status)
fibaro:call(userId, "sendEmail", subject, status)
--fibaro:call(5, "sendEmail", subject, status)
local status = ' ';
end -- function
function main()
local currentDate = os.date("*t");
local currenthour = string.format("%02d", currentDate.hour) .. ":" .. string.format("%02d", currentDate.min)
local weekday = currentDate.wday
local startSource = fibaro:getSourceTrigger();
if ((weekday == 1) and currenthour == "19:45") then
checkBatteries()
end
setTimeout(main, 60*1000)
end
if (sourceTrigger["type"] == "autostart") then
main()
else
local currentDate = os.date("*t");
local startSource = fibaro:getSourceTrigger();
if (startSource["type"] == "other") then
checkBatteries()
end
end
Versuche mal das, habe es eben getestet
if tonumber(batteryLevel) >= 50 and tonumber(batteryLevel) <=100 then
--Debug('green', "Batterie " ..name..' ('..room..') - Batterie: '..batteryLevel..' % OK')
--status = status .. n .. ") " ..name..' ('..room..') Bat.: '..batteryLevel..' % OK\n';
n = n-1
elseif
und habe dieses Ergebnis bei mir von 22 devices:
[DEBUG] 08:49:48: Wed May 10 08:49:48 2017
[DEBUG] 08:49:48: Batterie Flood1 WK (Heizungsraum) - Batterie: 44 % Warnung
[DEBUG] 08:49:49: Batterie KeyFob1 (Buero) - Batterie: 37 % Warnung
[DEBUG] 08:49:49: Wed May 10 08:49:49 2017
[DEBUG] 08:49:49: ------------------------------------------------------------
[DEBUG] 08:49:49: 1) Flood1 WK (Heizungsraum) Bat.: 44 % Warnung
[DEBUG] 08:49:49: 2) KeyFob1 (Buero) Bat.: 37 % Warnung
[DEBUG] 08:49:49: Am :Wed May 10 08:49:49 2017
[DEBUG] 08:49:49: Gecheckt wurden 2 devices.