Template Sensors not showing up after updating to 2023.6.1

So I just updated from 2023.4.0 to 2023.6.1. Everything is working great, except I noticed that none of my template sensors were working. It’s not like they were in an error state - they just were not showing up in Developer Tools > States. It’s as if they did not exist.

I ran a quick seach online and came across an article which mentioned this happening becuase it’s returning a non-numerical result with “unit_of_measurement”. The article suggested to delete “unit_of_measurement” from my sensors. Sure enough, they started working. (Article link: https://community.home-assistant.io/t/templates-misbehaving-in-2023-5/567998/23

Now here’s the thing. My front end is completely made up of multiple Picture Elements Cards with the template sensors on them and the unit of measurement is used as the badge. For example, in the snippet below:

image

The top 4 “controls” are state badges which use the template sensors as the entity.

Here’s the code I use in configuration.yaml:

      - name: "Bedroom Cooling Sensor"
        unit_of_measurement: "Air Con"
        state: >
          {% if is_state('climate.bedroom_ac', 'off') %}
            Off
          {% elif state_attr('climate.bedroom_ac', 'hvac_action') == 'idle' %}
            Idle
          {% elif is_state('climate.bedroom_ac', 'cool') %}
            On
          {% else %}
            Error
          {% endif %}

  - sensor:
      - name: "Bedroom Temperature"
        unit_of_measurement: "Temp"
        state: "{{ states('sensor.rm4_mini_temperature') | int }}°C"

  - sensor:
      - name: "Bedroom Humidity"
        unit_of_measurement: "Humidity"
        state: "{{ states('sensor.rm4_mini_humidity') | int }}%"

  - sensor:
      - name: "Bedroom Air Conditioner Fan Speed"
        unit_of_measurement: "Speed"
        icon: mdi:fan
        state: >
          {% if is_state('input_select.br_air_conditioner_fan_speed_selector', 'low') %}
            Low
          {% elif is_state('input_select.br_air_conditioner_fan_speed_selector', 'medium') %}
            Med
          {% elif is_state('input_select.br_air_conditioner_fan_speed_selector', 'high') %}
            High
          {% else %}
            Error
          {% endif %}

Here’s the code I use on the tile:

  - type: state-badge
    entity: sensor.bedroom_cooling_sensor
    title: Toggle AC
    tap_action:
      action: call-service
      service: input_boolean.toggle
      service_data:
        entity_id: input_boolean.br_cooling_switch
    hold_action:
      action: none
    style:
      top: 7%
      left: 2%
      color: transparent
      '--ha-label-badge-font-size': 1.5em
      transform: none
  - type: state-badge
    entity: sensor.bedroom_temperature
    title: Bedroom Temperature
    style:
      top: 7%
      left: 17%
      color: transparent
      '--ha-label-badge-font-size': 1.5em
      transform: none
  - type: state-badge
    entity: sensor.bedroom_humidity
    title: Bedroom Humidity
    style:
      top: 7%
      left: 32%
      color: transparent
      '--ha-label-badge-font-size': 1.5em
      transform: none

After updating to 2023.6.1 (I skipped 2023.5 so not sure if the issue was seen there), all of these template sensors stop working, unless I remove “unit_of_measurement”. In which case, I lose the names of the badges seen in the snippet and I just see the circle with the state of the entity. This looks very ugly and has essentially killed my front end. See below:

image

Would anyone have any idea how I should go about here? In case you’re wondering, this is what my complete front end should look like and it uses template sensors a whole lot!

Any help would be appreciated.

i have a similar problem, but i notice that only the first 5 sensors show up.

I just updated my post with the code I’m using. How have you coded yours? I’m wondering what is different for you that you had atleast 5 show up…

Remove the unit_of_measurement option for all your sensors that have a non-numeric output. This is no longer allowed,

here are mine:

#################################################
########### Home Cinema Sensors KODI  ###########
#################################################

##Movie 1
  - platform: template
    sensors:
      kodi_recently_movies_1:
        friendly_name: "Kodi Movie 1 Poster"
        icon_template: mdi:movie-roll
        value_template: >-
          {{ state_attr('sensor.kodi_recently_added_movies', 'data').split('{')[2].split('poster": "')[1].split('"},')[0]}}

      kodi_recently_movies_name_1:
        friendly_name: "Kodi Movie 1 name"
        icon_template: mdi:movie-roll
        value_template: >-
          {{ state_attr('sensor.kodi_recently_added_movies', 'data').split('{')[2].split('"title": "')[1].split('",')[0]}} 

      kodi_recently_movies_2:
        friendly_name: "Kodi Movie 2"
        icon_template: mdi:movie-roll
        value_template:  >-
          {{ state_attr('sensor.kodi_recently_added_movies', 'data').split('{')[3].split('poster": "')[1].split('"},')[0]}}

      kodi_recently_movies_3:
        friendly_name: "Kodi Movie 3"
        icon_template: mdi:movie-roll
        value_template:  >-
          {{ state_attr('sensor.kodi_recently_added_movies', 'data').split('{')[4].split('poster": "')[1].split('"},')[0]}}

      kodi_recently_movies_4:
        friendly_name: "Kodi Movie 4"
        icon_template: mdi:movie-roll
        value_template:  >-
          {{ state_attr('sensor.kodi_recently_added_movies', 'data').split('{')[5].split('poster": "')[1].split('"},')[0]}}
        
      kodi_recently_movies_5:
        friendly_name: "Kodi Movie 5"
        icon_template: mdi:movie-roll
        value_template:  >-
          {{ state_attr('sensor.kodi_recently_added_movies', 'data').split('{')[6].split('poster": "')[1].split('"},')[0]}}

      kodi_recently_movies_6:
        friendly_name: "Kodi Movie 6"
        icon_template: mdi:movie-roll
        value_template:  >-
          {{ state_attr('sensor.kodi_recently_added_movies', 'data').split('{')[7].split('poster": "')[1].split('"},')[0]}}
        
      kodi_recently_movies_7:
        friendly_name: "Kodi Movie 7"
        icon_template: mdi:movie-roll
        value_template:  >-
          {{ state_attr('sensor.kodi_recently_added_movies', 'data').split('{')[8].split('poster": "')[1].split('"},')[0]}}


I see you edited your post. Yes the the unit_of_measurement is no longer allowed for non-numeric sensors. There is no way around this requirement.

You will have to come up with some other display method.

Personally I never liked the look of badges and use the custom button card.

Another option could be the new tile cards or if they are too limited, the custom mushroom card.

Apologies for bumping an old thread, but in case any one is interested, I have a solve.

As seen in the situation above, any sensor with a non-numeric value cannot have a Unit of Measurement (UoM) as it is not supported any more. Due to this, I have been running on 2023.4.0.

Today, I finally upgraded to 2024.2.2, and as expected, faced the same issue. However, I have managed to figure out a work around. Try these steps out:

  • In the non numeric sensor code, add the availability function. Just make sure that the entity is one which will ALWAYS a non numeric value so the sensor would be unavailable. Just add a light or fan entity. This will cause the sensor to be forever unavailable and instead of not working, it will be displayed as an unavailable badge. See below:

unavai

My code reads like this:

  - sensor:
      - name: "Bedroom Fan Sensor"
        unit_of_measurement: "Fan"
        state: >
          {% if is_state('light.bedroom_fan', 'off') %}
            Off
          {% elif is_state('light.bedroom_fan', 'on') %}
            On
          {% else %}
            Offline
          {% endif %}
        availability: "{{ is_number(states('light.bedroom_fan')) }}"

Now that we have the badge displaying as unavailable, we just need to change its value to “On” or “Off”. If you go to Developer Tools > States, and then change the value manually, you’ll see that the badge is displayed exactly as before. The key is to now automate the state change of this unavailable sensor on the basis of the entity it represents. However, there is no function in HA which allows us to do this.

A quick search led me to this video - https://www.youtube.com/watch?v=V2-bwsTLjh4 which walks you through installing a small Python script which creates a service we can use to set the state

Follow the instrunctions in the video and you will end up with a service named “python_script.set_state”. You can use this to change the state of the unavailable sensor. All that needs to be done is to create an automation which sets the required state. Here’s the code I am using

alias: Hack - Set UI Fan Sensor Value as per fan state
description: ""
trigger:
  - platform: state
    entity_id:
      - light.bedroom_fan
condition: []
action:
  - service: python_script.set_state
    data_template:
      entity_id: sensor.bedroom_fan_sensor
      state: "{{trigger.to_state.state | title}}"
mode: single

fan

This has worked beautifully for me and I just had to share it with the community. I hope this helps.

Cheers

1 Like