Time observation for bayesian binary sensor

I’m trying to configure a bayesian binary sensor for detecting when we’re all sleeping.

- platform: bayesian
  prior: 0.3
  probability_threshold: 0.8
  name: 'sleep_mode'
  observations:
    - entity_id: light.living_room_lamp
      prob_given_true: 0.6
      prob_given_false: 0.3
      platform: 'state'
      to_state: 'off'
    - entity_id: binary_sensor.balcony_door
      prob_given_true: 0.6
      prob_given_false: 0.3
      platform: 'state'
      to_state: 'off'
    - entity_id: sensor.master_bedroom_multi_sensor_luminance
      prob_given_true: 0.6
      prob_given_false: 0.2
      platform: numeric_state
      below: 40
    - entity_id: sensor.livingroom_sensor_motion
      prob_given_true: 0.6
      prob_given_false: 0.2
      platform: state
      to_state: '0'

I would like to add a time based observation to the sensor. It’s very unlikely that we are all asleep before 21:00 so it would be helpful if I can add that. However, I’m not sure if this is possible? Could anyone help me out?

Thanks!

Were you able to figure this out? I’m looking to do the same thing

@ardevd there is a quite long thread on bayesian sensor covering your point, please check it out

First you need a time sensor if you haven’t already configured one.

  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'

Second, create a sensor to distinguish between night and day.
The below sensor will show ‘night’ between 5:00 and 21:00 and day the rest of the time.

  - platform: template
    sensors:
      night_sensor:
        value_template: >-
            {% if strptime(states('sensor.time'), '%H:%M').hour<5 or strptime(states('sensor.time'), '%H:%M').hour>=21 %}
            night
            {% else %}
            day
            {% endif %}

Third, add this to your bayesian sensor:

    - entity_id: 'sensor.night_sensor'
      prob_given_true: 0.73
      prob_given_false: 0.12
      platform: 'state'
      to_state: 'night'
1 Like

You can now also use the new ‘schedule’ helper. Very easy!

2 Likes

@iverlaek thanks for this!