Can't get Jinja to work in automations - at wits end

I’m just got my HA server up and running and added a bunch of lights, sensors, etc. Now I’m trying to automate and am having a very hard time using Jinja with the YAML. I’m not clear on what a template is, is it just another name for a function? But then I see in the documentation functions and macros talked about. I’ve looked at a lot of documentation and a number of users’ posted code, but am not able to make my simple hello world script work.

As you can see below, I’ve tried a number of strategies that I’ve lifted from various posted code, but all return YAML parse or runtime errors. The returned error messages are way above my head and I can’t make since of them. For instance, I have no idea of what a “flow collection” is.

I’m at my wits end. A lot of trial and mostly errors. Am I missing something? Is there something I should read that explains this? Even a glossary would be of some help. In the “Templating” page, it says “You must surround single-line templates with double quotes (”) or single quotes (')." But does not explain why or anything about multi line code. I assume the YAML gets parsed before the Jinga, but that’s just a guess. Any help would be greatly appreciated. I would love for approach “try 4” to work, but I’d be happy for any of the approaches to work. Once I get over this hump, I can be productive again. Thank you in advance.

--------------------------------------
# TRY 1:
action: light.turn_on
target:
  entity_id: light.rgb_bulb_9_third_reality
data:
  rgb_color: 0,255,255

RunTime Error: None for dictionary value @ data['rgb_color']. Got None

--------------------------------------
# TRY 1b:
action: light.turn_on
target:
  entity_id: light.rgb_bulb_9_third_reality
data:
  rgb_color: [0,255,0]

WORKS!  (light changes color)

--------------------------------------
{## TRY 2a - in Jinja Template Editor ##}
{## choose a random color, from a list of decent colors ##}

{% set colorList=['255,0,0','255,120,20','200,200,0','0,255,0','0,0,255'] %}
{% set i = (now() | as_timestamp | int) % (colorList|count)  %}
{{ "rgb_color: [" + colorList[i] + ']'}}

WORKS - results in "rgb_color: [0,0,255]"


--------------------------------------
# TRY 2b:
action: light.turn_on
metadata: {}
target:
  entity_id: light.rgb_bulb_9_third_reality
data:
  {% set colorList=['255,0,0','255,120,20','200,200,0','0,255,0','0,0,255'] %}
  {% set i = (now() | as_timestamp | int) % (colorList|count) %}
  rgb_color: {% "[" + colorList[i] + "]" %}

Error in parsing YAML: missed comma between flow collection entries (line: 6, column: 4)

--------------------------------------
# TRY 2c:
action: light.turn_on
metadata: {}
target:
  entity_id: light.rgb_bulb_9_third_reality
data:
  {% set colorList=[[255,0,0],[255,120,20],[200,200,0],[0,255,0],[0,0,255]] %}
  {% set i = (now() | as_timestamp | int) % (colorList|count) %}
  rgb_color: {% "colorList[i] %}

Error in parsing YAML: missed comma between flow collection entries (line: 6, column: 4)

--------------------------------------
# TRY 2d:
action: light.turn_on
metadata: {}
target:
  entity_id: light.rgb_bulb_9_third_reality
data:
  "{% set colorList=[[255,0,0],[255,120,20],[200,200,0],[0,255,0],[0,0,255]] %}"
  "{% set i = (now() | as_timestamp | int) % (colorList|count) %}"
  rgb_color: "{% "colorList[i] %}"

Error in parsing YAML: bad indentation of a mapping entry (line: 7, column: 3)


--------------------------------------
# TRY 3:
action: light.turn_on
metadata: {}
- variables:
    colorList: ['255,0,0','255,120,20','200,200,0','0,255,0','0,0,255','0,0,0','0,0,0','0,0,0','0,0,0']
    i: "{{ now() | as_timestamp | int % 6 }}"
target:
  entity_id: light.rgb_bulb_9_third_reality
data:
  rgb_color: "{{  colorList[i]  }}"

Error in parsing YAML: end of the stream or a document separator is expected (line: 3, column: 1)


--------------------------------------
# TRY 4:
action: light.turn_on
metadata: {}
variables:
  colorList:
    - 255,0,0		# red
    - 255,120,20	# orange
    - 200,200,0		# yellow
    - 0,255,0		# green
    - 0,0,255		# blue
  i: "{{ now() | as_timestamp | int % (colorList|count) }}"
target:
  entity_id: light.rgb_bulb_9_third_reality
data:
  rgb_color: "{{  colorList[i]  }}"

Invalid action: extra keys not allowed @ data[0]['action']
 
 
--------------------------------------
# TRY 5:
- alias: "Using templated variables"
  variables:
    colorList:
    - 255,0,0		# red
    - 255,120,20	# orange
    - 200,200,0		# yellow
    - 0,255,0		# green
    - 0,0,255		# blue
    i: "{{ now() | as_timestamp | int % (colorList|count) }}"
    
- alias: "Turn light on"
  action: light.turn_on
  metadata: {}
  target:
  entity_id: light.rgb_bulb_9_third_reality
  data:
    rgb_color: "{{ colorList[i] }}"
    
Invalid action: expected a dictionary for dictionary value @ data[1]['target']

The configuration variable rgb_color expects a true list, not a list-shaped string. That list can be given either as a js-style list like you did in 1b, or as a YAML list like:

action: light.turn_on
target:
  entity_id: light.rgb_bulb_9_third_reality
data:
  rgb_color: 
    - 0
    - 255
    - 255

Try 2a, 2b, 2c, 2d all have issues of mixing YAML and Jinja. The Template Editor tool only “understands” Jinja, it can’t tell if the YAML is correct.

Other key concepts that are violated in these tries:

  • You cannot build YAML configuration with Jinja, it can only supply the value of a configuration variable’s key-value pair. What the Heck is a Template and how can I use it?
  • Jinja templates are local to the key they are defined under. Everything you want to access (except values already assigned to a YAML variable) needs to be under the YAML key you are creating a value for.

2c is kind of close, but it violates Template Rule #1, has part of the template outside of the configuration variable, and has no Expression.

Corrected, 2c would look like:

action: light.turn_on
metadata: {}
target:
  entity_id: light.rgb_bulb_9_third_reality
data:
    rgb_color: |
      {% set colorList=[[255,0,0],[255,120,20],[200,200,0],[0,255,0],[0,0,255]] %}
      {% set i = (now().timestamp()| int) % (colorList|count) %}
      {{ colorList[i] }}
But the template could be simplified significantly if you just want a random rgb color value
action: light.turn_on
metadata: {}
target:
  entity_id: light.rgb_bulb_9_third_reality
data:
    rgb_color: |
      {{ [[255,0,0],[255,120,20],[200,200,0],[0,255,0],[0,0,255]]|random }}

Tries 3, 4, and 5 all fail to supply a true list. You can use the split() string method to convert the comma-separated strings into a true list.
Tries 3 and 4 have a YAML variables block defined inside the action, which is invalid.
Try 5 also has incorrect indentation of entity_id: which is what causes the “expected a dictionary” error.

Corrected, with minimal changes, Try 5 would look like:

- alias: "Using templated variables"
  variables:
    colorList:
      - 255,0,0		# red
      - 255,120,20 	# orange
      - 200,200,0		# yellow
      - 0,255,0		# green
      - 0,0,255		# blue
    i: "{{ now().timestamp() | int % (colorList|count) }}"
    
- alias: "Turn light on"
  action: light.turn_on
  metadata: {}
  target:
    entity_id: light.rgb_bulb_9_third_reality
  data:
    rgb_color: "{{ colorList[i].split(',') }}"

Or another option:

  - alias: Using templated variables
    variables:
      colorMap:
        red: [255,0,0]
        orange: [255,120,20]
        yellow: [200,200,0]
        green: [0,255,0]
        blue: [0,0,255]
  - alias: Turn light on
    action: light.turn_on
    metadata: {}
    data:
      rgb_color: "{{ colorMap.values() | list | random }}"
    target:
      entity_id: light.rgb_bulb_9_third_reality
1 Like

Update – oops, I missed you mentioned that.

You can mix in the arrays, if you prefer:

sequence:
  - variables:
      colors:
        red: [255,0,0]
        green: [0,255,0]
        blue: [0,0,255]
      selected_color: green
  - action: light.turn_on
    target:
      entity_id: light.dots1
    data:
      rgb_color: "{{ colors[selected_color] }}"

I don’t fully understand but I have a toe hold and can grow / understand from here. I think a lot of what you mentioned has to do with scoping, which with your corrections i can start to understand. Didgeridrew, thank you.