Condition on 1 of 3 actions

Hello all,

I only started this week in home assistant and my history to coding is, to say the least, limited. But I’m always looking to learn.

With that out of the way, I have this automation to announce when when someone opens the backdoor (as family tends to enter through there).

Currently I have a sensor that triggers a bell chime on a speaker group in the living area.

In my office & bedroom it sends a TTS to announce that the backdoor is open.

Now my girlfriend gets annoyed by the tts in the bedroom on certain hours (understandably), and she rather doesn’t like to hear it between 22h00 and 06h00.

So the question is, does it help to just move the condition down to the part in the code where only the smart alarm clocks are? or is there more to it?

this is what I got:

alias: Keukendeur geopend
description: ""
trigger:
  - platform: device
    type: turned_on
    device_id: 850d8fb6d5fce40d98a20ada15eb49ae
    entity_id: switch.switch
    domain: switch


action:
  - service: media_player.play_media
    target:
      entity_id: media_player.living_groep
    data:
      media_content_id: media-source://media_source/local/doorbell-6.mp3
      media_content_type: audio/mpeg
    metadata:
      title: doorbell-6.mp3
      thumbnail: null
      media_class: music
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://media_source


  - service: media_player.play_media
    target:
      entity_id: media_player.bureau_speaker
    data:
      media_content_id: media-source://tts/google_translate?message=De+Keukendeur+is+open
      media_content_type: provider
    metadata:
      title: De Keukendeur is open
      thumbnail: https://brands.home-assistant.io/_/google_translate/logo.png
      media_class: app
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://tts
        - media_content_type: provider
          media_content_id: media-source://tts/google_translate?message=De+Keukendeur+is+open

condition:
  - condition: time
    weekday:
      - sun
      - sat
      - fri
      - thu
      - wed
      - tue
      - mon
    after: "06:00:00"
    before: "22:00:00"

  - service: media_player.play_media
    target:
      entity_id: media_player.wekkers_1
    data:
      media_content_id: media-source://tts/google_translate?message=De+Keukendeur+is+open
      media_content_type: provider
    metadata:
      title: De Keukendeur is open
      thumbnail: https://brands.home-assistant.io/_/google_translate/logo.png
      media_class: app
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://tts
        - media_content_type: provider
          media_content_id: media-source://tts/google_translate?message=De+Keukendeur+is+open
mode: single

PS: Bonus points for those who can tell my how it would automatically resume TuneIn and spotify when they where playing before this script.

Thanks in advance!

You sort of have it right…

In yaml the major sections (keys) don’t care where they are in the order. They will still work exactly the same if the order is:

trigger
condition
action

or:

trigger
action
condition

or:

action
condition
trigger

So putting the condition where you have it won’t really work. One, because you are putting the major section (condition:) inside of another major section(action:) and two, even if that would work it still wouldn’t matter because it doesn’t matter the order.

However, you CAN put a condition in the action as you want by just leaving the “condition:” key out:

alias: Keukendeur geopend
description: ""
trigger:
  - platform: device
    type: turned_on
    device_id: 850d8fb6d5fce40d98a20ada15eb49ae
    entity_id: switch.switch
    domain: switch


action:
  - service: media_player.play_media
    target:
      entity_id: media_player.living_groep
    data:
      media_content_id: media-source://media_source/local/doorbell-6.mp3
      media_content_type: audio/mpeg
    metadata:
      title: doorbell-6.mp3
      thumbnail: null
      media_class: music
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://media_source


  - service: media_player.play_media
    target:
      entity_id: media_player.bureau_speaker
    data:
      media_content_id: media-source://tts/google_translate?message=De+Keukendeur+is+open
      media_content_type: provider
    metadata:
      title: De Keukendeur is open
      thumbnail: https://brands.home-assistant.io/_/google_translate/logo.png
      media_class: app
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://tts
        - media_content_type: provider
          media_content_id: media-source://tts/google_translate?message=De+Keukendeur+is+open

  - condition: time
    weekday:
      - sun
      - sat
      - fri
      - thu
      - wed
      - tue
      - mon
    after: "06:00:00"
    before: "22:00:00"

  - service: media_player.play_media
    target:
      entity_id: media_player.wekkers_1
    data:
      media_content_id: media-source://tts/google_translate?message=De+Keukendeur+is+open
      media_content_type: provider
    metadata:
      title: De Keukendeur is open
      thumbnail: https://brands.home-assistant.io/_/google_translate/logo.png
      media_class: app
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://tts
        - media_content_type: provider
          media_content_id: media-source://tts/google_translate?message=De+Keukendeur+is+open
mode: single

that way the last action won’t run unless the condition is met.

Thanks a lot!

Wow, that easy huh?

Mostly. At least in your easy use case.

It will only work like that for the last action in the automation because it will stop at the first false condition.

if you have

action 1
action 2
condition 1 false
action 3
condition 2 true
action 4

it will not run action 3 or 4 even if condition 2 is true because it stops at condition 1

1 Like

Reference: Understanding Automations

All automations are made up of a trigger and an action. Optionally combined with a condition.

Reference: Automation Actions

Conditions can also be part of an action. You can combine multiple service calls and conditions in a single action, and they will be processed in the order you put them in. If the result of a condition is false, the action will stop there so any service calls after that condition will not be executed.

1 Like