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?