Automation Turn On light if coming home - Suggestions

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:

  1. 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 }}
  1. 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).

  2. 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
  1. 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!

To determine if someone is home, I simply use the following trigger:-

trigger:
  - platform: state
    entity_id:
      - zone.home

Then in the action section I check whether someone is present or not:-

action:
  - if:
      - condition: numeric_state
        entity_id: zone.home
        above: 0

And equally if no one is home:-

action:
  - if:
      - condition: numeric_state
        entity_id: zone.home
        below: 1

I do this as conditions in the action section rather than in the conditions section after the trigger, because I use the same automation whether someone is arriving or leaving, and then set my heating on/off accordingly.

Here is the full automation:-

alias: Set Tado Home or Away
description: ""
trigger:
  - platform: state
    entity_id:
      - zone.home
action:
  - if:
      - condition: numeric_state
        entity_id: zone.home
        below: 1
    then:
      - device_id: a47f7db07a4a9c7633ac807ab9d13457
        domain: mobile_app
        type: notify
        message: Away Mode Activated
        title: Tado Alert
      - service: script.set_tado_mode_away_and_reset
        data: {}
    else:
      - if:
          - condition: numeric_state
            entity_id: zone.home
            above: 0
            below: 2
          - condition: state
            entity_id: climate.main_house
            attribute: preset_mode
            state: away
        then:
          - device_id: a47f7db07a5a9c7633ac807ab9f33457
            domain: mobile_app
            type: notify
            message: Home Mode Activated
            title: Tado Alert
          - service: script.set_tado_mode_home_and_reset
            data: {}
mode: single

1 Like

Hi, could you send me what you have in your scripts? I’m curious how you solved it.

The script for away is:

alias: "Set Tado Mode: AWAY"
sequence:
  - service: climate.set_preset_mode
    data:
      preset_mode: away
    target:
      entity_id:
        - climate.main_house
        - climate.bedroom
        - climate.office
  - service: rest_command.reload_tado
    data: {}
mode: single
icon: mdi:home

Home would be the same, but preset is home rather than away. It was one of my early scripts, so now I’d do home and away in a single script with a variable.

Also, the final service call ‘rest_command.reload_tado’ forces Home Assistant to resync with Tado. You don’t need this, and Tado will eventually sync up with HA, and its quite complicated to setup.