Variables in automations - comprehension lack

Hi everyone,

it’s my first time trying to use variables in an automation. It’s built up as follows:


- id: medication_reminder
  alias: medication reminder
  max_exceeded: silent

  variables:
    name: Janedoe

  trigger:
    …

  action:

  - service: notify.alexa_media
    data:
      message: "{{name}}"
      target: media_player.this_device
      data:
        type: push

…

The above code is working flawless. But if I use


            message: |
              {{ [
                  "{{name}} time for your medicine!",
                  "Do not forget your medicine {{name}}?",
                  "…"
                ]|random }}

I get the error „Template variable warning: 'name' is undefined when rendering 'Do not forget your medicine {{name}}'“

Using {{name|default}} is cleaning up that. Which is a little bit… long-winded.

Is there a possibility to make it better?

You can’t nest templates. You have {{name}} inside of
{{ [ "{{name}} ...etc.. ] | random }}

Try this version which concatenates the value of the name variable to a given string.

            message: |
              {{ [
                  name ~ " time for your medicine!",
                  "Do not forget your medicine " ~ name ~ "?",
                  "Whatever else " ~ name
                ]|random }}
1 Like

So damn blind - I was too long inside this automation. Thank you so much, Taras!

1 Like