Conditions in automations

I recently learned that you can use conditions in scripts! This changed my life, or at least a lot of my YAML. It got me thinking. Keep in mind that I have no idea how much work I’m proposing for the developers of this project, and I don’t know a better place to talk about it.

What if condition wasn’t just a top level section of an automation, like trigger and action, but could be integrated into the action portion of the automation and sub-conditions could be embedded into a higher-level condition. I’ll give an example of what I mean:

automation:
  test_automation:
    trigger:
      - some trigger
    action:
      - script.no_condition
      - condition:
          # any of the regular automation conditions could be here
          # I'm going to use 'time' as an example
          condition: time
          weekday:
            - mon
            - wed
            - fri
          # in addition to the conditions' regular keys, there's
          # also an 'actions' key that contains actions that are
          # only executed if the containing condition is satisfied
          action:
            - script.monday_wednesday_friday
      - condition:
          condition: time
          weekday:
            - tue
            - thu
          action:
            - service.tuesday_thursday
            # You can also embed a condition inside a condition
            - condition:
                condition: state
                entity_id: boolean_input.some_toggle
                state: 'on'
                action:
                  - script.tuesday_thursday_optional

So, on Mon, Wed, & Fri, this automation would run

  • script.no_condition
  • script.monday_wednesday_friday

Then on Tues & Thurs, it would run

  • script.no_condition
  • script.tuesday_thursday
  • and possibly script.tuesday_thursday_optional depending on the state of input_boolean.some_toggle

This style, combined with the stackable ‘and’ and ‘or’ conditions, could be super-powerful. I have found myself writing multiple automations with the same trigger, but different conditions and actions several times. I’ve thought about trying to re-write them into one automation with templates or something, but I thought this seemed like a reasonable approach to better “embrace the YAML”.

Am I nuts, or would this be cool?

OK, let me change your life again…:smile:

You can already put conditions in the “action:” section, too.

How many life changing events can you handle in a short period of time? :wink:

Yeah, I saw that, but they treat everything as a single linear execution. I was envisioning potentially multiple conditions each with their own set of actions where even though one condition fails, a subsequent condition could still succeed and execute its actions.

That, and the conditions within conditions are where it gets super powerful for me.

You can already do that too but it is a bit more complicated.

To do what you want you can use templates to define service call in actions based on certain if/elif/else conditions.

then you can have a list of service calls with different templates.

action;
  - service_template:
    {% if ('states.some_entity.state', 'some_state') %}
      some_service
    {% else %}
      some_different_service
    {% endif %}
    entity_id: some_entity
  - service_template: >
    {% if ('states.some_entity.state', 'some_state') %}
      some_service
    {% else %}
      some_different_service
    {% endif %}
    entity_id: some_entity
  - service: some_service
    data_template: 
      entity_id: >
        {% if ('states.some_entity.state', 'some_state') %}
          some_entity
        {% else %}
          some_different_entity
        {% endif %}
1 Like

Yeah, and I am doing that in most cases for the automations that require it, but if I have the choice, I prefer to “embrace the YAML”. It usually leads to better error messages from the config checker when I make a mistake. Debugging templates is horrible.

Strike two!

ok…then nevermind. :grinning:

Now, if I can just come up with something else you already know…:thinking:

:smile: