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']