#Setting a valid dictionary for the action of an automation

1 messages · Page 1 of 1 (latest)

hoary gust
#

Hi, I'm looking for YAML help:
I have this automation ```yaml

  • alias: device restarter
    id: '1701106234000'
    trigger:
    • platform: time
      at: "12:09:15"
      id: tr1
      variables:
      my_action: [{'service': 'script.turn_on', 'target': {'entity_id': 'script.my_script'}}]
      action: # "{{ my_action }}"
    • delay: 1
and as you can see, I want to set the action dynamically.
#

When I run this exact automation, the trace displays this: ```
...
user_id: null
trigger:
id: tr1
idx: '0'
alias: null
platform: time
now: '2023-11-28T12:09:15.176709+01:00'
description: time
entity_id: null
my_action:

  • service: script.turn_on
    target:
    entity_id: script.my_script
so it looks like it should work the way I tried, by saying `action: "{{ my_action }}"`
but it doesn't, HA logs say ```
Automation with alias 'device restarter' could not be validated and has been disabled: expected dictionary @ data['action'][0]. Got '{'
devout terrace
#

You can't template service calls that way

hoary gust
#

I thought it could work, because I can do service: "{{ my_service }}"...

#

I figured if I can substitute one mapping I can maybe substitute all

#

and the trace says it's a valid list of dictionaries, doesn't it?

devout terrace
#

Yes, you can template the service

#

You can't template the entire call

hoary gust
#

hmm okay 😌 thank you

#

For anyone who needs this, this works: ```yaml

  • alias: device restarter
    id: '1701106234000'
    trigger:
    • platform: time
      at: "12:29:55"
      id: tr1
      variables:
      call1: {'service': 'script.togglekarl', 'data': {'extravariable': 'boingboing'}}
      action: # "{{ my_action }}"
    • service: "{{ call1.service }}"
      data: "{{ call1.data}}"
hoary gust
#

ps: ```yaml
action:
- alias: "IF call1 exists, then execute"
if:
- "{{ call1 is defined }}"
then:
- alias: call1
- alias: "IF call2 exists, then execute"
...

umbral oriole
#

no need to check or an if statement

#

just list out actions

#

no actions, nothing to repeat

#

There was a PR at one point to template the action section, never went up for review

#

You can also use yaml in variables to make it easier to type out

#

e.g.

  trigger:
    - platform: time
      at: "12:29:55"
      id: tr1
      variables: 
        actions:
        - service: script.togglekarl
          data: 
            extravariable: boingboing
  action:
  - repeat:
      for_each: "{{ actions }}"
      sequence:
      - service: "{{ repeat.item.service }}"
        data: "{{ repeat.item.data }}"
hoary gust
#

daaaang that is way cooler! 🤩 Thank you so much!!

#

I love it!

umbral oriole
#

It's great, I do it quite a bit. Makes it nice to mix w/ templating because you can completely avoid choose

hoary gust
#

Ohhh that's awesome! Is your config up on the web somewhere?