When sensor get no value it returns '-' character, how to test this?

Tags: #<Tag:0x00007f73b4a3e128> #<Tag:0x00007f73b4a3dfc0>

Context: I use a meteo component (meteosuisse) to check the wind speed and temperature in my area. It works great, but sometimes the component does not provide data. If I use developers tools, state value is ‘-’.

First I tried to test the state value with this code:

entity_id: input_boolean.my_boolean
service_template: >
  {% if states.sensor.my_sensor.state is defined %} 
      input_boolean.turn_on
  {% else %}
         input_boolean.turn_off
  {% endif %}

But of course it didn’t work as the returned value of the sensor was ‘-’ (minus sign).

It’s very difficult to test, because 95% of the time the sensor component returns values, but when no value is available , I have to handle it.

I’ve seen the documentation about is state there but as I cannot test it, I was wondering if it was the right way.

{% if is_state('sensor.my_sensor.state', '-') %}
      input_boolean.turn_off
{% else %}
         input_boolean.turn_on
{% endif %}

Any help welcome.

Why do you say you can’t test it? Just enter it into the Template Editor.

But, yes, it looks ok to me.

thanks, yes I know the template editor, but not all the functionalities, you mean I can just add a line before my code to define the value of a dummy sensor?
Because the real sensor will give me the ‘-’ value once per month…

You could go to the STATES tab and temporarily change the state of the sensor, then go back to the template editor (and possibly force it to refresh by making some change, like adding a space character somewhere) to see how it reacts. The only problem is that when the sensor updates itself the temporary value you wrote will be overwritten.

The other, easier way, is to change what you entered into the template editor to use a non-existent entity_id, then go to the STATES tab and “create” that entity by manually entering the entity_id.

1 Like

ok, thanks, I just tried it with the “set state” option but it always returns:

input_boolean.turn_on

there is my code:

{% if is_state('sensor.sion_wind_speed_max.state', '-') %}
      input_boolean.turn_off
{% else %}
         input_boolean.turn_on
{% endif %}

Maybe a problem with quotes? Or bad formatting?

EDIT: my fault, I had to delete the “.state” in my code, thanks!

{% if is_state('sensor.sion_wind_speed_max', '-') %}