Using !include syntax in an Automation

Trying to pull in a random value from a file in an automation

automations.yaml

- id: '1600991651680'
  alias: TEST message
  description: ''
  trigger:
  - platform: state
    entity_id: cover.garage_door
    to: open
    for: 00:15:00
  condition: []
  action:
  - data:
      title: !include phrases_garage_door.yaml
      message: ''
    service: notify.mobile_app_my_iphone
  mode: single

phrases_garage_door.yaml

>-
  {{ [
    "\U0001F698 Garage door is open flopen!",
    "\U0001F698 Theives are happy, let's make them sad. Close the garage door.",
    "\U0001F698 Close the door more porfavor"
  ]|random }}

This is what gets loaded automatically into the UI when I refresh, after adding the !include line into the automations.yaml

Looks good but when the automation runs it returns the name of the file include file instead of a random value from it. Very odd considering the UI is showing the contents of the include file.

The strings + random syntax works as I originally had that directly in the automation but want to separate the string values out so I can expand on them and call them from other places too rather than having them buried inside the automation itself.

I’ve never seen the HA !include used to include only a portion of an entity. Maybe that’s why it doesn’t work? BTW thieves is spelled wrong :wink:

The syntax above works in scripts but not in automations (where it just returns the name of the file instead of the value(s) from the file). So I went with a script instead which I can call from the automation.

That’s interesting. Automation actions are supposed to be identical to scripts.

Scripts have other advantages for me right now anyway so I went that route. Maybe I will submit a bug if I have time.

Are you aware of the difference in calling a script directly vs using the script.turn_on service?

Basically:

service: script.your_script

Actions wait for the script to complete before progressing.
Errors in the script abort the next actions.

vs

service: script.turn_on
entity_id: script.your_script

Actions continue in parallel with the script.
Errors in the script do not abort the next actions.

Yes. What is the relevance to this thread?

Just wondering if you knew. Scripts can also have disadvantages if you call them inappropriately.

1 Like