Error in template, I am missing something

I have the following template:


Which throws this error:

Log Details (ERROR)

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘d_drucker’) for dictionary value @ data[‘sensors’][‘power_usage_3ddrucker’][‘value_template’]. Got ‘{{ states.switch.3d_drucker.attributes.current_power_w }}’. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/

I can not find the error, any hints?

use the proper formatting (code block) so we can see your yaml. Its kinda hard to troubleshoot the way it is

its likely because the entity_id starts with a number. here are the docs that explain what to do to make it work. see the note about half way down:

be aware tho that i was just trying to help someone else with one of their templates with an entity_id that started with a number following the rules in the referenced docs and i wasn’t successful. eventually i decided the easiest thing to do was change the entity_id to not start with a number and that solved their problem.

@finity is correct. It is because your object_id starts with a number.

To work around this use the following template.

- platform: template
  sensors:
    power_usage_3d_drucker:
      value_template: "{{ states.swtich['3d_drucker'].attributes.current_power_w }}"
      friendly_name: '3D Drucker'
      unit_of_measurement: 'W'

But I highly suggest changing your object_id from 3d_drucker to something else.

1 Like

just to correct a mis-spelling in the template above (“switch” instead of “swtich”) use this:

value_template: "{{ states.switch['3d_drucker'].attributes.current_power_w }}"

but @petro I’m honestly not sure that will work. I was working on trying to help another user last week or so with the same situation and I could never get the template to work.

here is the thread I started to see if anyone else had any better insight why it wouldn’t work but I never got any replies:

1 Like

They may have made changes to make all numbers fail as an object_id. I’ve seen it work for some configurations. I personally avoid it so I’ve never had to deal with it.

Best place to play around would be inside the template editor.

Yeah, and that’s the strange part about it.

It worked fine in the template editor but as soon as I moved the same template into an automation it would fail.

Yeah, I just saw that in your other post. Take a look at my response there

I could fixt it by chaning the entity name to something which doesn’t start with a number, but I don’t get the point. I can name entities starting with a number,but the template doesn’t work…

did you look at the link in my post #3 above?

it tells you why in that link.

The point is that code does not allow variables to start with a number. So when you try to access the state object, it screws up because a number is first.

This is not allowed in any code langauge:

variable.or.method.name.that.starts.with.number.like.this.3d
                                                          ^
                                                          |

So while you can make a object_id start with a number, as soon as you try to access that state object, it’s going to break because code doesn’t allow it.

Templates are code, the language is Jinja2. Jinja2 runs on python. Python does not allow numbers to start method or variable names. So this code will error in the template:

states.swtich.3d_drucker.attributes.current_power_w
              ^
              |
        Because this is wrong

So in order to get around these types of issues you have 1 of a few options:

  1. Don’t name any object_id starting with a number. entity_id = domian.object_id. For example light.livingroom where light is the domain and livingroom is the object_id.
  2. Use other ways to access state. Example: states('domain.object_id') or states('light.livingroom'). This may not work all the time because again, numbers starting object_id’s are a no-no.

hopefully that clears this up.

So, in summery: The template doesn’t work because you are using a number first as the object_id.

And just to drive this point home… read your error. It specifically tells you what it doesn’t understand:

What preceeds d_drucker? The number 3, which is the cause of your error and problem.

Here’s some quick light reading explaining.