Help with template sensor please :)

Hi all.
Ive been banging my head against this for ages on and off – i think ive been looking at it for too long and i am now missing something obvious.

Ive tried a few different combos (along the lines of states(‘sensor.name’) but i just cant seem to get anything to work.

The error is:
Failed config
sensor.template:
- Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ‘)’) for dictionary value @ data[‘sensors’][‘air_status’][‘value_template’]. Got ‘{% if states.sensor.thing|int < 34 %}\n Very Good\n{% elif states.sensor.thing|int < 67) %}\n Good\n{% elif states.sensor.thing|int < 100 %}\n Fair\n{% elif states.sensor.thing|int < 150 %}\n Poor\n{% elif states.sensor.thing|int < 200 %}\n Very Poor\n{% endif %}\n Hazardous’. (See ?, line ?). Please check the docs at https://home-assistant.io/integrations/template/

  - platform: template
    sensors:
      air_status:
        friendly_name: "Air Status"
        value_template: >-
          {% if states.sensor.thing|int < 34 %}
            Very Good
          {% elif states.sensor.thing|int < 67) %}
            Good
          {% elif states.sensor.thing|int < 100 %}
            Fair
          {% elif states.sensor.thing|int < 150 %}
            Poor
          {% elif states.sensor.thing|int < 200 %}
            Very Poor
          {% endif %}
            Hazardous

TIA!

  - platform: template
    sensors:
      air_status:
        friendly_name: "Air Status"
        value_template: >-
          {% if states.sensor.thing|int < 34 %}
            Very Good
          {% elif states.sensor.thing|int < 67) %}
            Good
          {% elif states.sensor.thing|int < 100 %}
            Fair
          {% elif states.sensor.thing|int < 150 %}
            Poor
          {% elif states.sensor.thing|int < 200 %}
            Very Poor
          {% else %}
            Hazardous
          {% endif %}

aww man!

i knew it was something simple. Suddenly the error message makes sense!

Theres an extra ) there, on the second condition, that I didnt notice!

Hi - I have this same error but the parentheses are on the call for is_state()

sensor:
  - platform: template
    sensors:
      front_door_state:
        friendly_name: 'Front Door state'
        entity_id: sensor.front_door_state
        value_template: >-
          {%- if is_state('sensor.front_door_access_control', '23') -%}
            Closed 
          {%- else -%}
            Open
          {%- endif -%}

Any ideas why I’m getting that error? This is almost exactly copying the example from the docs to rename a sensor. Pulling my hair out.

TIA

is_state_attr

Thanks but I don’t think it is an attribute

And I get the same error with the following:

sensor:
  - platform: template
    sensors:
      front_door_state:
        friendly_name: 'Front Door state'
        entity_id: sensor.front_door_state
        value_template: >-
          {%- if is_state_attr('sensor.front_door_access_control', '23') -%}
            Closed 
          {%- else -%}
            Open
          {%- endif -%}

Post the error message.

Sorry, was trying to minimise as it is also a ‘)’ like the OP, see below, thanks

Logger: homeassistant.components.hassio
Source: components/hassio/__init__.py:420
Integration: Home Assistant Supervisor (documentation, issues)
First occurred: 13:17:24 (21 occurrences)
Last logged: 17:01:21

Unexpected error calling config validator: 'NodeListClass' object has no attribute 'items'
Unexpected error calling config validator: 'NodeListClass' object has no attribute 'items' Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ')') for dictionary value @ data['sensors']['front_door_state']['value_template']. Got "{%- if 'sensor.front_door_access_control' == '23') -%}\n Closed \n{%- else -%}\n Open\n{%- endif -%}". (See ?, line ?).

But this is the line it thinks the error is in and I have both open and close parentheses

is_state_attr('sensor.front_door_access_control', '23')

Remove this line:

        entity_id: sensor.front_door_state

entity_id is not a valid option for a Template Sensor.

Did you save? The error message in your template is missing characters. Based on your post, you’re either looking at the wrong spot, you didn’t save your changes, or the error is old/out-of-date:

notice how your error is missing is_state(

1 Like

OK, thanks, it was supposed to be unique_id so I changed entity_id to that, however same error as previous.

I have saved it and it says saved however I changed a string in the same file and it did not update in the error message. Any idea how/why that would happen?

And it’s not an old error, the last logged time == now

You must be looking at the wrong file. What are you using to edit the file?

configuration.yaml in the file editor add-on. Going for a trusty hard reboot then I’ll try via samba.

Just to check, is it supported to have multiple tabs open for HA? Maybe that’s causing something?

After modifying the Template Sensor’s configuration, you must either execute Reload Template Entities or restart Home Assistant. If you are doing that and there is still a discrepancy between what you edited and what Home Assistant is actually using then it’s due to what Petro said.

I was definitely updating the right file and restarting HA. After reboot, I can now get it working. Thanks, not sure why the file editor was not saving properly.

I say I can get it working: I can get passed that error, but the sensor doesn’t actually work.

It says unavailable and also the UI says it doesn’t have a unique_id

This is the config:

sensor:
  - platform: template
    sensors:
      front_door_state:
        friendly_name: 'Front Door state'
        unique_id: "245345536234245234"
        value_template: >-
          {%- if is_state('sensor.front_door_access_control', 23) -%}
            Closed 
          {%- else -%}
            Open
          {%- endif -%}

I tried with the unique_id without the quotes (although I think it’s a string) and tried the 23 as a string with quotes

states are always strings so the quotes around the 23 are needed.

the unique_id is so that you can adjust the entity_id and friendly name in the frontend through the entity. Just name the unique_id front_door_state

Like Petro said, that is needed here and should have worked. However, your system is a mystery to me now that you claim the only way your Template Sensor changes in configuration.yaml are recognized is after rebooting the server. That shouldn’t be at all necessary and a simple Reload Template Entities, or restart, should do the trick. :man_shrugging:

Thanks both, appreciate your help. Yeah I tried with "23" too and updated unique_id as petro said…still unavailable, still saying “does not have a unique ID”

So this is currently my config for that sensor:

sensor:
  - platform: template
    sensors:
      front_door_state:
        friendly_name: 'Front Door state'
        unique_id: front_door_state
        value_template: >-
          {%- if is_state('sensor.front_door_access_control', '23') -%}
            Closed 
          {%- else -%}
            Open
          {%- endif -%}

Previously I have had changes in the config work from a restart, it is just when doing this sensor that I’ve had issues.

That said, I do have a couple of lingering error messages, I think from another integration that I didn’t get round to diagnosing, but I don’t think they would be affecting this sensor. I’m not sure how to troubleshoot that further as the messages are very vague:

Logger: homeassistant.helpers.check_config
Source: components/script/config.py:44
First occurred: 17:53:36 (7 occurrences)
Last logged: 18:28:17

Unexpected error validating config
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/check_config.py", line 145, in async_check_ha_config_file
    await config_validator.async_validate_config(  # type: ignore
  File "/usr/src/homeassistant/homeassistant/components/script/config.py", line 44, in async_validate_config
    for object_id, cfg in config[DOMAIN].items():
AttributeError: 'NodeListClass' object has no attribute 'items'

And

Logger: homeassistant.components.hassio
Source: components/hassio/__init__.py:420
Integration: Home Assistant Supervisor (documentation, issues)
First occurred: 17:53:36 (5 occurrences)
Last logged: 18:28:17

Unexpected error calling config validator: 'NodeListClass' object has no attribute 'items'

I’m beginning to think you have a configuration error. Any additions/modifications you make are ignored by Home Assistant until you fix that configuration error. That would explain why the mods you make to the Template Sensor aren’t being used by Home Assistant (until you give it no choice when you reboot the server).

Execute Configuration > Server Controls > Check Configuration and see what it has to say.

1 Like