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):
and here are the sensors as rendered in the states page:
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?