Programming your own sleeping sensor

Hi everyone,

for a very long time I thought about getting or building a sensor, checking if I am already sleeping or not. Mainly I am using this information for controlling my light automations (bright light during evening and dimmed during night/after falling asleep, light is turned on by motion sensors). Just time-based it isn’t working well because I do not have regular sleeping times, especially on weekend.
Because I was not completely convinced about the solutions I found [and my girlfriend completely not convinced that we do need such a sensor :wink: ] I thought about a software-based solution for that, I would like to present to you. Maybe it is helpful for someone.

First, I have created three helpers (sleeping, sleeping later and sleeping long), both are Booleans and storing the information, I need. The helper sleeping is used for the information “sleeping yes/no” and sleeping later the information if I am awake after midnight. The helper sleeping long is set manually for example on holidays.

Moreover, there are four automations setting the status of my helpers.

Automation — Turning off sleeping
I am using two time triggers; one dynamically storing my wakeup time (can be replaced easily by a fixed time) during the week and another at 9:00 for the weekend/holidays. The automation just turns off the helpers sleep and sleep later.

alias: "Licht: Set helper sleep off"
description: ""
trigger:
  - platform: time
    at: input_datetime.aufstehen
  - platform: time
    at: "09:00:00"
condition: []
action:
  - choose:
      - conditions:
          - condition: time
            after: input_datetime.aufstehen_start
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
          - condition: state
            entity_id: input_boolean.ausschlafen
            state: "off"
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id:
                - input_boolean.schlafen
                - input_boolean.schlafen_spaeter
      - conditions:
          - condition: time
            weekday:
              - sun
              - sat
            after: "08:00:00"
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id:
                - input_boolean.schlafen
                - input_boolean.schlafen_spaeter
      - conditions:
          - condition: time
            after: "08:00:00"
          - condition: state
            entity_id: input_boolean.ausschlafen
            state: "on"
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id:
                - input_boolean.schlafen
                - input_boolean.schlafen_spaeter
    default:
      - stop: ""
mode: single

Automation — Turning on sleeping
Every 15 minutes the automations starts, checking if it is after sunset (with a safety of additional 30 minutes), confirming that someone is at home and if all lights are off for minimum 15 minutes. If yes, I am sleeping and the helper sleeping is set to on.

alias: "Licht: Set helper sleep on"
description: ""
trigger:
  - platform: time_pattern
    minutes: "00"
  - platform: time_pattern
    minutes: "15"
  - platform: time_pattern
    minutes: "30"
  - platform: time_pattern
    minutes: "45"
condition:
  - condition: sun
    after: sunset
    after_offset: "00:30:00"
  - condition: state
    entity_id: group.family
    state: home
    for:
      hours: 0
      minutes: 15
      seconds: 0
  - condition: state
    entity_id: light.alle_lichter
    state: "off"
    for:
      hours: 0
      minutes: 15
      seconds: 0
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.schlafen
mode: single

Automation — sleep later
If it is after midnight and I am still awake, I am using the helper sleeping later for storing this information. Therefore, I am checking at 00:01 o’clock if the helper sleeping still is off and turning on my helper sleeping later.

This seems to be unnecessary, but it is important because I am using the information “after sunset” and right after midnight this changes to false because it is before sunset of the new day.

alias: "Licht: Set helper sleep_spaeter"
description: ""
trigger:
  - platform: time
    at: "00:01:00"
condition:
  - condition: state
    entity_id: input_boolean.schlafen
    state: "off"
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.schlafen_spaeter
mode: single

Automation — Go to bed after midnight
This automation is very similar to the automation for turning on the sleep helper but additionally checking the status of the helper sleeping later and not using the sunset condition.

alias: "Licht: set helper sleep on später"
description: ""
trigger:
  - platform: time_pattern
    minutes: "00"
  - platform: time_pattern
    minutes: "15"
  - platform: time_pattern
    minutes: "30"
  - platform: time_pattern
    minutes: "45"
condition:
  - condition: state
    entity_id: group.family
    state: home
    for:
      hours: 0
      minutes: 15
      seconds: 0
  - condition: state
    entity_id: light.alle_lichter
    state: "off"
    for:
      hours: 0
      minutes: 15
      seconds: 0
  - condition: state
    entity_id: input_boolean.schlafen_spaeter
    state: "on"
  - condition: time
    before: input_datetime.aufstehen
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.schlafen
mode: single

Now, you always can access the information stored in the helper sleeping for all your automations.

I know that this isn’t the best solution but for me it’s working so far pretty well and I do not have any problems. And the best, you do not need any additional sensors and do not need to spend money.

I hope this helps someone and helps to improves some of your automations.

Regards,
Lars

2 Likes