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.

113 lines
5.3 KiB

return {
on = {
devices = {
'Rockrobo_Status',
'Rockrobo_Staubbaehelter_leeren',
'Rockrobo_letzte_Reinigung'
},
timer = {
'every 10 minutes'
}
},
data = {
old_state = { initial = 'Undefined' },
old_cleaning_start = { initial = 0 },
old_dust_bin = { initial = 0 },
auto_on = { initial = 0 }, -- Flag to indicate automatic control.
auto_on_time = { initial = 0 } -- Time when the last cleaning was done.
},
execute = function(domoticz, item)
local noonehome_timemin = 60 -- Start cleaning when this time is passed since motion detector turned off.
local noonehome_timemax = 2880 -- If this time passed since the motion was detected the last time, do not clean any more. Usecase: vacation.
local last_cleaning_time = 1440 -- Minimum time between cleanings.
local cleaning_mins_max = 300 -- Cumulated cleaning time to clean the vacuum cleaner.
local lmt_max = 90 -- Level at which the notification to clean the vacuum cleaner is sent.
local active_time = 'at 08:00-20:00' -- What is time of day in which the cleaning can be started.
local last_auto_on_time = 60 -- Try again to clean if at least this time has passed since the last attempt.
current_state = tostring(domoticz.devices('Rockrobo_Status').rawData[1])
local Time = require('Time')
cleaning_start = Time(domoticz.devices('Rockrobo_letzte_Reinigung').text)
current_dust_bin = domoticz.devices('Rockrobo_Staubbehaelter').percentage
-- Initialise time.
if (domoticz.data.auto_on_time == 0) then
domoticz.data.auto_on_time = domoticz.time
end
if (domoticz.data.old_cleaning_start == 0) then
domoticz.data.old_cleaning_start = domoticz.time
end
-- Start the cleaning if no one is at home.
if (domoticz.devices('Bewegung_Wohnzimmer').state == 'Off' and domoticz.devices('Bewegung_Wohnzimmer').lastUpdate.minutesAgo > noonehome_timemin and domoticz.devices('Bewegung_Wohnzimmer').lastUpdate.minutesAgo < noonehome_timemax) then
-- Check if preconditions are ok.
if (domoticz.time.compare(cleaning_start).mins >= last_cleaning_time and domoticz.time.matchesRule(active_time) and domoticz.data.auto_on == 0 and domoticz.time.compare(domoticz.data.auto_on_time).mins >= last_auto_on_time) then
if (current_state ~= 'Cleaning' and current_state ~= 'Zoned cleaning' and current_state ~= 'Error' and current_state ~= 'Care' and current_dust_bin < 100) then
domoticz.log('Rockrobo starte Reinigung.')
domoticz.devices('Rockrobo').switchSelector(30) -- 'Zone'
domoticz.data.auto_on = 1
domoticz.data.auto_on_time = domoticz.time
end
end
elseif (domoticz.devices('Bewegung_Wohnzimmer').state == 'Off' and domoticz.devices('Bewegung_Wohnzimmer').lastUpdate.minutesAgo >= noonehome_timemax) then
domoticz.log('Rockrobo Reinigung ausgesetzt, da lange keiner zu Hause war.')
end
-- Cleaning is finished.
if (current_state ~= 'Cleaning' and current_state ~= 'Zoned cleaning' and (domoticz.data.old_state == 'Cleaning' or domoticz.data.old_state == 'Zoned cleaning')) then
domoticz.log('Reinigung abgeschlossen.', domoticz.LOG_INFO)
domoticz.data.auto_on = 0
domoticz.data.auto_on_time = domoticz.time
end
-- Estimation how full is the dust bin.
if (cleaning_start.compare(domoticz.data.old_cleaning_start).mins > 1) then
current_dust_bin = math.min(100, (current_dust_bin + math.ceil(100*tonumber(domoticz.devices('Rockrobo_Dauer_letzte_Reinigung_min').state)/cleaning_mins_max))) -- Round up and limit.
domoticz.devices('Rockrobo_Staubbehaelter').updatePercentage(current_dust_bin)
end
-- Reset the dust bin values.
if (domoticz.devices('Rockrobo_Staubbaehelter_leeren').changed) then
domoticz.log('Setze die Staubbehälterwartung zurück.')
current_dust_bin = 0
domoticz.devices('Rockrobo_Staubbehaelter').updatePercentage(current_dust_bin)
end
-- Send out the dust bin notification.
if (current_dust_bin > lmt_max and domoticz.data.old_dust_bin < lmt_max) then
domoticz.log('Rockrobo Staubbehälter leeren.', domoticz.LOG_ERROR)
domoticz.email('Rockrobo', 'Rockrobo Staubbehälter leeren.', 'root@homeserver')
end
-- Send out the care notification.
if (domoticz.data.old_state ~= 'Care' and current_state == 'Care') then
domoticz.log('Rockrobo braucht Wartung.', domoticz.LOG_ERROR)
domoticz.email('Rockrobo', 'Rockrobo braucht Wartung.', 'root@homeserver')
end
-- Send out the fault notification.
if (domoticz.data.old_state ~= 'Error' and current_state == 'Error') then
domoticz.log('Rockrobo hat einen Fehler.', domoticz.LOG_ERROR)
domoticz.email('Rockrobo', 'Rockrobo hat einen Fehler.', 'root@homeserver')
end
domoticz.data.old_state = current_state
domoticz.data.old_cleaning_start = cleaning_start
domoticz.data.old_dust_bin = current_dust_bin
end
}