Template isn't working with "bracket" notation

I’ve been trying to help another person in another thread to create a template sensor to return an attribute from an existing sensor. The existing sensor has an entity_id that starts (and in this case ends) with a number. The entity domain/id in question is “sensor.2”.

Here is the other thread:

I think the easiest way to go would be to rename the entity_id to not be “2” but that doesn’t solve the current question.

According to the docs on templating if the entity_id starts with a number you need to use bracket syntax to render the template correctly. See the note about half way down here for the example used:

I thought that they should just be able to change their template to follow the syntax in the example but it won’t work.

If you put the template in the template editor it renders correctly but transferring that same template in the sensor results in ‘unknown’.

I created a few sensors to see if if I could figure out if there was a syntax error but I had the same results in my tests so I’m here now to figure out if I’m doing something wrong or if this is some kind of bug.

Here are the template sensors i’ve created based on existing entities in my set up:

- platform: template
  sensors:
    1:
      value_template: "{{ states.zwave.door_window_sensor_1.attributes.battery_level}}"
    test_1:
      value_template: >
        {% if states.sensor['1'].state %} 
          {{states.sensor['1'].state}}
        {% else %} 
          Not Working          
        {% endif %}
    test_2:
      value_template: "{{states.sensor['1'].state}}"
    test_3:
      value_template: "{{ states.zwave['door_window_sensor_1'].attributes.battery_level}}"
    test_4:
      value_template: >
        {%- if states.zwave["door_window_sensor_1"].attributes.battery_level -%} 
          {{states.zwave["door_window_sensor_1"].attributes.battery_level}}
        {%- else -%} 
          Not Working          
        {%- endif -%}
    test_5:
      value_template: >
        {% if state_attr('zwave["door_window_sensor_1"]', 'battery_level') %} 
          {{states.zwave["door_window_sensor_1"].attributes.battery_level}}
        {% else %} 
          Not Working          
        {% endif %}

Here are the screen shots of the results.

putting those templates into the template editor (I added - to control whitespace for convenient layout but it didn’t effect results):

ex

and here are the sensors as rendered in the states page:

ex2

ex3

As you can see the “if” test in the template is evaluated to true but the returned state of the sensor using brackets is ‘unknown’.

Can anyone see where this is going wrong?

Sorry but I try also with your suggestion:
value_template: '{% if states.binary_sensor["2"].attributes.battery_level %}{{ states.binary_sensor["2"].attributes.battery_level}}{% else %}Unknown{% endif %}'
but I have always the same error:
Error loading /home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping in “/home/homeassistant/.homeassistant/packages/battery.yaml”, line 208, column 9 expected <block end>, but found ‘<block mapping start>’ in “/home/homeassistant/.homeassistant/packages/battery.yaml”, line 213, column 10

Can you explain me please how I can change my sensor name in the entity to solve the issue?

Thanks

Hmmm, did you try this with methods instead of the state objects?

{{ states('sensor.1') }}
{{ state_attr('zwave.door_window_sensor_1','battery_level') }}

No I never tried that since the docs say that the way I was trying “should” have worked.

But I’m pretty sure the second one would work anyway since (I think) the problem occurred only because I used the bracket notation just to test for a failure that would have otherwise rendered correctly using the entity without the brackets.