Unchanged Template Sensor is now broken?

I have this template sensor in configuration.yaml which worked fine until a recent update to HA (I had made no changes to it). It is a customized battery sensor that used to have text that would look like this:

Charging: 75%

  • or the like. Anyone have any ideas (all the details are below):
sensor:
  - platform: template
    sensors:
      battery_status_kruse_cell:
        friendly_name: Kruse's Cell Battery
        value_template: >-
          {% if is_state('binary_sensor.kruse_s_s23_ultra_is_charging','on') %}
             Charging: {{ states('sensor.kruse_s_s23_ultra_battery_level') }}
          {% else %}
             Not Charging: {{ states('sensor.kruse_s_s23_ultra_battery_level') }}
          {% endif %}
        icon_template: >-
          {% set batterylevelicon = states('sensor.kruse_s_s23_ultra_battery_level') | int(0) // 10 * 10 %}
          {% if batterylevelicon == 100 %}
             mdi:battery
          {% else %}
             mdi:battery-{{batterylevelicon}}
          {% endif %}
        unique_id: battery_status_kruse_cell
        unit_of_measurement: "%"

As you can see from within Developer Tools → States, the sensor shows as unknown - with a weird value forthe battery icon (the sensopr is still unknown if I just hard code the icon to ‘mdi:battery’, and I have checked to make sure all the battery icons (such as ‘mdi:battery-50’ are all still available):

But the same code works in the Developer Tools → Template editor as shown here - even when I try to set a value that is not 100% for the battery, for debugging purposes…

Anyone have any ideas? this seems odd!

You’ve given it a unit_of_measurement but then set the state to something that isn’t a number. You’ll probably find an error about this in the logs. Remove the unit_of_measurement and see if that fixes it.

If it doesn’t, please show the state of your battery level sensor.

1 Like

That was it, thank you - I just commented out the unbit of measurement (and added the % to the end of the Charging / Not Charging text lines), and the issue went away - I do have a unit of measurement log error but that is in in/hr for a weather station so I have to dig into that one separately! Thanks again

1 Like

One other quick question @Troon , in my syslog before my correction I see I had this error:

May 4 22:02:06 kruse-pi homeassistant[599]: ValueError: Sensor sensor.battery_status_kruse_cell has device class None, state class None unit % and suggested precision None thus indicating it has a numeric value; however, it has the non-numeric value: Not Charging: 97 (<class ‘str’>)#033[0m

And after the correction I still have this “ValueError”:

May 5 08:01:26 kruse-pi homeassistant[599]: ValueError: Sensor sensor.battery_status_kruse_cell has device class None, state class None unit % and suggested precision None thus indicating it has a numeric value; however, it has the non-numeric value: Charging: 100 (<class ‘str’>)#033[0m

So, “string_template” instead of “value_template” doesnt work, “value_template” does now work but has the above log error… idea for what variable to show there instead of “value_template” since I have seen examples of text being used with a huge number of them still using “value_template”?

The error still says that sensor.battery_status_kruse_cell has a unit of %. Have you reloaded? Please show a screenshot from Developer Tools / States like this but for your sensor:

Are you aware of the existence of the official documentation? Where did string_template come from?

please don’t say ChatGPT

1 Like

@Troon it was a guess - I didn’t see anything listed as a template that would fit. Not Chatgpt lol!

Yes I did reload - actually the “reload yaml” doesn’t do it I have to restart HA… In any event, here is the screenshot and below that again the most recent syslog error and configuraiton.yaml text - just trying to get rid of the error message now -

Developer Tools → State:

From Syslog:

May 5 08:01:26 kruse-pi homeassistant[599]: ValueError: Sensor sensor.battery_status_kruse_cell has device class None, state class None unit % and suggested precision None thus indicating it has a numeric value; however, it has the non-numeric value: Charging: 100 (<class ‘str’>)#033[0m

Code in configuration.yaml. It may just be that the error message is showing the additional text now ‘%’ I threw on the end of those lines…

sensor:
  - platform: template
    sensors:
      battery_status_kruse_cell:
        friendly_name: Kruse's Cell Battery
        value_template: >-
          {% if is_state('binary_sensor.kruse_s_s23_ultra_is_charging','on') %}
             Charging: {{ states('sensor.kruse_s_s23_ultra_battery_level') }}%
          {% else %}
             Not Charging: {{ states('sensor.kruse_s_s23_ultra_battery_level') }}%
          {% endif %}
        icon_template: >-
          {% set batterylevelicon = states('sensor.kruse_s_s23_ultra_battery_level') | int(0) // 10 * 10 %}
          {% if batterylevelicon == 100 %}
             mdi:battery
          {% else %}
             mdi:battery-{{batterylevelicon}}
          {% endif %}
        unique_id: battery_status_kruse_cell

It does show as this - works - but I am just trying to get rid of the log error message:

image

Update: this appears to be a bug in one of the latest releases:

1 Like