"Leaving and Returning Home"

"Please help me create an automation. Right now, almost everything works correctly, but there is an issue with changing the state of zone.home from, for example, 3 to 1, as the automation is also triggered.

In general, I want the trigger to be a change in the home zone. If all residents leave the home, I want to receive a notification. If one person returns home, I want it to first turn on the outside lights, but only if it’s after sunset. Then, it should turn on the interior lights, but only after the main door is opened, and this should also happen after sunset.

I’ve tried various combinations, but it always activates the automation when changing the zone.home state to 1, even from 2 or 3 to 1."

  - id: "2522788646735623"
    alias: Światło On po powrocie do domu
    description: ""
    trigger:
      - platform: state
        entity_id:
          - zone.home
    condition:
      - condition: template
        value_template: "{{ is_state('input_select.dom_select', 'Auto') }}"
    action:
      - if:
          - condition: template
            value_template: "{{ states('zone.home') | float < 1 }}" # Home = 0
        then:
          - service: notify.facebook_text_giebek
            data:
              message: Nikogo nie ma w domu - po powrocie powinno włączyć się światło
        else:
          - if:
              - condition: template
                value_template: "{{ states('zone.home') | float == 1 }}" # Home = 1
              # - condition: template
              #   value_template: "{{ states('zone.home') | float > 0 and states('zone.home') | float < 2 }}"
              - condition: template
                value_template: "{{ is_state('sun.sun', 'below_horizon') }}" # słońce jest poniżej horyzontu.
              - condition: template
                value_template: "{{ ((as_timestamp(now()) - as_timestamp(trigger.from_state.last_changed)) | int ) > 5 }}"
            then:
              - if:
                  - condition: template
                    value_template: "{{ is_state('light.swiatlo_podbitka_przod', 'off') or is_state('light.swiatlo_podbitka_wykusz', 'off')}}"
                  - condition: template
                    value_template: "{{ is_state('sun.sun', 'below_horizon') }}" # słońce jest poniżej horyzontu.
                then:
                  - service: light.turn_on
                    target:
                      entity_id:
                        - light.swiatlo_podbitka_przod
                        - light.swiatlo_podbitka_wykusz
                    data:
                      brightness_pct: 10
              - wait_for_trigger:
                  - platform: state
                    entity_id: binary_sensor.drzwi_wejsciowe_contact
                    to: "on"
                timeout: "00:00:10"
                continue_on_timeout: false
              - service: light.turn_on
                data:
                  transition: 10
                  color_name: red
                  brightness_pct: 100
                target:
                  entity_id: light.yeelink_bslamp1_fb60_light
              - service: light.turn_on
                data:
                  transition: 10
                  color_name: white
                  brightness_pct: 100
                target:
                  entity_id: light.hst
              - service: light.turn_on
                data:
                  transition: 20
                  effect: Day
                  kelvin: 3000
                  brightness_pct: 20
                target:
                  entity_id:
                    - light.yeelink_ceilc_c7c1_light
              - service: notify.facebook_text_giebek
                data:
                  message: Świtło On po powrocie do domu - test
    mode: restart

That’s because you have used a State trigger without defined to or from configuration variables. When you do that, the trigger fires every time the entity’s state or any of its attributes update. In the case of a zone entity, that means every person entering the zone will cause the automation to fire 2 times. Once for the state change, and once for the change to the persons attribute.

When you’re interested in whether a state’s value crosses a threshold rather than lands on a specific value, use Numeric state triggers…

- id: "2522788646735623"
  alias: Światło On po powrocie do domu
  description: ""
  trigger:
    - platform: numeric_state
      entity_id:
        - zone.home
      below: 1
      id: leave
    - platform: numeric_state
      entity_id:
        - zone.home
      above: 0
      id: return
  condition:
    - condition: template
      value_template: "{{ is_state('input_select.dom_select', 'Auto') }}"
  action:
    - choose:
        - conditions:
            - condition: trigger
              id: leave
          sequence:
            - service: notify.facebook_text_giebek
              data:
                message: Nikogo nie ma w domu - po powrocie powinno włączyć się światło
        - conditions:
            - condition: trigger
              id: return
            - condition: template
              value_template: "{{ is_state('sun.sun', 'below_horizon') }}" # słońce jest poniżej horyzontu.
          sequence:
            - service: light.turn_on
              target:
                entity_id:
                  - light.swiatlo_podbitka_przod
                  - light.swiatlo_podbitka_wykusz
              data:
                brightness_pct: 10
            - wait_for_trigger:
                - platform: state
                  entity_id: binary_sensor.drzwi_wejsciowe_contact
                  to: "on"
              timeout: "00:00:10"
              continue_on_timeout: false
            - service: light.turn_on
              data:
                transition: 10
                color_name: red
                brightness_pct: 100
              target:
                entity_id: light.yeelink_bslamp1_fb60_light
            - service: light.turn_on
              data:
                transition: 10
                color_name: white
                brightness_pct: 100
              target:
                entity_id: light.hst
            - service: light.turn_on
              data:
                transition: 20
                effect: Day
                kelvin: 3000
                brightness_pct: 20
              target:
                entity_id:
                  - light.yeelink_ceilc_c7c1_light
            - service: notify.facebook_text_giebek
              data:
                message: Świtło On po powrocie do domu - test
  mode: single

Thank you for your response. You changed the mode to single , which, in the case of a state change during the automation, had 00:00:10 set for testing purposes, but the final value will be 10 minutes.

            - wait_for_trigger:
                - platform: state
                  entity_id: binary_sensor.drzwi_wejsciowe_contact
                  to: "on"
              timeout: "00:10:00"
              continue_on_timeout: false

Is there any way to address this?

Sure, I think it will work perfectly now. Thank you for your help. Best regards.

Why is “- choose:” and “sequence” more frequently chosen in automations rather than “if, then”?
Is there a simple explanation for this?

Habit and available functionality.

In HA, the Choose action existed for a while before the If/Then action was added. So, a lot of us who have been around since then are just in the habit of using Choose.

Choose allows more than just then/else branching without requiring nesting.

  - choose:
      - conditions: #If
        sequence: #Then
      - conditions: #Else If
        sequence: #Then
    default: # Else
1 Like

Please, I need one more help. I enjoy making automations more challenging :P.
Is it possible to make it so that after detecting motion, the program resets back to counting down somewhere around 25 minutes

  - id: "2522788646735623"
    alias: Światło On po powrocie do domu
    description: ""
    trigger:
      - platform: numeric_state
        entity_id:
          - zone.home
        below: 1
        id: leave
      - platform: numeric_state
        entity_id:
          - zone.home
        above: 0
        id: return
    condition:
      - condition: template
        value_template: "{{ is_state('input_select.dom_select', 'Auto') }}"
    action:
      - choose:
          - conditions:
              - condition: trigger
                id: leave
            sequence:
              - service: notify.facebook_text_giebek
                data:
                  message: Nikogo nie ma w domu - po powrocie powinno włączyć się światło
              - delay: "00:00:05"
              - service: notify.facebook_text_giebek
                data:
                  message: Za 25 minut światło powinno się wyłączyć.
              - delay: "00:25:00"
              - service: homeassistant.turn_off
                target:
                  entity_id: light.wszystkie_swiatla
              - service: media_player.turn_off
                target:
                  entity_id:
                    - media_player.lazienka_2
              - if:
                  - condition: template
                    value_template: "{{ is_state('media_player.lg_salon', 'on') }}"
                then:
                  - service: automation.turn_off
                    entity_id: automation.paski_led_on_off
                  - service: scene.create
                    data:
                      scene_id: wled_tv_mem
                      snapshot_entities: light.lg_tv
                  - service: light.turn_on
                    target:
                      entity_id:
                        - light.lg_tv
                    data:
                      brightness_pct: 100
                      color_name: red
                      effect: Blink
                  - service: number.set_value
                    target:
                      entity_id:
                        - number.lg_tv_speed
                    data:
                      value: "200"
                  - wait_template: "{{ is_state('binary_sensor.wszystkie_czujniki_pir', 'on') }}"
                    timeout: 300
                  - if:
                      - "{{ wait.completed }}" # - "{{ not wait.completed }}"
                    then:
                      - service: automation.trigger
                        data:
                          skip_condition: false
                        target:
                          entity_id: automation.wylacz_wszystkie_swiatla
                      - service: notify.facebook_text_giebek
                        data:
                          message: Automatyzacja działa - TV został wyłączony!!!!!!!!!!!!!!!!!!!!
                    else:
                      - service: media_player.turn_off
                        target:
                          entity_id:
                            - media_player.lg_salon
                  - delay:
                      seconds: 2
                  - service: scene.turn_on
                    target:
                      entity_id: scene.wled_tv_mem
                  - service: automation.turn_on
                    entity_id: automation.paski_led_on_off
              - service: notify.facebook_text_giebek
                data:
                  message: Wszystkie światła i media zostały wyłączone.
          - conditions:
              - condition: trigger
                id: return
              - condition: template
                value_template: "{{ is_state('sun.sun', 'below_horizon') }}" # słońce jest poniżej horyzontu.
            sequence:
              - service: light.turn_on
                target:
                  entity_id:
                    - light.swiatlo_podbitka_przod
                    - light.swiatlo_podbitka_wykusz
                data:
                  brightness_pct: 10
              - wait_for_trigger:
                  - platform: state
                    entity_id: binary_sensor.drzwi_wejsciowe_contact
                    to: "on"
                timeout: "00:10:00"
                continue_on_timeout: false
              - delay: "00:00:02"
              - service: light.turn_on
                data:
                  transition: 100
                  color_name: white
                  brightness_pct: 1
                target:
                  entity_id: light.yeelink_bslamp1_fb60_light
              - delay: "00:00:08"
              - service: light.turn_on
                data:
                  transition: 100
                  color_name: white
                  brightness_pct: 1
                target:
                  entity_id: light.hst
              - delay: "00:00:14"
              - service: light.turn_on
                data:
                  transition: 30
                  effect: Day
                  kelvin: 3000
                  brightness_pct: 30
                target:
                  entity_id:
                    - light.yeelink_ceilc_c7c1_light
              - service: notify.facebook_text_giebek
                data:
                  message: Świtło On po powrocie do domu - test
    mode: restart

There wasn’t a specific topic, but now the automation works well. It rarely happens, but when my wife and I leave the apartment, the kids can continue watching TV. In the near future, they will get phones and integrate them with HA. For now, school is more important :).

If someone notices an error in the code or has a suggestion to improve the code, please write to me.

Thanks again for your help.