If-then Action in Automation

Hi there,

i need some help… i want to use if-then as action for my automation… i want to do this:

Trigger: Door opened

If: group.family changes from “not_home” to “home”
Then: turn off Automation Sonos Alarm

Else: Run Automation Sonos Alarm

But i can’t find a way to use State Change in if-then. Is it possible ?

Thank you !

Assuming you mean do something based on a condition after the door triggers:

trigger:
  - platform: state
    entity_id: binary_sensor.door
    from: 'off'
    to: 'on'
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: group.family
            state: 'home'
        sequence:
          - turn off Automation Sonos Alarm here
      - conditions:
          - condition: state
            entity_id: group.family
            state: 'not_home'
        sequence:
          - run Automation Sonos Alarm here

If you really want to wait after the door triggers for a change in state of the group then use a wait for trigger instead: Script Syntax - Home Assistant then use the choose action.

Or use the If… then statement like this in your automation:

trigger:
  - platform: state
    entity_id: binary_sensor.door
    from: 'off'
    to: 'on'
action:
  - if:
    - condition: state
      entity_id: group.family
      state: 'home'
    then:
    - turn off Automation Sonos Alarm here
    else:
    - run Automation Sonos Alarm here

Post the Sonos Alarm automation … because something tells me this can be done in a way that doesn’t require actually turning it off.

Yes, I would like to wait for the status change, otherwise the alarm will be triggered immediately. Unfortunately, it always takes 1 to 2 minutes for the presence status to be updated.
How does the wait for trigger work? Can I define how long he should wait for this trigger until the automation continues

Yes that’s the wait timeout, it’s in the documentation I linked to.

You should answer Taras’s post though.

1 Like

The problem is that when I open the door I don’t have the status “home” yet.

Hi, i have to rebuild it, but this is the actual one:

alias: ALARM Sonos_Haustuer
description: ""
trigger:
  - type: opened
    platform: device
    device_id: 8bf6c7ff01d04e068de2a061f13cbefe
    entity_id: binary_sensor.haustuer
    domain: binary_sensor
condition:
  - condition: state
        entity_id: group.family
        state: not_home
    
action:
  - service: sonos.snapshot
    data:
      with_group: true
      entity_id: all
  - service: media_player.volume_set
    data:
      volume_level: 0.75
    target:
      entity_id:
        - media_player.sonos_buero
        - media_player.sonos_bad
        - media_player.sonos_kuche
  - service: media_player.play_media
    data:
      media_content_id: http://xxx.xxx.xxx.xx:8123/local/alarmhome.mp3
      media_content_type: music
    target:
      entity_id:
        - media_player.sonos_buero
        - media_player.sonos_bad
        - media_player.sonos_kuche
  - delay:
      hours: 0
      minutes: 0
      seconds: 20
      milliseconds: 0
  - service: sonos.restore
    data:
      with_group: true
      entity_id: all
mode: single

Let me know if this is how you want it to behave. When the door is opened, it waits until group.family reports home before proceeding to the next condition. It waits for no more than 2 minutes (you can change it). The second condition checks if group.family is not_home. If you are not home, the alarm sound is played. If you are home then the automation exits and no sound is played.

alias: ALARM Sonos_Haustuer
description: ""
trigger:
  - type: opened
    platform: device
    device_id: 8bf6c7ff01d04e068de2a061f13cbefe
    entity_id: binary_sensor.haustuer
    domain: binary_sensor
condition: []
action:
  - wait_template: "{{ is_state('group.family', 'home') }}"
    timeout: '00:02:00'
    continue_on_timeout: true
  - condition:  "{{ is_state('group.family', 'not_home') }}"
  - service: sonos.snapshot
    data:
      with_group: true
      entity_id: all
  - service: media_player.volume_set
    data:
      volume_level: 0.75
    target:
      entity_id:
        - media_player.sonos_buero
        - media_player.sonos_bad
        - media_player.sonos_kuche
  - service: media_player.play_media
    data:
      media_content_id: http://xxx.xxx.xxx.xx:8123/local/alarmhome.mp3
      media_content_type: music
    target:
      entity_id:
        - media_player.sonos_buero
        - media_player.sonos_bad
        - media_player.sonos_kuche
  - delay:
      hours: 0
      minutes: 0
      seconds: 20
      milliseconds: 0
  - service: sonos.restore
    data:
      with_group: true
      entity_id: all
mode: single

EDIT

Correction. Improved logic by employing two conditions.

1 Like

Yes i think this is the solution… so it waits for 2 Minutes and if after 2 Minutes

not_home → the Automation runs
home → The Automation doesn’t run

correct ?

Yes, that’s correct.

The value of timeout determines how long it should wait before it gives up waiting.

The value of continue_on_timeout determines what it should do next after it gives up waiting.

1 Like

Okay perfect, I’ll try it. Thank you very much :slight_smile:

Hi, i‘ve tried the automation but it doesn‘t work as hoped. The alarm starts immediately and does‘t wait…
Please have a look at the beginning of my automation… is everything right with the waiting template ?

alias: ALARM Sonos_Haustuer
description: ""
trigger:
  - type: opened
    platform: device
    device_id: 8bf6c7ff01d04e068de2a061f13cbefe
    entity_id: binary_sensor.haustuer
    domain: binary_sensor
condition: []
action:
  - wait_template: "{{ is_state('group.family', 'not_home') }}"
    timeout: "00:03:00"
    continue_on_timeout: false

I have corrected the automation’s logic (see modified example above).

Here’s what’s different:

action:
  - wait_template: "{{ is_state('group.family', 'home') }}"
    timeout: '00:02:00'
    continue_on_timeout: true
  - condition:  "{{ is_state('group.family', 'not_home') }}"
  • After the door is opened, it waits up to 2 minutes for group.family to report home. If the timeout expires, it continues to the next action which is another condition.
  • The second condition checks if group.family is not_home.

Let’s say someone (not your family) opens the door.
The wait_template will expire because group.family remains not_home.
The second condition confirms your family is not_home.
The alarm sound is played.

Let’s say a family member opens the door.
The wait_template provides up to 2 minutes for group.family to report home.
The second condition doesn’t confirm your family is not_home.
The alarm sound is not played.

1 Like

Hi, perfect now it works :ok_hand:

Thank you very much, again :wink:

Hi, I didn’t consider anything else. I came home yesterday and my cell phone was empty, so of course the status didn’t change to “home” and the automation went through. Is it possible to add something here, such as pressing a button on my light switch to “manually” cancel the automation?

Create an Input Boolean then add a State Condition that confirms the Input Boolean is on (indicating the normal operation). If you turn off the Input Boolean (before you open the door), it will prevent the automation from executing its actions.