Turn on Christmast tree when someone it's at home, turn off when no one is at home, and turn on when sunset and turn off at 00:00

I’m trying to do this but I need tre automation and one doesn’t work, I would have only one

Automation 1:

alias: Albero di Natale ON
description: ''
trigger:
  - platform: sun
    event: sunrise
    offset: '0'
  - platform: state
    entity_id: device_tracker.person1
    to: home
  - platform: state
    entity_id: device_tracker.person2
    to: home
  - platform: state
    entity_id: device_tracker.person3
    to: home
condition: []
action:
  - service: switch.turn_on
    target:
      entity_id: switch.sonoff_albero_di_natale
mode: single

Automation 2:

alias: Albero di Natale OFF at 00:00
description: ''
trigger:
  - platform: time
    at: '00:00'
condition: []
action:
  - service: switch.turn_off
    target:
      entity_id: switch.sonoff_albero_di_natale
mode: single

Automation 3 (doesn’t work):

alias: Albero di Natale OFF when not home
description: ''
trigger:
  - platform: state
    entity_id: device_tracker.person1
    to: not_home
  - platform: state
    entity_id: device_tracker.person2
    to: not_home
  - platform: state
    entity_id: device_tracker.person3
    to: not_home
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: device_tracker.person1
        state: not_home
      - condition: state
        entity_id: device_tracker.persona2
        state: not_home
      - condition: state
        entity_id: device_tracker.persona3
        state: not_home
action:
  - service: switch.turn_off
    target:
      entity_id: switch.sonoff_albero_di_natale
mode: restart

Please tell me how to do only one automation with this, thanks

I’m not going deep in to the logics, to heavy for me.

But I definitely see a typo:

entity_id: device_tracker.person3
entity_id: device_tracker.persona3

One of the two is faulty… :wink:

Create a group containing the three device_trackers. It will help to simplify the automation.

group:
  people:
    name: People
    entities:
      - device_tracker.person1
      - device_tracker.person2
      - device_tracker.person3

Restart Home Assistant to load the new group (or execute Configuration > Server Controls > Reload Groups). Go to Developer Tools > States, find the group you just created and confirm its state is home (let me know if its state is on instead of home).

Create the following automation:

alias: Albero di Natale
description: ''
trigger:
  - platform: sun
    event: sunrise
  - platform: state
    entity_id: group.people
  - platform: time
    at: '00:00'
condition: []
action:
  - choose:
      - conditions: "{{ trigger.platform == 'sun' or (trigger.platform == 'state' and trigger.to_state.state == 'home') }}"
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.sonoff_albero_di_natale
      - conditions: "{{ trigger.platform == 'time' or (trigger.platform == 'state' and trigger.to_state.state == 'not_home')}}"
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.sonoff_albero_di_natale
    default: []
mode: single
1 Like

I have device trackers with person name and surname, this was to hide them in the post

Thanks I’ll try

My device trackers are not people, they are from wifi, wich is faster, how can I group them?

Thanks @123 and @finity for helping out. It works and I’m learning how to do advanced automations.
I have a question for another automation that sync light with TV status, for this trigger, how can i retrieve if 1 second passed?

trigger:
  - platform: state
    entity_id: media_player.tv_camera_cast
    to: 'off'
    for:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0

Then I’ll apply the value_template mode wich is beautiful, now I have 4 automation for 2 tvs ahahah