Template and entity_id beginning with a number

Hello,

I have this sensor and this template.
The documentation says this:

If your template uses an entity_id that begins with a number (example: states.device_tracker.2008_gmc) you must use a bracket syntax to avoid errors caused by rendering the entity_id improperly. In the example given, the correct syntax for the device tracker would be: states.device_tracker[‘2008_gmc’]

I did as in the example, but it doesn’t work and I get these error Message:

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token ‘,’, got ‘integer’) for dictionary value @ data[‘sensors’][‘template_433_mhz_hub_doorbell_buzzer’][‘icon_template’]. Got “{% if is_state(‘sensor[‘433_mhz_hub_doorbell_buzzer’]’, ‘buzz’) %}\n mdi:volume-high\n{% else %}\n mdi:volume-off\n{% endif %}”. (See ?, line ?). Please check the docs at Template - Home Assistant

I can’t find my mistake, can someone help?
Thank you.

platform: mqtt  
    name: 433_mhz_hub_doorbell_buzzer
    state_topic: "home/433_mhz_hub/buzzer/status"  
    value_template: '{{ value_json.status }}'

.

platform: template
sensors:
  template_433_mhz_hub_doorbell_buzzer:
    ##friendly_name: "XXX"
    value_template: "{{ states.sensor['433_mhz_hub_doorbell_buzzer'].state }}"
    icon_template: >-
      {% if is_state('sensor['433_mhz_hub_doorbell_buzzer']', 'buzz') %}
        mdi:volume-high
      {% else %}
        mdi:volume-off
      {% endif %}

You have two sets of single quotes nested in the one line.
Change one of the sets to double quotes.

HA will smile at you and remove it’s errors.

Thank you very much. Now it works.

I run in another Probelm.
I change the icon when a Sensor Node comes online.
But this doesn’t work with this:

platform: template
sensors:
  template_433_mhz_hub_status :
    ##friendly_name: "XXX"
    value_template: "{{ states.sensor['433_mhz_hub_status'].state }}"
    icon_template: >-
      {% if is_state('sensor["433_mhz_hub_status"]', 'online') %}
        mdi:wifi
      {% else %}
        mdi:wifi-off
      {% endif %}

But the same Code works with this template:

platform: template
sensors:
  template_kitchen_node_1_status:
    ##friendly_name: "XXX"
    value_template: "{{ states.sensor.kitchen_node_1_status.state }}"
    icon_template: >-
      {% if is_state('sensor.kitchen_node_1_status', 'online') %}
        mdi:wifi
      {% else %}
        mdi:wifi-off
      {% endif %}

What could be the reason for this?

In general it is suggested that you avoid using a number as the first character in any object name. It will cause problems with ANY code base, because they all do not allow it. TBH your best option is to rename the device to start with a letter.

Anways, on to your problem…

To get this working with a number, you should use the following code:

platform: template
sensors:
  template_433_mhz_hub_status :
    ##friendly_name: "XXX"
    value_template: "{{ states.sensor['433_mhz_hub_status'].state }}"
    icon_template: >
      {% if states.sensor["433_mhz_hub_status"].state == 'online' %}
        mdi:wifi
      {% else %}
        mdi:wifi-off
      {% endif %}

The reason behind this is that in order to avoid the ‘number starting variable issue’, you need to grab the state from the object, you cannot use the method is_state with a nested key grab. you have to use one or the other. Your other option is to use is_state properly, but you may run into issues with the variable starting with a number. Anyways, here is that solution:

platform: template
sensors:
  template_433_mhz_hub_status :
    ##friendly_name: "XXX"
    value_template: "{{ states.sensor['433_mhz_hub_status'].state }}"
    icon_template: >
      {% if is_state('sensor433_mhz_hub_status', 'online') %}
        mdi:wifi
      {% else %}
        mdi:wifi-off
      {% endif %}

Thank you for your explenation.
To avoid future errors and problems I am going to change the name to R433_mhz_hub_status

EDIT:
Don’t use a capital letter in the name.
Does not work: R433_mhz_hub_status
Works: r433_mhz_hub_status