Error: condition among automation actions

Hello community,
I’m new here, I’ve been playing with HA for a few days now and it’s been great :slight_smile:

I’m really finding hard to find a reason why this configuration in automations.yaml would not work (some parts are hidden and replaced by “…”):

- alias: GoodMorning
  trigger:
  action:
    - service: script.turn_on
      data_template:
        entity_id: script.say_something
        variables:
          message: |-
            ...
    - condition: template
      value_template: |-
        {% set hour = states('sensor.time').split(':')[0] | int %}
        {% hour >= 4 and hour < 10 %}
    - delay:
        seconds: 10
    - wait_template: "{{ is_state('media_player.lounge', 'idle') or is_state('media_player.lounge', 'paused') }}"
      timeout: '00:01:00'
    - service: media_player.volume_set
      data:
        entity_id: media_player.lounge
        volume_level: 0.7
    - service: media_player.play_media
      data_template:
        entity_id: media_player.lounge
        media_content_id: ...
        media_content_type: music

From the logs:

mag 09 00:12:33 server hass[2738]: 2019-05-09 00:12:33 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: [condition] is an invalid option for [automation]. Check: automation->action->1->condition. (See /home/homeassistant/.homeassistant/automations.yaml, line 31). Please check the docs at https://home-assistant.io/components/automation/

The weird thing is if I put some simple condition like true in value_template it works (but of course it’s not what I want).
I’ve been fighting with this for an hour now… am I missing something? Docs says that in theory this could be done:

Thanks

  1. if you want to prevent all the actions happening with a condition, don’t put the condition in the actions. Do it like this:
- alias: GoodMorning
  trigger:
  ...
  condition:
    - condition: template
   ...
  action:
  1. your template should be:
        {% set hour = states('sensor.time').split(':')[0] | int %}
        {{ hour >= 4 and hour < 10 }}

Which could be simplified to just:

{{ 10 > now().hour >= 4 }}
2 Likes

Tried that, same error.

I purposely put a condition among the actions because I want to execute whatever there is before the condition and then stop if the condition fails.

Damn, the percent signs!! Thank you, good catch!

Even better, I didn’t think of the now object, thanks.

1 Like

Sorry, I somehow skipped over the message action above the condition.