Check if state is empty?

Hi!
I want to create a template sensor wich should check his own state:

- platform: template
    sensors:
      sonos_status_kue:
        value_template: '{% if is_state("sensor.sonos_status_kue", "") %}off{% else %}ON{% endif %}'

When I test the IF in the template development tool the result is OK.
If I try it in in my configuration and restart Home Assistant, it does not start.
the log:

Is there another way to check if a state is empty?

Thank you

What is the log output?

Oh OK,
I try it another way. With my template sensor.sonos_status_kue I want to check the state of media_player.kuche. The problem is, that the media_player.kuche is not available at this moment.
It is a Sonos speaker without energy, the media_player.kuche appears after it get’s energy. Then the states.media_player.kuche.state turns to “paused”.
But I want: when there is no media_player.kuche (the speaker is without energy) then state.sensor.sonos_status_kue should be “OFF” else (if the media_player.kuche is available) the state of the media_player.kuche

This is my try:

- platform: template
  sensors:
    sonos_status_kue:
      value_template: '{% if is_state("media_player.kuche.state", "") %}off{% else %}{{ states.media_player.kuche.state }}{% endif %}

The Log:
homeassistant.components.sensor.template: UndefinedError: ‘None’ has no attribute ‘state’

Thank you

Ok, hte log output is ok if it just after starting of hass.
So what is the problem?
Is the sensor state wrong?

Yes, the state of the sensor.sonos_status_kue is wrong- it is empty but it should be “off”

This is because the state is not initialized yet.
You can test it this way:

'{% if states.media_player.kuche %}...

This will be “false” if the object is not initialized yet (i.e. “None”) and “true” otherwise.
You can add the if condition that checks for the on/off state below that.

Sebastian

1 Like

Thank you, that is the solution!

- platform: template
    sensors:
      sonos_status_kue:
        value_template: '{% if states.media_player.kuche ==  none %}off{% else %}{{ states.media_player.kuche.state }}{% endif %}'

Id like to use your solution but when it is “on” it prints the values of the sensors, anyway just to output On if on?

hi all, its a old post, but in google very high :slight_smile:

i have the same Problem:

customize File:

sensor.tankerkoenig_dirk_schirmacher_diesel:
  friendly_name: ED Tankstelle
  value_template: >
    {% if states("sensor.tankerkoenig_dirk_schirmacher_diesel") == "unknown" %}
      geschlossen
    {% else %}
      {{ states('sensor.tankerkoenig_dirk_schirmacher_diesel') }}
    {% endif %}

how can i say: if the state is “unknown” switch the output from “unknown” to “close/geschlossen” ?
Or take the sensor value…

Thanks All.

I know this is months old but here you go:

{% if is_state("sensor.tankerkoenig_dirk_schirmacher_diesel", "unknown") %}
      geschlossen
{% else %}
      {{ states('sensor.tankerkoenig_dirk_schirmacher_diesel') }}
{% endif %}
1 Like