Automation using yaml

I am a little confused on how to approach this.

I want to create many automations that are very similar, just the time varies. So I want to create the base automation logic using the UI because that way its easier and I cant fudge the syntax.

And then I want to copy and paste the lines over and over again using yaml and just editing the time section. Obviously because this will be faster than doing it using the UI.

So I created the first automation. However it doesnt show in configuration.yaml but only in automations.yaml

Where do I add the subsequent automations? I can read that automations.yaml isnt to be manually edited. But then I also read in the wiki page that one should create the automations in the configuration.yaml and then paste it into automations.yaml which sort of contradicts the initial warning.

In any case if i have to follow this process, do I need to now copy the first automation manually created also to configuration.yaml? and then re copy all of them back to automations.yaml? or just the new ones?

1 Like

Not saying this cannot be done manually but via the GUI you have a “duplicate automation” option (3 dots top right)

1 Like

I am aware of that but its still a lot more work than copying 100 automations in the yaml and using find and replace to change what i want to.

Its an automation therefore it will be in automations.yaml not config!

In the automations.yaml file.

That’s how automations were created and edited before it was possible via the Ui, some people don’t use the Ui at all and simply edit the automations.yaml.

Absolutely no point, if you want to create it anywhere then use a text editor that supports yaml and then paste it to automations.yaml. Remember of course that you can edit the automations.yaml to your hearts content as no changes will be seen until you reload automations and or restart HA (note automations will be reloaded if you edit one in the Ui)

1 Like

Thanks for all the clarifications!

The below is a rather strange warning in the documentation, in that case! :slight_smile:

I’m curious about what your automation does and why you need to make 100 copies. Maybe there is a way to make one automation that does the same. This would likely be easier to maintain.

Can we see?

Yeah sure

alias: monday1
description: ''
trigger:
  - platform: time
    at: '08:00:00'
condition:
  - condition: time
    before: '00:00:00'
    weekday:
      - mon
    after: '00:00:00'
action:
  - service: rest_command.monday1
    data: {}
mode: single

I have folders called monday1, monday2, monday3… then tuesday1, tuesday2 and so on for every single day of the week. and based on the day and time this triggers the playback of a playlist which i use a restful command for.

So this works out to quite a few automations overall.

Anything more efficient?

So I added the second automation to automations.yaml and restarted HA but I dont see it in the UI.
I changed the ID from the one generated for the first automation automatically to a different number. And obviously changed the alias and the other parameters. I’m confused.

this is whatr my automations.yaml currently looks like

- id: '1660822974667'

  alias: monday1

  description: ''

  trigger:

  - platform: time

    at: 08:00:00

  condition:

  - condition: time

    before: 00:00:00

    weekday:

    - mon

  action:

  - service: rest_command.monday1

    data: {}

  mode: single

- id: '1660822974612'

  alias: monday2

  description: ''

  trigger:

  - platform: time

    at: 09:30:00

  condition:

  - condition: time

    before: 00:00:00

    weekday:

    - mon

  action:

  - service: rest_command.monday2

    data: {}

  mode: single

How many times per day?
Is it the same total number of times for each day?

Yes, same total number of times per day. Correct. And same times too. But all the restful commands they trigger are different. Not one is identical to the other.

Based on your replies, I believe this can be done with a single automation. List the hours for each day (i.e. 08:00, 09:30, …).

Here’s what I suggest you try. You will need to add more Time Triggers based on that list I asked you to prepare in my previous post.

alias: example 
description: ''
trigger:
  - platform: time
    at: '08:00:00'
  - platform: time
    at: '09:30:00'
condition: []
action:
  - service: "rest_command.{{ now().strftime('%A') | lower}}{{ trigger.idx | int(0) + 1 }}"
    data: {}
mode: single

Each of the listed triggers is identified by an internal variable called trigger.idx. The first trigger’s trigger.idx has a value of 0, the second trigger’s value is 1, etc. We can use this to construct the appropriate rest_command. Your numbering scheme for the rest_commands starts with 1, not zero, so that’s why the template adds 1 to the value of trigger.idx.

To get today’s name, we use now().strftime('%A'). However it reports the name in titlecase (first character is capitalized) but we need it all in lowercase so that’s why we also use the lower filter.

Let me know if this does what you want or if you feel it needs modification.

2 Likes

This totally went over my head! :slight_smile: Will have to go over it when I have more time to figure it out.

However my original question still remains unanswered, how do I manually edit automations via yaml?

You can use the Automation Editor in YAML mode or any text editor (by default, automations created by the Automation Editor are stored in automations.yaml).

If you use the Automation Editor, upon clicking the Save button it automatically executes Check Configuration to validate whatever you created/modified. If there are any errors they’ll be reported otherwise it will proceed to execute Reload Automations.

If you use a text editor, you must manually execute Check Configuration to ensure your changes are free of syntax errors. Then you must execute Reload Automations in order to load your new/modified automation.

In either case, executing Reload Automations not only loads your new/modified automation it also reloads all of your existing automations. That means if any of your existing automations are in the process of executing some part of their action (for example, a delay or wait_template or repeat, etc) they’ll be immediately terminated and then restarted from scratch (i.e. they don’t return to where they left off). EDIT Outdated information. Now it only reloads new and modified automations (not all automations).

If you use a text editor to modify the contents of automations.yaml, be advised that the contents are ultimately managed by the Automation Editor. If you add something the Automation Editor doesn’t support, like a YAML comment, the next time you use the Automation Editor and click Save it will remove all of the comments you added. In addition, it’s particular about the way some things are formatted (notably multi-line templates) and will re-format whatever you have added to comply with its formatting rules.

For users who don’t wish to use the Automation Editor to create/modify their automations, and prefer using a text editor (like Visual Studio Code), it’s possible to split the configuration of automations. The ones created with the Automation Editor reside in the default location, automations.yaml, and the ones you create with a text editor go elsewhere, into one or even more files of your choosing. The steps to split the configuration are explained here. The result is a sub-directory containing one or more files for automations you create with a text editor. They’re no longer accessible to the Automation Editor, only to whatever text editor you use , so it can’t re-format them.

2 Likes