Hi,
So the code has evolved quite a lot over the last few days and may not be that relevant to my initial question but I wanted to share it here just in case anyone else would like to use any of it. Feel free to ask any questions and suggest amendments (its a bit over engineered).
Broadly speaking the purpose of the code is to do three things:
1. Turn on the lights when motion is detected (setting a count down timer)
2. Allow for different scenes to be executed at different times of the day
3. Reduce the countdown timer of other rooms in the house (on the basis that if you are in the living room, you probably aren't in the conservatory) This bit probably needs a bit more thought and could be cleverer
In addition to this there are three general categories of activation:
1. Motion detected when we are out
2. Motion detected when we are in and its dark
3. Motion detected when we are in, its dark and nightmode is on (this is a dummy switch to indicate that we are in bed).
In situations 1 and 3 the activated scene is hard coded, in the 2 situation the scene is read from a variable. The additional benefit of the variable is that I'm able to stop the motion sensors adjusting the scenes by having a 'none' scene. This means that if I want to show off my hue lights to visitors from the phone app or make use of scenes not in domoticz I dont have domoticz fighting me.
In addition to the code below there are two other scripts - one that counts down the countdown variables, and one that chnages the scene variable depending on time of day.
Thanks again for your help.
Dan
Code: Select all
--**********************************************************************************
-- script_device_MotionTriggered
-- PURPOSE: Tell all motion sensors when to turn on. The countdown script turns them off.
--**********************************************************************************
--**********************************************************************************
-- timedifference function
--**********************************************************************************
function timedifference (s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = os.difftime (t1, t2)
return difference
end
--**********************************************************************************
-- Motion Function (applies to all rooms)
--**********************************************************************************
function motion (room_name, countdown_name)
--set the full scene name by adding room name to the active_scene
scene_with_room = "Scene:" .. room_name .. " Lights " .. active_scene
--whatever the house status tell people motion was sensed.
print("script_device_MotionTriggered: " .. room_name .. " motion triggered/active")
--Nobody home (so basically someone has broken in)
if otherdevices['Someone Home'] == 'Off' then
-- send a notification
commandArray['SendNotification']='Motion#Motion in ' .. room_name .. ' when there should not be#0'
--turn on the LimitLess LED hub (probably not off, but just in case)
commandArray['Limitless Plug (LRF 1,01)']='On'
--turn the lights on bright red
commandArray["Scene:" .. room_name .. " Lights Red"]='On'
--set the off timer to 3 minutes
commandArray["Variable:" .. countdown_name]= tostring(3)
--set the other lights timers to 3 minutes
set_countdown(countdown_name, 3)
--Somebody home, its dark, but nightmode is not active (normal evening use)
elseif otherdevices['Someone Home'] == 'On' and otherdevices['Night Mode'] == 'Off' and uservariables["day_night"] == 'night' then
--Log the action to be performed
print('script_device_MotionTriggered: Activing ' .. scene_with_room)
--turn on the lights
commandArray[scene_with_room]='On'
--set the livingroom off timer to 60 minutes
commandArray["Variable:" .. countdown_name]= tostring(60)
--set the other lights timers to 15 minutes
set_countdown(countdown_name, 15)
--Someone home, its dark and nightmode is active
elseif otherdevices['Someone Home'] == 'On' and otherdevices['Night Mode'] == 'On' and uservariables["day_night"] == 'night' then
--turn the lights on in Nightlight mode
commandArray["Scene:" .. room_name .. " Lights NightLight"]='On'
--set the off timer to 3 minutes
commandArray["Variable:" .. countdown_name]= tostring(3)
--set the other lights timers to 3 minutes
set_countdown(countdown_name, 3)
end
end
--**********************************************************************************
--COUNTDOWN REDUCTION FUNCTION
--when the motion sensor is triggered this not only means that we are entering a room, but also that we are leaving another one. We therefore reduce countdown timers for every other room
--**********************************************************************************
function set_countdown(countdown_name,countdown)
--start with a list of rooms/countdown variables
rooms = {'livingroom_countdown', 'conservatory_countdown'}
for i, room in ipairs(rooms) do
if room == countdown_name then
--This is the active room, we dont want to do anything
else
--This is not the active room, we want to reduce the countdown
print ("script_device_MotionTriggered: Setting " .. room .." to " .. countdown)
if ( uservariables[room] > countdown) then
commandArray['Variable:'.. room]= tostring(countdown)
end
end
end
end
--**********************************************************************************
-- START UP CODE
--**********************************************************************************
--Initilise a command array
commandArray={}
--Make a log entry to show the script is running
print('script_device_MotionTriggered: Script running')
--get the active scene from the variable house_scene. This is set eleswhere in a timer script. All the scenes in the house has the format room_name Lights active_scene
-- EG Living Room Lights Bright
active_scene = uservariables["house_scene"]
--**********************************************************************************
-- LIVING ROOM
--**********************************************************************************
if devicechanged['Living Room Motion Sensor']=='On' or devicechanged['Living Room Motion Sensor 2']=='On' or otherdevices['Living Room Motion Sensor']=='On' or otherdevices['Living Room Motion Sensor 2']=='On' then
--set the room name
room_name = 'Living Room'
--set the coundown_variable
countdown_name = 'livingroom_countdown'
motion(room_name,countdown_name)
end
--**********************************************************************************
-- CONSERVATORY
--**********************************************************************************
if devicechanged['Conservatory Motion Sensor']=='On' or otherdevices['Conservatory Motion Sensor']=='On' then
--set the room name
room_name = 'Conservatory'
--set the coundown_variable
countdown_name = 'conservatory_countdown'
motion(room_name,countdown_name)
end
--**********************************************************************************
-- SET NOBODY HOME AFTER A THE HOUSE HAS BEEN EMPTY 24 HOURS
--**********************************************************************************
if timedifference(otherdevices_lastupdate['Living Room Motion Sensor']) > 86400 then
print('script_device_MotionTriggered: The motion sensor was last triggered 24 hours ago, switching to someone out')
if otherdevices['Someone Home'] == 'On' then
commandArray['SendNotification']='Nobody Home#No movement for 24 hours - predicting that someone is out and adjusting the house appropriately#0'
commandArray['Someone Home']='Off'
end
end
return commandArray