Template with random list of lists

I’m trying to randomize colors for my lamps using automation. But for whatever reason, I am getting

Invalid data for call_service at pos 1: None for dictionary value @ data[‘hs_color’]

The code I’m using is:

- alias: Color time
  trigger:
    platform: time_pattern
    seconds: '/3'
  action:
    service: light.turn_on
    data_template:
      entity_id: >
        light.{{ ['color_lamp_1', 'color_lamp_2', 'floor_lamp'] | random }}
      brightness: 250
      hs_color: >
        [10, {{range(30, 70) | random}}]
      transition: 6

What’s strange is that entity_id is working fine with the same approach, but hs_color gets None.

Any ideas why?

It works for entity_id because this option expects to receive a string in the form of light.something. Your template succeeds in doing that.

It fails for hs_color because this option expects to receive a list in the form of [10, 45]. However, your template is generating a string that only looks like a list.

Change it to this (i.e. remove the line-continuation character >):

      hs_color: [10, {{range(30, 70) | random}}]

The moment the YAML parser sees the > it treats the rest as a Jinja2 template and templates can only generate strings.


EDIT
The correct version, which passes Config Check, is actually:

      hs_color: [10, "{{range(30, 70) | random}}"]

Goot point. I should have mentioned it before. I tried it like this:

      hs_color: [10, {{range(30, 70) | random}}]

And I’m getting

Error loading /config/configuration.yaml: invalid key: "OrderedDict([('range(30', None), ('70) | random', None)])"
  in "/config/automations.yaml", line 33, column 0

when I’m checking the config

Try this. It’s an alternate way of defining a list:

      hs_color:
        - 10
        - {{range(30, 70) | random}}

EDIT
The correct version, which passes Config Check, is actually this:

      hs_color:
        - 10
        - "{{range(30, 70) | random}}"
  action:
    service: light.turn_on
    data_template:
      entity_id: >
        {% set h=range(10,70) %}
        light.{{ ['color_lamp_1', 'color_lamp_2', 'floor_lamp'] | random }}
      hs_color: 
        - {{ h | random }}
        - {{ h | random }}
    transition: 2

Here where I am at now. Still and error

Error loading /config/configuration.yaml: invalid key: "OrderedDict([('h | random', None)])"
  in "/config/automations.yaml", line 36, column 0

The latest example you posted is unlike the previous one. The template for entity_id defines a variable called h which you then attempt to use in the template of the hs_color option. You can’t do that. The scope of a Jinja2 template is limited to the option where it is defined.

Also, the transition option should be indented so that it aligns with the other options under data_template.

My understanding was that Jinja variables have global scope. Ok, so the variable is limited.
But your option still raises the error.

  action:
    service: light.turn_on
    data_template:
      entity_id: light.{{ ['color_lamp_1', 'color_lamp_2', 'floor_lamp'] | random }}
      hs_color:
        - 10
        - {{range(30, 70) | random}}
Error loading /config/configuration.yaml: invalid key: "OrderedDict([('range(30', None), ('70) | random', None)])"
  in "/config/automations.yaml", line 35, column 0

As an experiment, try delimiting the template with double-quotes like this:

  action:
    service: light.turn_on
    data_template:
      entity_id: light.{{ ['color_lamp_1', 'color_lamp_2', 'floor_lamp'] | random }}
      hs_color:
        - 10
        - "{{range(30, 70) | random}}"

Let me know if it continues to report the same error message.

Great stuff, mate! No error now.
I guess making it a string didn’t let Python evaluate it before Jinja parsed it.

1 Like

Yes, I now realize my original suggestion should have been like this (it passes Config Check):

      hs_color: [10, "{{range(30, 70) | random}}"]
2 Likes

I might be wrong but it looks more like YAML than Python

It is YAML which is parsed by Python and then also parsed by Jinja template parser.

In my original configuration
hs_color: [10, {{range(30, 70) | random}}]
was ready by Python, but with an error because [] didn’t contain a valid list.

After it was changed to (mind the quotes)
hs_color: [10, "{{range(30, 70) | random}}"]
it became a valid list of [int, str] after which the str was parsed by Jinja.

And if i want to randomize also the H value, not only the Saturation??