Template does not work

I define a new sensor, thus

- platform: template

  sensors:

    сolor_base:

      friendly_name: "Current base night light color"

      unique_id: sensor.rgb.base

      value_template: >-

        {% if state_attr('light.gateway_light_34ce0088c1a9', 'rgb_color') == '(0,0,255)' %}

          blue

        {% elif state_attr('light.gateway_light_34ce0088c1a9', 'rgb_color') == '(0,255,0)' %}

          green

        {% elif state_attr('light.gateway_light_34ce0088c1a9', 'rgb_color') == '(255,0,0)' %}

          red

        {% endif %}

But when checking the server swears, there are ideas what is wrong here

A light’s rgb_color attribute exists only when the light is on. When the light is off the attribute doesn’t exist so any template that attempts to access the non-existent attribute will not succeed.

it burns, but the server still swears

Invalid config for [sensor.template]: invalid slug сolor_base (try solor_base) for dictionary value @ data['sensors']. Got OrderedDict([('сolor_base', OrderedDict([('friendly_name', 'Current base night light color'), ('unique_id', 'sensor.rgb.base'), ('value_template', "\n{% if state_attr('light.gateway_light_34ce0088c1a9', 'rgb_color') == '(0,0,255)' %}\n\n blue\n\n{% elif state_attr('light.gateway_light_34ce0088c1a9', 'rgb_color') == '(0,255,0)' %}\n\n green\n\n{% elif state_attr('light.gateway_light_34ce0088c1a9', 'rgb_color') == '(255,0,0)' %}\n\n red\n\n{% endif %}")]))]). (See ?, line ?).

Everything worked, only sensor.color_base does not work, and does not set the correct color name

If you are using Google Translate, you should know that this is almost meaningless in English. I assume you are trying to say that the light is on but Home Assistant still reports an error.

I found the problem.

The error message reports:

invalid slug сolor_base (try solor_base)

It is saying that the name “color_base” is unacceptable and suggests “solor_base” as a replacement. The important point is that the first letter of “color_base” is invalid and so it offers “solor_base”.

I examined the hex codes for the text you posted. Each letter has a corresponding hexadecimal number equivalent (hex code). For example, the hex code for the letter c is 63.

However, the hex code for the the first letter of color_base is not 63. It is a combination of two hex codes:

I don’t know what kind of keyboard or text editor you are using but somehow you entered what is known as a double-byte character. This kind of character is unacceptable for use when defining an entity’s name.

Here is the error message when I test it with the c double-byte character:

That message is not produced after I replace the double-byte c in color_base with a proper single-byte c.
Screenshot from 2020-11-21 16-30-21

So all you need to do is delete the first c in the word color_base and replace it with a proper, single-byte c.

If you are using version 0.118.X then this should work:

- platform: template
  sensors:
    color_base:
      friendly_name: "Current base night light color"
      value_template: >-
        {% set rgb = state_attr('light.gateway_light_34ce0088c1a9', 'rgb_color') %}
        {% if rgb == (0,0,255) %} blue
        {% elif rgb == (0,255,0) %} green
        {% elif rgb == (255,0,0) %} red
        {% else %} other
        {% endif %}

You are absolutely right, it turned out to be the Russian letter S, in spelling it is completely similar to the letter C. However, you should know this yourself, you have a very Ukrainian name))

1 Like

Ah! Had I known you use the Cyrillic alphabet then it might have been easier to spot the alternative meaning of “c”.

As for my name, there have been very few opportunities for me to spell it using the Cyrillic alphabet (Тарас).