The script below works perfectly.
But I want something extra and that is that when a mobile leaves home, a message goes to Telegram with the name and how many mobiles are still at home.
I've tried everything necessary but no good result yet.
I turned off the debug rules because these bits now work fine.
I have set the 6 lines in question to read because this gives error messages.
The 7 are the number of registered cell phones.
I hope this is possible in this script.
Code: Select all
local phones =
{
'Mobiel Fred',
'Mobiel Hanny',
'Mobiel Mariska',
'Mobiel Barney',
'Mobiel Nils',
'Mobiel Test',
'Mobiel Mandy',
'Thuis',
}
return
{
active = true,
on = {
devices = phones
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = 'phones',
},
execute = function(dz, item)
local home = dz.devices('Thuis')
local atHome = home.active
local function phonesActive()
local activePhones = 0
for _, phoneName in ipairs(phones) do
if dz.devices(phoneName).active then
dz.log(phoneName .. ' active', dz.LOG_DEBUG)
activePhones = activePhones + 1
end
end
if (dz.devices('Thuis').state == 'On') then
activePhones = activePhones - 1
end
return activePhones
end
if item.active then
if (dz.devices('Thuis').state == 'Off') then
dz.notify(item.name:gsub('Mobiel ', '') .. ' komt thuis. Er is nu ' .. phonesActive() .. ' mobiele telefoon binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
--dz.log(item.name:gsub('Mobiel ', '') .. ' komt thuis. Er is nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)
else
if phonesActive() > 1 then
dz.notify(item.name:gsub('Mobiel ', '') .. ' komt ook thuis. Er zijn nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
--dz.log(item.name:gsub('Mobiel ', '') .. ' komt ook thuis. Er zijn nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)
home.switchOn().checkFirst().silent()
end
end
--else
--if phonesActive() < 7 then
--dz.notify(item.name:gsub('Mobiel ', '') .. ' vertrekt. Er zijn nu nog' .. phonesActive() .. ' mobiele telefoons binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt nog niet goed
--dz.log(item.name:gsub('Mobiel ', '') .. ' vertrekt.Er zijn nu nog' .. phonesActive() .. ' mobiele telefoons binnen.' dz.LOG_DEBUG)
--end
--end
else
if phonesActive() < 1 then
--dz.log(item.name:gsub('Mobiel ', '') .. ' vertrekt. Er zijn nu geen mobiele telefoons meer binnen.', dz.LOG_DEBUG)
dz.notify('Devices'.. item.name .. ' vertrekt. Er zijn nu geen mobiele telefoons meer binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
home.switchOff().checkFirst().silent()
end
end
if (dz.devices('Thuis').state == 'Off') then
for _, phoneName in ipairs(phones) do
if dz.devices(phoneName).active then
home.switchOn().checkFirst().silent()
end
end
end
end
}Fred