Scripts for my smart home setup.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
3.4 KiB

return {
on = {
devices = {
'Bewegung_Wohnzimmer',
'Lichtstaerke_Wohnzimmer',
'Steckdose_TV'
}
},
data = {
auto_on = { initial = 0 }, -- Flag to indicate automatic contro.
auto_off_count = { initial = 0 }, -- Retry counter.
turn_off_time = { initial = 0 },
old_state = { initial = 'Off' },
light_low = { initial = 0 }
},
execute = function(domoticz, item)
local lvl_low = 20 -- Luminosity level at which the light is turned on.
local lvl_high = 60 -- Luminosity level at which the light is turned off.
local update_time_max = 30 -- Time to wait after the movement was detected the last time.
local auto_off_max = 1 -- Maximum number of turn off attempts.
no_active_time = 'at 22:00-06:00'
current_state = domoticz.devices('Bewegung_Wohnzimmer').state
trigger_time = domoticz.time
-- Flip/Flop low light.
if (domoticz.devices('Lichtstaerke_Wohnzimmer').lux <= lvl_low) then
domoticz.data.light_low = 1
elseif (domoticz.devices('Lichtstaerke_Wohnzimmer').lux > lvl_high) then
domoticz.data.light_low = 0
end
-- Save the time, when the motion sensor turned off.
if (domoticz.data.turn_off_time == 0 or (current_state == 'Off' and domoticz.data.old_state == 'On')) then
domoticz.data.turn_off_time = domoticz.time
end
if (domoticz.data.light_low == 1) then
if (current_state == 'On' and domoticz.data.auto_on == 0 and domoticz.time.matchesRule('at nighttime') and not domoticz.time.matchesRule(no_active_time)) then
domoticz.log('Lichtstärke im Wohnzimmer ist niedrig, schalte das Licht ein.', domoticz.LOG_INFO)
domoticz.devices('Licht_Wohnzimmer').switchOn()
domoticz.data.auto_on = 1
elseif (current_state == 'Off' and domoticz.data.auto_on == 1) then
if (domoticz.data.auto_off_count < auto_off_max and domoticz.time.compare(domoticz.data.turn_off_time).mins > update_time_max) then
domoticz.log('Niemand im Wohnzimmer, schalte das Licht aus.', domoticz.LOG_INFO)
domoticz.devices('Licht_Wohnzimmer').switchOff()
domoticz.data.auto_on = 0
domoticz.data.auto_off_count = domoticz.data.auto_off_count + 1
elseif (domoticz.data.auto_off_count >= auto_off_max) then
domoticz.log('Automatisches Abschalten nach Bewegungserkennung ist ausgesetzt.', domoticz.LOG_INFO)
end
end
else
if (domoticz.devices('Licht_Wohnzimmer').state == 'On') then
domoticz.log('Lichtstärke im Wohnzimmer ist hoch, schalte das Licht aus.')
domoticz.devices('Licht_Wohnzimmer').switchOff()
end
domoticz.data.auto_on = 0
domoticz.data.auto_off_count = 0 -- Reset the counter
end
-- When the TV is on, let's switch to the smaller lamp.
if (domoticz.devices('Licht_Wohnzimmer').state == 'On' and domoticz.devices('Steckdose_TV').state == 'On' and domoticz.devices('Licht_Lampe').state == 'Off') then
domoticz.log('TV eingeschaltet, schalte auf die kleine Lampe um.', domoticz.LOG_INFO)
domoticz.devices('Licht_Lampe').switchOn()
domoticz.devices('Licht_Wohnzimmer').switchOff()
end
domoticz.data.old_state = current_state
end
}