Template "syntax error" but changing sensor name fixes it?

I’m positively stumped here.

Why is this happening? All I did was remove the unique addon prefix from my sensor name (which was to match the MQTT publication topic, so I knew which addon-hostname-machine it came from), which is just normal alphanumeric letters?

I’ve spent about 3 hours banging my head on this syntax error…and it makes zero sense?

This sensor works, but the templates error with syntax errors:

Sensor:

mqtt:
  # Temperature and Humidity Reading Sensors
  sensor:

    # Outside Front Sensor
    - name: "6f2b8d81-rtl4332mqtt_HA_Addon Outside Front Sensor Report Time"
      force_update: true
      state_topic: "homeassistant/sensor/rtl433_by_source/6f2b8d81-rtl4332mqtt_HA_Addon/Acurite-Tower/1234"
      availability:
        - topic: "homeassistant/sensor/rtl433_by_source/6f2b8d81-rtl4332mqtt_HA_Addon/Acurite-Tower/1234"
          value_template: "{{ 'time' in value_json }}"
          payload_available: "True"
          payload_not_available: "False"
      value_template: >-
        {{ value_json.time }}

Sensor:


Template:

{{ states.sensor.6f2b8d81_rtl4332mqtt_ha_addon_outside_front_sensor_report_time.last_updated }}

Result:

TemplateSyntaxError: expected token 'end of print statement', got 'f2b8d81_rtl4332mqtt_ha_addon_outside_front_sensor_report_time'

-----BUT----

This sensor works, AND the templates works:

Sensor:

mqtt:
  # Temperature and Humidity Reading Sensors
  sensor:

    # Outside Front Sensor
    - name: "rtl4332mqtt_HA_Addon Outside Front Sensor Report Time"
      force_update: true
      state_topic: "homeassistant/sensor/rtl433_by_source/6f2b8d81-rtl4332mqtt_HA_Addon/Acurite-Tower/1234"
      availability:
        - topic: "homeassistant/sensor/rtl433_by_source/6f2b8d81-rtl4332mqtt_HA_Addon/Acurite-Tower/1234"
          value_template: "{{ 'time' in value_json }}"
          payload_available: "True"
          payload_not_available: "False"
      value_template: >-
        {{ value_json.time }}

Sensor:


Template:

{{ states.sensor.rtl4332mqtt_ha_addon_outside_front_sensor_report_time.last_updated }}

Result:

2023-07-16 15:01:20.640491+00:00

Entity_id that begins with a number

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']

1 Like

Did my suggestion to use bracket notation fix the reported problem?

I ultimately ended up changing the names as indicated in my original post and haven’t made anything new that starts with a number to test. I wasn’t aware there was a difference until this thread.