Hi,
I am relatively new to Home Assistant and have limited experience in configuration and automation. I created an automation to turn on lights when we arrive home in the evening/night. The automation works as expected, but I’m curious if there is a better way to achieve it or if you have any suggestions.
I have a door sensor and two lights connected with Shelly. This is all the configuration I did:
- I created a sensor to count the number of people at home:
# Conto il numero di persone in casa
- platform: template
sensors:
totale_persone_a_casa:
friendly_name: "Totale Persone a Casa"
value_template: >
{{ expand(states.person)|selectattr('state','equalto','home')|list|length }}
-
Then I created a binary sensor in the helper section named
input_boolean.just_arrived_home
. (I couldn’t do it in YAML, I don’t know why). -
I created an automation that triggers when the sensor value changes. If the value is above 0, the binary sensor is turned on. After a 5 minute delay, the binary sensor is turned off.
- id: '1675622318729'
alias: Nuova Persona a Casa
description: ''
trigger:
- platform: numeric_state
entity_id: sensor.totale_persone_a_casa
above: 0
below: 3
condition: []
action:
- service: input_boolean.turn_on
data: {}
target:
entity_id: input_boolean.appena_tornati_a_casa
- delay:
hours: 0
minutes: 5
seconds: 0
milliseconds: 0
- service: input_boolean.turn_off
data: {}
target:
entity_id: input_boolean.appena_tornati_a_casa
mode: single
- I created a second automation that triggers when the door sensor opens and the sun is down. In the action block, I added an if condition. If the binary sensor is on (indicating that at least one person is home in the last 5 minutes), then the lights are turned on and the binary sensor is turned off. If this automation does not trigger the binary sensor is turned off from the previous one.
- id: '1675621145645'
alias: Accendi Luci Sala se Torniamo a Casa
description: ''
trigger:
- type: opened
platform: device
device_id: 07a48facbc84808c26f39579c1b8a6b9
entity_id: binary_sensor.sensore_ingresso
domain: binary_sensor
condition:
- condition: sun
before: sunrise
after: sunset
action:
- if:
- condition: state
entity_id: input_boolean.appena_tornati_a_casa
state: 'on'
then:
- type: turn_on
device_id: 2ed7dc4da7b29fe7a95bc717786a9086
entity_id: light.luce_sala_tv_2
domain: light
- type: turn_on
device_id: fd8ba9b35970b8d95184e2064f949f64
entity_id: light.luce_sala_ingresso
domain: light
- service: input_boolean.turn_off
data: {}
target:
entity_id: input_boolean.appena_tornati_a_casa
mode: single
Do you have any suggestions?
Thank you!