Template binary sensor issue with 'unavailable' value

Hi there, pretty new to HA, so please bear with me.

I’m working on a binary sensor template which looks like:

{% set setpoint = states('input_number.suite_setpoint_calefaccion') %}
{% set current_temp = states('sensor.termostato_suite_sonoff_temperature') %}
{% set setpoint_offset = states('input_number.offset_setpoint_calefaccion') %}
{% if not(setpoint|is_number) or not(current_temp|is_number) or not(setpoint_offset|is_number) %}
{{ False }}
{% endif %}

When sensor.termostato_suite_sonoff_temperature is unavailable the binary sensor shows as unavailable.
I thought the is_number checks I’m doing would catch this and end up returning False but it doens’t seem so.
What I’m missing here?

Thanks!

It worked for me when I entered a device that doesn’t have a numeric state.

{% set setpoint = states('alarm_control_panel.ha_alarm') %}
{% set current_temp = states('input_number.test3') %}
{% set setpoint_offset = states('input_number.test3') %}
{% if not(setpoint|is_number) or not(current_temp|is_number) or not(setpoint_offset|is_number) %}
  True
 {% else %}
  False
{% endif %}

I tested it with a default.

Are you sure the current state of sensor.termostato_suite_sonoff_temperature is unavailable when you tested it?

Yes, current status is unavailable. I checked this in DevTools | Templates.

I should add that this happens inside the Template options in the binary sensor dialog, not sure if the evaluation there may be slightly different.

I was testing it in Dev Tools too. Switch the setpoint with a entity that definitely use a non-number state like a light or a thermostat. You should see the change in DevTools

I tested switching a number to null and indeed it’s not changing the template. The logic most likely need changed.

Use has_value instead. I would also apply this when the variables are set, as it will make the logical expression more readable.


{% set setpoint = states('alarm_control_panel.ha_alarm') | has_value %}
{% set current_temp = states('input_number.test3') | has_value %}
{% set setpoint_offset = states('input_number.test3') | has_value %}
{{ not(setpoint) or not(current_temp) or not(setpoint_offset) }}
1 Like

I’m missing something in that format style. I went with…

{% if has_value(input_number.suite_setpoint_calefaccion') and has_value('sensor.termostato_suite_sonoff_temperature') and
 has_value('sensor.termostato_suite_sonoff_temperature')%}
  true
 {% else %}
  false
{% endif %}
1 Like

I also switched to AND from OR

Thank you! Didn’t know about has_value and it does indeed seem to do the trick, I get a proper evaluation now and can return either true or false when no numeric values are provided.

A related question, is {{this.state}}} the right way to return current sensor value? I added it and the dreaded unavailable got back…

{% set setpoint = states('input_number.suite_setpoint_calefaccion') | has_value %}
{% set current_temp = states('sensor.termostato_suite_sonoff_temperature') | has_value %}
{% set setpoint_offset = states('input_number.offset_setpoint_calefaccion') | has_value %}
{% if not(setpoint) or not(current_temp) or not(setpoint_offset) %}
{{ this.state }}
{% elif is_state('input_boolean.bloqueo_calefaccion', 'on') %}
False
{% elif current_temp < setpoint %}
True
{% elif current_temp >= setpoint and current_temp <= (setpoint + setpoint_offset / 2) %}
True
{% else %}
False
{% endif %}

The above code returns unavailable because of the {{this.state}}, if I change that line to either True or False I get rid of the unavailable state.

Any hints?

If you’re running this in the template editor, it won’t work. We haven’t seen the rest of your definition: Is it a binary_sensor or sensor?

1 Like

It’s a binary sensor and what I posted its all the code it has. And yes, I’m running inside the Template options of the binary sensor dialog, so I guess this is the template editor you are referring to.

{{ this.state }} not working there is a known issue or limitation? It kind of breaks the nice live preview you get when making changes to the code. At any rate, I’m more than good if it does work at runtime!

Thank you again for all your help!
-VGA.

1 Like

When we say “Template Editor” that generally means the one in Developer Tools. As I understand it, the template renderer in the Template Helper is functionally the same.

The this variable is the state object of the entity. It only holds value locally within the sensor, automation, or script entity that creates it. It has no defined value in the Template Editor or in the Helper editor.

If you are using this, it is often best practice to provide a default value.

Right, not exactly what I was referring to, but I would think it’s the same mechanism that runs the template editor under the dev tools. I don’t use the UI to build things, unfortunately. You could log a feature request for that. I don’t know if it would be technically possible.

I assume it should be easier to implement in the template editor of the Binary sensor dialog as that one should already have the context for this while the template editor in DevTools shouldn’t.
Anyway, not really a big deal!