Page 1 of 1

Hours in Bed Sensor

Posted: Tuesday 17 January 2017 7:28
by ben53252642
This is a script I wrote for creating an "Hours in Bed" sensor.

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
There is good explanation of the logic in the code comments.

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
My favourite part is being able to check the Domoticz chart and see what times of day I spend in bed. :D
Screen Shot 2017-01-17 at 5.26.20 pm.png
Screen Shot 2017-01-17 at 5.26.20 pm.png (67.1 KiB) Viewed 3541 times
Screen Shot 2017-01-17 at 6.27.08 pm.png
Screen Shot 2017-01-17 at 6.27.08 pm.png (309.62 KiB) Viewed 3523 times

Re: Hours in Bed Sensor

Posted: Tuesday 17 January 2017 9:09
by Egregius
I wish I had time to spent 12 hours in bed :P

Re: Hours in Bed Sensor

Posted: Tuesday 17 January 2017 9:10
by ben53252642
Lol I was up all night creating this script...

That was a recovery sleep. 8-)

Re: Hours in Bed Sensor

Posted: Tuesday 17 January 2017 9:26
by franzelare
how do you check the bed occupation?
I plan to add this to my bedroom automation:
https://www.mysensors.org/build/bed_occupancy
so the time in bed script is a perfect addition!

Re: Hours in Bed Sensor

Posted: Tuesday 17 January 2017 9:51
by ben53252642
This script uses a process of elimination and checks that can be used to logically conclude that the bed is the only place I can be.

Simple logic for my instance is:

1) Is the bedroom the last motion sensor that was trigger (yes / no)

2) Is my cell phone at home (yes no)

3) Are all the lights turned off (yes / no)

Reason I compare the last triggered times of the main entrance door vs the bedroom motion sensor is that conditions 2 and 3 in my scenario can theoretically be true even if I am at home so step 1 makes the overriding determination of my location in the house and thus the logic works.

Script checks the state of steps 1, 2 and 3 every 1 minute, if they all match (yes) then it concludes I am in bed and updates the sensor.

Re: Hours in Bed Sensor

Posted: Tuesday 17 January 2017 23:34
by korniza
I have an idea how to track sleep and create scenarios on wake up. If we can use a platform from activity trackers (for example fitbit/samsung s healt etc) we can control lights or even the coffee machine to power up on morning wake up.

Re: Hours in Bed Sensor

Posted: Tuesday 17 April 2018 8:50
by Wilco
Hello Ben,

you talk about "(otherdevices["iPhone Ben"] == 'On'", how do you check this ?

Kind regards,
Wilco

Re: Hours in Bed Sensor

Posted: Tuesday 17 April 2018 9:51
by ben53252642
Wilco wrote: Tuesday 17 April 2018 8:50 Hello Ben,

you talk about "(otherdevices["iPhone Ben"] == 'On'", how do you check this ?

Kind regards,
Wilco
I use a presence detection method (there are many available and you may have to try more than one before you find a setup that works reliably). That said in many cases it is not necessary to include checking if a phone is present to determine that one is in bed.

Example:

A house full of motion sensors,
Last motion sensor to be triggered is located in the main bedroom
All lights in the bedroom are turned off and blinds / curtains are closed

I'd argue that is sufficient information to determine that one is in bed and + the time in bed counter.

Here is an alternate line without the phone check.

Code: Select all

if (otherdevices["Main Bedroom"] == 'Off' and difference1 > difference2 and otherdevices["Main Bedroom Hallway"] == 'Off' and otherdevices["Main Bathroom"] == 'Off' and otherdevices["Main Entrance Hallway"] == 'Off' and otherdevices["Kitchen"] == 'Off') then
So far I am pretty happy with my time in bed counter, I've got over a year of stored data, here is the last month. :D

timeinbedmonth.JPG
timeinbedmonth.JPG (100.35 KiB) Viewed 2281 times
Hope this helps.

Re: Hours in Bed Sensor

Posted: Tuesday 17 April 2018 17:09
by Wilco
Thanx Ben,

but i want to detect my iPhone, do you have example code or something like that ?

Wilco

Re: Hours in Bed Sensor

Posted: Tuesday 17 April 2018 22:34
by ben53252642
I used this for a while which worked pretty well

viewtopic.php?f=61&t=13318&start=140#p114396

When I was using it though it required disabling two factor authentication on my Apple account, not sure if that is still the case (suggest reading the thread). I moved to Android about a year ago hence why I can no longer use it.

I'm currently using a custom solution which gets the status of all wifi devices from my multiple WiFi routers and if it can't see the phone it starts scanning for it via bluetooth (via multiple Raspberry Pi's for total coverage).

I see someone has posted something here to get status directly from a WiFi router.

viewtopic.php?t=15531

Personally I like using the router the most since it doesn't cause any additional battery drain on the phone.

My routers are running DD-WRT firmware.

Re: Hours in Bed Sensor

Posted: Wednesday 27 June 2018 16:44
by emeyedeejay
This looks cool ... does anybody know if it's possible to make the graph a bar graph?