Add condition to action in automation

basically I want to check to see if a media_player is in use, if its not then play the radio…if it is then do nothing. along the lines of:

    - condition: template
  data_template: "{{ is_state('media_player.family_room', 'paused') }}"
- service: media_player.select_source
  entity_id: media_player.family_room
  data:
    source: "BBC Radio 2"

The above is in an automation, this is the “action” part as there are other conditions not relevant to this section.
not too sure what type of template or indeed if I should call a script after the condition?

Check your pasted code. The condition statement is indented incorrectly. Probably a pasting error but spacing is important in YAML and pasting incorrectly indented code does not help with debugging.

This template will be true when idle, off or paused

Edit: fixed as per @anon43302295’s post below.

value_template: "{{ is_state('media_player.family_room', 'idle') or is_state('media_player.family_room', 'paused') or is_state('media_player.family_room', 'off) }}"

You can call any service after the condition. Including a script.

Should be value_template there, not data_template.

1 Like

Thanks. Fixed the stupid mistake. Time for sleep.

thanks for the help but I’m getting a configuration error, here’s the action part from the automation:

  action:
- delay: '00:00:05'
- service: media_player.volume_set
  entity_id: media_player.family_room
  data:
    volume_level: 0.16
- condition: template
  value_template: "{{ is_state('media_player.family_room', 'idle') or is_state('media_player.family_room', 'paused') or is_state('media_player.family_room', 'off) }}"
- service: media_player.select_source
  entity_id: media_player.family_room
  data:
    source: "BBC Radio 2"

It would help if you told me what error.

its a formatting error, but if I use the following it passes…just need to test but the 1st “condition:” doesn’t look right:

  action:
- delay: '00:00:05'
- service: media_player.volume_set
  entity_id: media_player.family_room
  data:
    volume_level: 0.16
    condition: 
    - condition: template
      value_template: "{{ is_state('media_player.family_room', 'idle') or is_state('media_player.family_room', 'paused') or is_state('media_player.family_room', 'off) }}"
- service: media_player.select_source
  entity_id: media_player.family_room
  data:
    source: "BBC Radio 2"

Please paste the actual error. It helps considerably.

However…

Your condition indentation is incorrect. Should be:

action:
- delay: '00:00:05'
- service: media_player.volume_set
  entity_id: media_player.family_room
  data:
    volume_level: 0.16
- condition: template
  value_template: "{{ is_state('media_player.family_room', 'idle') or is_state('media_player.family_room', 'paused') or is_state('media_player.family_room', 'off) }}"
- service: media_player.select_source
  entity_id: media_player.family_room
  data:
    source: "BBC Radio 2"

thanks for keeping with me, if I use the following format:

  action:
- delay: '00:00:05'
- service: media_player.volume_set
  entity_id: media_player.family_room
  data:
    volume_level: 0.16
- condition: 
    condition: template
      value_template: "{{ is_state('media_player.family_room', 'idle') or is_state('media_player.family_room', 'paused') or is_state('media_player.family_room', 'off) }}"
- service: media_player.select_source
  entity_id: media_player.family_room
  data:
    source: "BBC Radio 2"

I get the error:

Configuration invalidCHECK CONFIG

Error loading /config/configuration.yaml: mapping values are not allowed here in “/config/automations/occupancy.yaml”, line 76, column 25

What about if you use the configuration I posted?

I get this error:

Invalid config for [automation]: [condition] is an invalid option for [automation]. Check: automation->action->7->condition. (See /config/configuration.yaml, line 678). Please check the docs at Automation - Home Assistant

It is suspicious that it is saying action 7 is at fault when in fact the condition is at position 3 in your actions. Is this that actual code you are testing?

Where is line 678 in your code?

sorry I trimmed the code down as its quite large, here’s the full version:

- alias: "Family Greeting"
  trigger:
- platform: state
  entity_id:
    - input_boolean.her_present
    - input_boolean.me_present
  from: 'off'
  to: 'on'
  for: '00:01:45'
  condition:
- condition: time
  after: '09:00:00'
  before: '22:00:01'
  action:
- delay: "00:00:05"
- wait_template: "{{ is_state('script.say_greeting', 'off') }}"
- wait_template: "{{ is_state('binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor', 'on') }}"
- delay: "00:00:25"
- service: script.say_greeting
  data_template:
    entity_id: "media_player.mr_s_echo_dot"
    person: >
      {{ trigger.to_state.name }}
      {%- for s in states.input_boolean
            |rejectattr('entity_id','eq',trigger.entity_id)
            |selectattr('state','eq','on')
            if '_present' in s.entity_id and
               (now() - s.last_changed).total_seconds() < 2 -%}
        {{ (' and ' if loop.last else ', ') ~ s.name }}
      {%- endfor %}
    message: >
      {{ ["Welcome home PERSON, how about some BBC Radio 2, you've earned it!",
          "Welcome home PERSON, time for some relaxing tunes",
          "Hi PERSON The fridge & cooker had a fight while you were out, please don't leave again!",
          "Hey PERSON Thank goodness you're back, I have a list of jobs for you to do.",
          "Welcome home PERSON, you get the kettle on and I'll sort the tunes out",
          "Welcome home PERSON, I bet your feet are cold so I'll set the heating to full power captain!",
          "Hey PERSON Good to have you back, the kettle is on standbye",
          "Welcome home PERSON, mine is a large tea with 15 sugars",
          "Welcome home PERSON, shoes off, kettle on and how about some radio?",
          "Welcome home PERSON, I think there is a spoon in the knife draw. Can you check?",
          "Welcome home PERSON, I've been working on a joke. Here goes. A furniture store keeps calling me, All I wanted was, one, night, stand",
          "Welcome home PERSON, I've been working on a new joke. Here goes. I used to work, in a shoe recycling shop, It was sole destroying",
          "Welcome home PERSON, I think I've cracked this joke thing. Here goes. Why did the scarecrow win an award? Because he was outstanding in his field",
      ] | random }}
- delay: '00:00:05'
- service: media_player.volume_set
  entity_id: media_player.family_room
  data:
    volume_level: 0.16
- condition: template
  value_template: "{{ is_state('media_player.family_room', 'idle') or is_state('media_player.family_room', 'paused') or is_state('media_player.family_room', 'off) }}"
- service: media_player.select_source
  entity_id: media_player.family_room
  data:
    source: "BBC Radio 2"

if I remove the condition element in the action part, the error no longer shows.

Your indentation is way off. Try this:

- alias: "Family Greeting"
  trigger:
    platform: state
    entity_id:
      - input_boolean.her_present
      - input_boolean.me_present
    from: 'off'
    to: 'on'
    for: '00:01:45'
  condition:
    condition: time
    after: '09:00:00'
    before: '22:00:01'
  action:
    - delay: "00:00:05"
    - wait_template: "{{ is_state('script.say_greeting', 'off') }}"
    - wait_template: "{{ is_state('binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor', 'on') }}"
    - delay: "00:00:25"
    - service: script.say_greeting
      data_template:
        entity_id: "media_player.mr_s_echo_dot"
        person: >
          {{ trigger.to_state.name }}
          {%- for s in states.input_boolean
                |rejectattr('entity_id','eq',trigger.entity_id)
                |selectattr('state','eq','on')
                if '_present' in s.entity_id and
                   (now() - s.last_changed).total_seconds() < 2 -%}
            {{ (' and ' if loop.last else ', ') ~ s.name }}
          {%- endfor %}
        message: >
          {{ ["Welcome home PERSON, how about some BBC Radio 2, you've earned it!",
              "Welcome home PERSON, time for some relaxing tunes",
              "Hi PERSON The fridge & cooker had a fight while you were out, please don't leave again!",
              "Hey PERSON Thank goodness you're back, I have a list of jobs for you to do.",
              "Welcome home PERSON, you get the kettle on and I'll sort the tunes out",
              "Welcome home PERSON, I bet your feet are cold so I'll set the heating to full power captain!",
              "Hey PERSON Good to have you back, the kettle is on standbye",
              "Welcome home PERSON, mine is a large tea with 15 sugars",
              "Welcome home PERSON, shoes off, kettle on and how about some radio?",
              "Welcome home PERSON, I think there is a spoon in the knife draw. Can you check?",
              "Welcome home PERSON, I've been working on a joke. Here goes. A furniture store keeps calling me, All I wanted was, one, night, stand",
              "Welcome home PERSON, I've been working on a new joke. Here goes. I used to work, in a shoe recycling shop, It was sole destroying",
              "Welcome home PERSON, I think I've cracked this joke thing. Here goes. Why did the scarecrow win an award? Because he was outstanding in his field",
          ] | random }}
    - delay: '00:00:05'
    - service: media_player.volume_set
      entity_id: media_player.family_room
      data:
        volume_level: 0.16
    - condition: template
      value_template: "{{ is_state('media_player.family_room', 'idle') or is_state('media_player.family_room', 'paused') or is_state('media_player.family_room', 'off) }}"
    - service: media_player.select_source
      entity_id: media_player.family_room
      data:
        source: "BBC Radio 2"

still getting the same error:

Configuration invalidCHECK CONFIG

Invalid config for [automation]: [condition] is an invalid option for [automation]. Check: automation->action->7->condition. (See /config/configuration.yaml, line 678). Please check the docs at Automation - Home Assistant

when I pasted your code in, I cannot see the different in formatting to mine so maybe it’s just how I’ve posted it here…if I remove the 2 condition lines then the error goes away :frowning:

What happens if you change the condition type:

    - delay: '00:00:05'
    - service: media_player.volume_set
      entity_id: media_player.family_room
      data:
        volume_level: 0.16
    - condition: state
      entity_id: media_player.family_room
      state: 'idle'
    - service: media_player.select_source
      entity_id: media_player.family_room
      data:
        source: "BBC Radio 2"

If that works try this:

    - delay: '00:00:05'
    - service: media_player.volume_set
      entity_id: media_player.family_room
      data:
        volume_level: 0.16
    - condition: or
      conditions:
        - condition: state
          entity_id: media_player.family_room
          state: 'idle'
        - condition: state
          entity_id: media_player.family_room
          state: 'off'
        - condition: state
          entity_id: media_player.family_room
          state: 'paused'
    - service: media_player.select_source
      entity_id: media_player.family_room
      data:
        source: "BBC Radio 2"

that cracked it! thanks for all your help on this :slight_smile:

Well that’s just weird.

I’d say maybe not so weird in light of the fact not a single one of the examples posted by partymike is free of indentation errors. Even the one based on your example, when echoed back to you, contained an indentation error that was not in your original example. It was only a matter of time before one copy/paste attempt finally broke the pattern and was successful …

@partymike
Use a text editor like Visual Studio Code with a YAML checker plug-in. It will identify YAML errors for you.

heh the indentation errors were because of my copying a section rather than the whole automation; I use VSC everyday both remote and in home assistant - the two line condition statement definitely didn’t work and looking at the code Tom_I posted I cannot see a formatting issue.

Love the support this community brings and appreciate the time you guys take to point me in the right direction :slight_smile: