Hours in Bed Sensor

Post Reply
ben53252642
Posts: 477
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Hours in Bed Sensor

Post by ben53252642 » Tuesday 17 January 2017 8:28

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 2393 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 2375 times

User avatar
Egregius
Posts: 2714
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Hours in Bed Sensor

Post by Egregius » Tuesday 17 January 2017 10:09

I wish I had time to spent 12 hours in bed :P

ben53252642
Posts: 477
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Hours in Bed Sensor

Post by ben53252642 » Tuesday 17 January 2017 10:10

Lol I was up all night creating this script...

That was a recovery sleep. 8-)

franzelare
Posts: 148
Joined: Thursday 19 February 2015 22:48
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Hours in Bed Sensor

Post by franzelare » Tuesday 17 January 2017 10:26

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!

ben53252642
Posts: 477
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Hours in Bed Sensor

Post by ben53252642 » Tuesday 17 January 2017 10:51

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.

korniza
Posts: 198
Joined: Thursday 27 August 2015 18:12
Target OS: Raspberry Pi
Domoticz version: V3.6028
Location: Greece
Contact:

Re: Hours in Bed Sensor

Post by korniza » Wednesday 18 January 2017 0:34

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.
>>>> Google Home <<<<<
SBC: Odroid XU4 * Raspberry Pi2 * banana Pi v1
Peripherals: rfxtrx433E, aeon z-stick gen5, bluetooth dongles
Extended Software packages: Xeoma (video NVR), FHEM (extra home automation software)

Wilco
Posts: 18
Joined: Monday 20 March 2017 16:38
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: Zwolle, Netherlands
Contact:

Re: Hours in Bed Sensor

Post by Wilco » Tuesday 17 April 2018 8:50

Hello Ben,

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

Kind regards,
Wilco

ben53252642
Posts: 477
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Hours in Bed Sensor

Post by ben53252642 » Tuesday 17 April 2018 9:51

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 1133 times
Hope this helps.

Wilco
Posts: 18
Joined: Monday 20 March 2017 16:38
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: Zwolle, Netherlands
Contact:

Re: Hours in Bed Sensor

Post by Wilco » Tuesday 17 April 2018 17:09

Thanx Ben,

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

Wilco

ben53252642
Posts: 477
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Hours in Bed Sensor

Post by ben53252642 » Tuesday 17 April 2018 22:34

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.

emeyedeejay
Posts: 19
Joined: Sunday 25 March 2018 12:56
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Hours in Bed Sensor

Post by emeyedeejay » Wednesday 27 June 2018 16:44

This looks cool ... does anybody know if it's possible to make the graph a bar graph?

Post Reply

Who is online

Users browsing this forum: ben53252642 and 2 guests