Script: Template returns None

Hi,

I am trying to write a script that loads rgb colors from a variable and sets a light accordingly.
The var has the value [255,0,0]

The script:

sofa_group_set_rgb:
  alias: Sofa_Group_Set_RGB
  sequence:
  - service: light.turn_on
    data:
      rgb_color: '{{ states(''input_text.sofa_group_status'') }}'
    entity_id: light.sofa_group
  mode: single

In developer tools > templates the templates returns the correct value.
But when I run the script I get:

Sofa_Group_Set_RGB: Error executing script. Invalid data for call_service at pos 1: None for dictionary value @ data[‘rgb_color’]

I already tried the template and the states( with different quotes: ’ and " and ‘’ all the same result

Anybody has an idea on this?
The variable is not changed or touched by any other script or automation.

Thank you,
Stefan

In all versions of Home Assistant, including the current one (0.116.X), the output of a template is always a string.

That why the error message indicates you are supply ‘Invalid data’ for rgb_color. It is expecting to receive a list but the value produced by the template is a string (even though what you entered may look like a list).

This will change in 0.117 when templates can produce other types than only string (list, dict, float, integer, boolean).

Excellent, it’s actually been merged! Frenck was coy in Discord about whether it was going in :slight_smile:.

Thank you, that might explain it! But I think when e.g. supplying a float instead of an int, it does say exactly that in the error message. With this error message, I thought “How can it be, that None is returned?” and would have never guessed, that a wrong data type might be the problem…

Ok, I tried converting it to a list now, but I still get the same error message. Output in Dev Tools really shows a list now. This is the script now (I know, there might be a shorter way, but I think it should still work):

sofa_group_set_rgb:
  alias: Sofa_Group_Set_RGB
  sequence:
  - service: light.turn_on
    data:
      rgb_color: >-
        {% set var1 = (states('input_text.sofa_group_status') | replace('[','') | replace(']','')) %}
        {% set list1 = var1.split(',') %}
        {{list1}}
    entity_id: light.sofa_group
  mode: single

I also tried the solutions from this post: Help with RGB light automation

First one, with a list in yaml-format:

sofa_group_set_rgb:
  alias: Sofa_Group_Set_RGB
  sequence:
  - service: light.turn_on
    data:
      rgb_color: >
        {% set var1 = (states('input_text.sofa_group_status') | replace('[','') | replace(']','') | replace(' ','')) %}
        {% set list1 = var1.split(',') %}
        - {{ list1[0] | int }}
        - {{ list1[1] | int }}
        - {{ list1[2] | int }}
    entity_id: light.sofa_group
  mode: single

Second one, with passing a list (This one does not work at all, it says some formatting errors):

sofa_group_set_rgb:
  alias: Sofa_Group_Set_RGB
  sequence:
  - service: light.turn_on
    data:
      rgb_color: >
        {% set var1 = (states('input_text.sofa_group_status') | replace('[','') | replace(']','') | replace(' ','')) %}
        {% set list1 = var1.split(',') %}
          ['{{list1[0] | int }}',
	      '{{list1[1] | int }}',
	      '{{list1[2] | int }}']
    entity_id: light.sofa_group
  mode: single

Ok, please don’t kill me for bad coding style, but this works fine :joy:

sofa_group_set_rgb:
  alias: Sofa_Group_Set_RGB
  sequence:
  - service: light.turn_on
    data:
      rgb_color: 
        - "{{ (states('input_text.sofa_group_status') | replace('[','') | replace(']','') | replace(' ','')).split(',')[0]}}"
        - "{{ (states('input_text.sofa_group_status') | replace('[','') | replace(']','') | replace(' ','')).split(',')[1]}}"
        - "{{ (states('input_text.sofa_group_status') | replace('[','') | replace(']','') | replace(' ','')).split(',')[2]}}"
    entity_id: light.sofa_group
  mode: single

It seems, as stated in the link i posted above, that as soon as I use > it is not taken as list anymore, no matter what I am doing… So I cannot use multi line code.

sofa_group_set_rgb:
  alias: Sofa_Group_Set_RGB
  sequence:
  - service: light.turn_on
    data:
      rgb_color: 
        - "{{ (states('input_text.sofa_group_status') | from_json)[0]}}"
        - "{{ (states('input_text.sofa_group_status') | from_json)[1]}}"
        - "{{ (states('input_text.sofa_group_status') | from_json)[2]}}"
    entity_id: light.sofa_group
  mode: single

The reason why this works is because a template is not being used in an attempt to create a list. The list is defined in YAML format and then each item in the list is a separate template.

FWIW, it’s the most common way of templating for options that require a list containing a fixed number of items (like rgb_color which requires three items). Where this technique fails is if the list can contain a variable number of items.

Ok, thank you for that explanation!
Seems a little bit complicated, but at least it works now :slight_smile:

No problem.

In my original post, in addition to explaining why it doesn’t work, I probably should’ve directed you to this old post. It would’ve spared you the experimentation you went through trying to make a template produce a list.

1 Like