Platform template is causing an error

Hi all,

I’m trying to create a template configuration but I suspect something is wrong with it?

  - platform: template
    sensors:
      sma_solar_power_filtered:
        friendly_name: Solar Power
        unit_of_measurement: 'kW'
          # The register returns a value of "2147483648" when no solar is available.
          # This template filters that value out as 0
        value_template: >-
          {% if states('sensor.SMA_Solar_Power') >= 20000 %}
            0
          {% else %}
            {{ states('sensor.SMA_Solar_Power') }}
          {% endif %}

It validates fine, but when I try to restart HA I see this on the screen and restart doesn’t happen:
image

The only log I see in the system logs looks unrelated and doesn’t come up if this template isn’t there. The modbus config just above which seems to be functioning just fine otherwise:

Any thoughts?

You dont have to restart HA in order to pick up template changes.

Under development tools → YAML, you can refresh templates.

try changing {% if states('sensor.SMA_Solar_Power') >= 20000 %} to
{% if states('sensor.SMA_Solar_Power') | float(0) >= 20000 %}

My guess is the template is trying to render before the sensor exists.

You can only have one

template: 
  - sensor:

In your configuration.yaml, otherwise only one sensor will be found

1 Like

Thanks for the suggestion. But no joy.

They aren’t using the “template” domain. They are using the “sensor” domain and “template” platform. So that part is fine.

To the OP:

the problem is that your entity_id is wrong. Entity_id’s don’t use upper case letters.

It should be (assuming the entity_id is also otherwise correct):

       value_template: >-
          {% if states('sensor.sma_solar_power') >= 20000 %}
            0
          {% else %}
            {{ states('sensor.sma_solar_power') }}
          {% endif %}

Thanks @francisp I took another look at the documentation and updated the yaml.

The annoying thing is that every time I revisit the install, HA seems to have a heap of changes that break it’s backward compatibility. Most of my 2021 configuration isn’t compatible with the latest 2023 version of HA any more!

template:
  - sensor:
      - name: "Solar Power"
        unit_of_measurement: "kW"
        state: >
          {% if states('sensor.SMA_Solar_Power') | float(0) >= 20000 %}
            0
          {% else %}
            {{ states('sensor.SMA_Solar_Power') }}
          {% endif %}

You’re still using caps in your template… :thinking:

works fine. That’s the ID of the sensor defined earlier in the config as part of the modbus sensor definitions.

If you go to Developer Tools > States and find the sensor’s entity_id, it will be entirely in lowercase.

The entity_id of all entities is always a ‘slug’ limited to lowercase alphabetic ASCII characters, numbers, and underscores.

Sure I note that you are correct. It is displayed in lower case in the states list.

I also note that it has been for many years and is currently working with the capital letters. Hence proof that it is not case sensitive.

Having said that, I guess is would be good practice to make it lower case as per the entity.

Agreed; it’s good practice to use the entity’s actual entity_id.