Instructions:
Create a "Custom Sensor" virtual sensor called "Hours in Bed" with Axis Label: "Hours", you can set your own icon if you like.
Go into events and create a new Lua event named "Hours in Bed" type of event is "Time"
Paste in the code, edit as needed and save. You can check the Domoticz "Status" log tab to see when it is activated, you will see printed something similar to this:
Code: Select all
LUA: Added one minute to time in bed counter. Hours: 12.383333333399
Code: Select all
commandArray = {}
time = os.date("*t")
counter = otherdevices_svalues['Hours in Bed']
counter = tonumber(counter)
-- Compare time difference between main entrance door and main bedroom motion sensors to see which was most recently activated
-- If the bedroom motion sensor was most recently seen by Domoticz, we can determine that someone is in the bedroom.
t1 = os.time()
s1 = otherdevices_lastupdate['Main Entrance Door Sensor']
year1 = string.sub(s1, 1, 4)
month1 = string.sub(s1, 6, 7)
day1 = string.sub(s1, 9, 10)
hour1 = string.sub(s1, 12, 13)
minutes1 = string.sub(s1, 15, 16)
seconds1 = string.sub(s1, 18, 19)
t2 = os.time{year=year1, month=month1, day=day1, hour=hour1, min=minutes1, sec=seconds1}
difference1 = (os.difftime (t1, t2))
s2 = otherdevices_lastupdate['Main Bedroom Entrance Motion']
year2 = string.sub(s2, 1, 4)
month2 = string.sub(s2, 6, 7)
day2 = string.sub(s2, 9, 10)
hour2 = string.sub(s2, 12, 13)
minutes2 = string.sub(s2, 15, 16)
seconds2 = string.sub(s2, 18, 19)
t3 = os.time{year=year2, month=month2, day=day2, hour=hour2, min=minutes2, sec=seconds2}
difference2 = (os.difftime (t1, t3))
-- Reset hours in bed counter at midnight
if (time.hour == 0) and (time.min == 00) then
counter = 0
commandArray['UpdateDevice'] = otherdevices_idx['Hours in Bed'] .. '|0|' .. tonumber(counter)
end
-- Check if in bed and update counter if true
-- First we check if my phone is present, then that the main bedroom motion sensor was the last seen by Domoticz
-- Finally we check if all the relevant lights and devices are turned off to make the determination that I am in bed.
if (otherdevices["iPhone Ben"] == 'On' and difference1 > difference2 and otherdevices["Main Bedroom"] == 'Off' and otherdevices["Main Bedroom Hallway"] == 'Off' and otherdevices["Main Bathroom"] == 'Off' and otherdevices["Main Entrance Hallway"] == 'Off' and otherdevices["Kitchen"] == 'Off') then
counter = counter + 0.016666666666667
commandArray['UpdateDevice'] = otherdevices_idx['Hours in Bed'] .. '|0|' .. tonumber(counter)
print ('Added one minute to time in bed counter. Hours: ' .. counter .. '')
end
return commandArray
