Deduce the sleep of tenants in a home automation with Home Assistant

5 minutes of reading
PURPOSES OF PROJET:
  • Use an approdeductive system to determine the state of sleep of the tenants of an environment
  • Difficulty level: bass
  • Cost: did
CONCEPTS FACED:
SOFTWARE COMPONENTS USED:
PHYSICAL DEVICES USED:
  • The device in which it is running Home Assistant, in addition, of course, to the household appliance to be registered and the home automation sensor necessary for determining the state (explained in projet)
  • Various (see projet)
PROJET MOST INDICATED FOR:

All environments

Notes and disclaimer
  • qualsiasi modifica all'impianto elettrico dev'essere effettuata da personale qualificato;
  • any changes implemented in probefore is a propersonal responsibility as well as a profirst risk and danger (this guide is for educational purposes only);
  • any changes implemented in probefore a device will void the warranty.
revision projet: 1.0

Abstract

domire smallIt might seem like a jokebut it is not: to deduce the state of falling asleep of a family through personal domotics is anything but superfluous.

Automate the behavior of a home with personal home automation with respect to the presence or absence of tenants it's not particularly difficult, especially when used personal HUBlike Home Assistant, Homey or ecosystems like Appthe HomeKits. Usually it is sufficient to configure the related one app management on smartphones of the whole family to ensure that, at the exit of all the elements (or even the return of only one member), the home automation provveda to perform actions (Default) in full autonomy.

The sleep phase is one of the most important of the day: rest, as is known, is sacred. Knowing this phase from the point of view of personal domotics, as mentioned above, can be particularly useful to activate important automatisms during the night, as for example the armo of the system ofalarm in "presence" mode (ie to activate only the monitoring of the opening of the gates); also the automatic closing of the water and gas supply can be useful, as well as the switching off of entire sections of the electrical system not in use during the night.

This projet is intended to define, at Home Assistant, a type entity "Binary template Sensor”Which determines, empirically, when it is possible to consider the family "sleeping" (state "ON") And when not (state"OFF“).
Finally we will see how to use in the field automation this type of sensor.

Nb This projet has no presumption to resolve the issue, but only to trigger the reasonnamein full educational perspective. It is in fact evident that the specificity of each house, of each family, influences the resolution of the proBlema.

Analysis

As anticipated, the idea is to define a "Binary Template Sensor"Which, based on the empirical observation of one or more metrics, determines whether the family is sleeping or not.

First, it is necessary to assume the existence of a type entity group which rappwith the logical grouping of the "Device Tracker“, Which individually rappfamily members resent. Regardless of the platforms used to define these trackers, we will assume that this group is called "group.famiglia", Which can assume (automatically, by nature) the values"home" and "not_home"(Or" at home "and" away from home "). The state "home"Is present when at least one element of the family results in the house, while “not_home" when all the items are away from home.

Following we will see various techniques to establish more or less effectively the phase of sleep or waking.

Sensore

Defining is a "Binary Template" Sensor"Is rather elementary. It is in fact sufficient to introduce in configuration a block of this type:

binary_sensor:
  - platform: template
    sensors:
      sonno_famiglia:
        value_template: ""

The field "value_template"Is the author of the state that, over time, the sensor will assume.
If up Home Assistant there was a light called "light.tavolo”And we realized a sensor as follows:

binary_sensor:
  - platform: template
    sensors:
      stato_luce:
        value_template: "{{ is_state('light.tavolo','on') }} "

the consequential sensor "binary_sensor.stato_luce"Would present state"on"Whenever the light is turned on and"off"On the contrary, when turned off. In essence, when the condition indicated in the field "value_template"The status of the" Binary Template is true Sensor"Takes on value"on"

What we will do then is to define "value_template”Who evaluate the presence status of the family e a series of other states.

Based on time

The simplest of choices, but also the most inaccurate. So let's assume that the family, in a certain time slot (let's say from 1 to 7 in the morning), if present at home is sleeping.

The configuration could be the following:

binary_sensor:
  - platform: template
    sensors:
      sonno_famiglia:
        value_template:  >
          "{
          is_state("group.famiglia","home")
          and
          (now().hour >1 and now().hour <7)
          }"

since the condition "value_template"Will be true only in the presence of the family and between one and seven in that case"binary_sensor.sonno_famiglia"Would assume status"on"

According to the lights

Unless they are vampires, they usually sleep at night with the lights off.
We remember the (automatic) presence of the group "group.all_lights"That rappsents, appgrease, all lights integrated a Home Assistant: this group assumes status "off"When all the lights are off.

Going to vary the above sensor, we also add this condition:

binary_sensor:
  - platform: template
    sensors:
      sonno_famiglia:
        value_template:  >
          "{
          is_state("group.famiglia","home")
          and
          (now().hour >1 and now().hour <7)
          and
          is_state("group.all_light","off")
          }"

the three conditions are all in AND, therefore the overall condition of the sensor becomes true (and therefore the sensor “on") Only between one and seven o'clock, if the family at home and the lights are off.

Based on the noise

Finally, if we have a noise sensor (for example, a Broadlink A1 e-Air or, better yet, a Minut Point), we can use this feature to define the state of rest.

In the second case (Minut Point) we would have available not a generic indication (with Broadlink A1 a fairly coarse reading is obtained, but still valid) but a punctual: the number of dB detected in the room in which the sensor is located. After have it integrated and after a necessary period of empirical observation (able to understand what is the level of nocturnal noise) we could in fact define a sensor that, evaluating also (or only) the number of decibels, determines whether we are sleeping or not.

Taking the sensor for granted "sensor.minut_sound_level”(Deriving from theintegration of a Minut Point), we still modify the above configuration:

binary_sensor:
  - platform: template
    sensors:
      sonno_famiglia:
        value_template:  >
          "{
          is_state("group.famiglia","home")
          and
          (now().hour >1 and now().hour <7)
          and
          is_state("group.all_light","off")
          and
          (states('sensor.minut_sound_level')|int < 30)
          }"

In this example the "Binary Template Sensor"Will assume state"on"Only between one and seven at night, with the lights off, a noise below 30dB and the family in the house.

Use of the sensor

Modeled our sensor "binary_sensor.sonno_famiglia" is profor use - typically in the field automation.

The first example is that of the use as trigger to trigger nighttime activities. In fact, let's say we want to shut off water and gas (we assume the presence of the switches "switch.acqua" and "switch.gas") And want to activate an alarm integrated with Home Assistant (entity "alarm_control_panel.alarm") In" night armed "mode:

automation:
  trigger:
    platform: state
    entity_id: binary_sensor.sonno_famiglia
    to: 'on'
    for:
      minutes: 30
  condition: []
  action:
    - service: alarm_control_panel.alarm_armed_night
      data:
        entity_id: alarm_control_panel.allarme
        code: 1234 #codice personale
    - service: switch.turn_on
      entity: switch.acqua, switch.gas

Note the use of "forum" in the section trigger: since "binary_sensor.sonno_famiglia"By its very nature it is subject to state fluctuations, the use of"forum"Guarantees that its status is stable for at least 30 minutes before triggering automation.

Similarly, "binary_sensor.sonno_famiglia”Can be used as a conditional element both in automations than in other areas (for example in the script).

In the following automation:

automation:
  trigger: [] #indicare uno o più trigger
  condition:
    condition: state
    entity_id: binary_sensor.sonno_famiglia
    state: 'on'
  action: [] #indicare una o più action

what is the trigger, action (which in turn) is performed only in the presence of state "on"Of the sensor"binary_sensor.sonno_famiglia", So if the family is asleep. Vice versa, setting the value to “off", The execution is guaranteed only in a state of wakefulness.

For the bravest

We advise the most daring to see of the Bayesian sensor available Home Assistant: it allows you to applicare il Bayes theorem for the calculation of prothe possibility of events occurring. This theorem - through this platform - can be appused to determine the propossibility that the family has gone to sleep, in addition to the above considerations.


Home Assistant Official LogoATTENZIONE: remember that there is on our FORUM community an ad hoc section dedicated to Home Assistant, for any doubt, question, information on the specific merit of these components.


Please comment below