Problem with templating while using name '3dprinter' in a sensor

I have a mqtt sensor getting data from a tasmota device. I have the sensor setup as follows

- platform: mqtt
  name: "3dPrinter Switch"
  state_topic: "tele/arlec-switch-1/STATE"
  value_template: '{{ value_json.POWER }}'
  json_attributes_topic: "tele/arlec-switch-1/STATE"
  json_attributes_template: "{{ value_json | tojson }}"

This works fine.

When I try and use the template platform to extract some attributes for display I get the error.

expected token 'end of print statement', got 'dprinter_switch') for dictionary value @ data['sensors']['3dprinter_switch_power']['value_template']. Got '{{ states.sensor.3dprinter_switch.attributes.POWER }}'
invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'dprinter_switch') for dictionary value @ data['sensors']['3dprinter_switch_uptime_seconds']['value_template']. Got '{{ states.sensor.3dprinter_switch.attributes.UptimeSec }}'
invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'dprinter_switch') for dictionary value @ data['sensors']['3dprinter_switch_wifi']['value_template']. Got '{{ states.sensor.3dprinter_switch.attributes.Wifi.Signal }}'. (See ?, line ?). 
- platform: template
  sensors:
    3dprinter_switch_wifi:
      value_template: '{{ states.sensor.3dprinter_switch.attributes.Wifi.Signal }}'
      unit_of_measurement: "dB"
      friendly_name: 3d Printer Switch Wifi
    3dprinter_switch_uptime_seconds:
      value_template: '{{ states.sensor.3dprinter_switch.attributes.UptimeSec }}'
      unit_of_measurement: "sec"
      friendly_name: 3d Printer Switch Uptime
    3dprinter_switch_power:
      value_template: '{{ states.sensor.3dprinter_switch.attributes.POWER }}'
      friendly_name: 3d Printer Switch
      icon_template: mdi:flash

When I use the template section of developer tools and only try and display the value of one of the attributes

{{ states.sensor.3dprinter_switch.attributes.POWER }}

I get the error

invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'dprinter_switch') for dictionary value @ data['template']. Got '\n{{ states.sensor.3dprinter_switch.attributes.POWER }}'

I suspect it is the .3 in the template name is possibly causing some issues.
Any way of getting around this without changing the name?

I just tried an experiment by switching the name of the mqtt sensor from “3dPrinter Switch” to “Switch 3dPrinter”
This in turn changed the sensor name from ‘sensor.3dprinter_switch’ to ‘sensor.switch_3dprinter’
Once I change the names in the template it all worked perfectly

1 Like

Also, unrelated, but you should take heed of the warning here and update the format of your attribute templates.

1 Like

Cool thanks @tom_l .

Note. If I use that format I still get the same error

Can you paste what you tried please?

Actually I was wrong. Looks like with the proper format of:

{{ state_attr('sensor.3dprinter_switch', 'POWER') }}

It all works

1 Like

Sort of related. Here is my problem.

How do I emulate the following

{{ states.sensor.3dprinter_switch.attributes.Wifi.Signal }}

Using the recommended format of

{{ state_attr('sensor.3dprinter_switch', 'Wifi') }}

This returns

{'AP': 1, 'BSSId': '86:83:C2:9A:9D:AC', 'Channel': 11, 'Downtime': '0T00:00:04', 'LinkCount': 1, 'RSSI': 76, 'SSId': 'coza-iot', 'Signal': -62}

Problem is getting the signal out as a template sensor

{{ state_attr('sensor.3dprinter_switch', 'Wifi').Signal }}

or

{{ state_attr('sensor.3dprinter_switch', 'Wifi')['Signal'] }}