How to merge automations using template

Hi!

i have the following automations and they work great, but i would like to combine theme in to one. im trying to do that with as many automations as i can using templates. but i get stuck on these regarding the state change. and multi entity_id in the actions any tips?

- id: '1517011965533'
  alias: AppleTV Vardagsrum Play
  trigger:
  - entity_id: media_player.apple_tv_vardagsrum
    from: idle
    platform: state
    to: playing
  condition: []
  action:
  - data:
      entity_id: script.1545874111999
    service: script.turn_on
- id: '1517012054613'
  alias: AppleTV Vardagsrum Pause
  trigger:
  - entity_id: media_player.apple_tv_vardagsrum
    from: playing
    platform: state
    to: paused
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - data:
      brightness_pct: 50
      entity_id: light.golvlampa_2
    service: light.turn_on
  - data:
      brightness_pct: 50
      entity_id: light.golvlampan_2
    service: light.turn_on
- id: '1517012153064'
  alias: AppleTV Vardagsrum play pause
  trigger:
  - entity_id: media_player.apple_tv_vardagsrum
    from: paused
    platform: state
    to: playing
  condition: []
  action:
  - data:
      entity_id: script.1545874111999
    service: script.turn_on
- id: '1517012229016'
  alias: AppleTV Vardagsrum Stop
  trigger:
  - entity_id: media_player.apple_tv_vardagsrum
    from: playing
    platform: state
    to: idle
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - data:
      entity_id: script.9998820176999
    service: script.turn_on

BR Mathias

Without going into the details yet, I’ll offer that it’s often clearer and easier to have separate automations than to try to combine them with fancy and hard-to-read constructs. When you have different triggers, different conditions, and different actions for each automation, trying to combine them is likely more trouble than it’s worth. I’ve been there.

1 Like

May I also ask why ?
It can’t be for space as I recently checked my configuration.yaml file, 532 bytes, but due to file allocation it consumes 1MB
And As Rob points out, scanning through a file when you have a vague search term will yield easier, quicker results than studying each template you come accross.
Put them all in the same package, order them alphabetically, or by device, or by room, close the package and you probably won’t return to it. So no great loss.
These also look like they were written by the automation manager (random id:'s and frequent [ ] are a dead givaway) you could compact these quite a lot, whilst making them more readable but I’m not sure what your theme is. light / media_player or what you want to do are they generally triggered by the sun going down ? Can’t see what this script 11999 does either.

Thanks for the input, my goal is to clean up my automations. But maybe you are right to leave these as they are.

The script you ask about turns of the light when going from pause to play.

And they are only working when the sun is down and it’s dark.

Br Mathias

Maybe we can help with making them more readable and combining actions that occur at the same time.
I am making an assumption here that your automations are all stored in automation.yaml. Have you considered splitting up your configuration (to make management easier) I would recommend packages but not everyone agrees, read up see what you think and ask questions when you get stuck.
Mutt

Yes, that is correct

If i split them up, will they still appear in the UI Automation editor or will it all be in yaml?

Br Mathias

Ah ! that’s the key issue.
Most people consider the automation editor to be a work in progress.
It can’t understand comments, so doesn’t know where to place them and to keep it clean, just removes them.
It also doesn’t know much about templates so I’m sure in most cases it treats them less than kindly.
If anyone gives you code (automation/script) they will get similar treatment.
So when people get to the stage where they are considering splitting up their yaml (it’s all yaml still, what you have know is yaml too) then they have to make the decision to abandon the automation manager. You can then go with your favourite editor (notpad++ is common on windows, atom on mac etc.) . Or if you like the idea of having an integrated one (i.e. on the same host - you could load ‘configurator’ (it’s an addon) not used it much myself but it seems to do the job.
Doing this is a big step and you will need time to consider what you want. But moving on opens up many possibilities. You get to work directly with code so your understanding will improve. you get to copy peoples code and put it where you want it to go. You could even take a full package (a media_player control or outside lights or colour management for RGB lights or … you get the idea).
You do need to travel at a pace that you feel comfortable with and during the transition there will be a few hiccups.

I tend to agree with the thread, keep em separate. If you want to combine them, you need a script for all the automations

script:
  apple_tv_vardagsrum_playing_paused:
    sequence:
    - data:
        brightness_pct: 50
        entity_id: light.golvlampa_2
      service: light.turn_on
    - data:
        brightness_pct: 50
        entity_id: light.golvlampan_2
      service: light.turn_on
- alias:  Super Automation
  trigger:
  - platform: state
    entity_id: media_player.apple_tv_vardagsrum
  condition:
  - condition: template
    value_template: >
      {% set just_go = [ 'idle.playing', 'paused.playing' ] %}
      {% set below_horizon = [ 'playing.paused', 'playing.idle' ] %}
      {% if trigger.to_state is defined and trigger.from_state is defined %}
        {% set state_pair = [ trigger.from_state.state, trigger.to_state.state ] | join('.') %}
        {{ ( state_pair in below_horizon and is_state('sun.sun','below_horizon')) or state_pair in just_go }}
      {% else %}
        False
      {% endif %}
  action:
    service_template: >
      {% set mapper = {
        'idle.playing':'script.1545874111999',
        'playing.paused':'script.apple_tv_vardagsrum_playing_paused',
        'paused.playing':'script.1545874111999',
        'playing.idle':'script.9998820176999' } %}
      {{ mapper[trigger.from_state.state ~ '.' ~ trigger.to_state.state] }}     

Not really ideal to be honest.

Thank you all for your inputs.

I think that I will abandon the automation editor and split it up for better order in my automations, I’m doing most of the things in the yaml files anyway.

Thanks for the automation example, I also see that it’s not worth it. But if you don’t know you have to ask😊