How to set template sensor to unavailable?

Hi,

I googled and tried, but I’m stuck with this problem. I created a template sensor to calculate the COP of a heatpump.
If the power consumption is lower than 10 the heat pump is idle / standby => no COP => should be “unavailable” or maybe “unknown”.

{% set p1 = float(states(‘sensor.power_th’), 0) %}
{% set p2 = float(states(‘sensor.power_el’), 0) %}
{% if p2 < 10 %}
{{ 0 }}
{% else %}
{{ p1/p2 }}
{% endif %}

Because unavailable is not allowed in the template but one should add an availablility template, which could be “COP == 0 => unavailable”
But it’s not possible to add an availability template through webinterface (or is it?). The template sensor itself was created via webinterface because with config by configuration.yaml, no device can be assigned (or can it?).

So I tried to customize the sensor:

homeassistant:
customize:
sensor.cop:
availability: “{{ int(states(‘sensor.cop’), 0) > 0 }}”

But it’s not working - the availability template seems to be ignored.

So, how could I assign a device to a sensor defined in configuration.yaml, OR how could I add an availibility template to the sensor created in the web interface?

Or must the solution be:

  • create COP sensor in configuration.yaml with templates for value and availibility
  • create a helper sensor in Web UI to just “copy” the value to the helper sensor, which can be assigend to a device?

Hi NoCoolUsernameLeft,

Please don’t take this the wrong way, but there are lots of examples here on the forums of availability: for templates and it is just 1 or 2 lines of code. Go to the edit in YAML in the UI editor and try it.
As long as you have it saved before you switch screens nothing will be lost and you have much to gain.

Also feel free to look for a feature request or add one if there isn’t one to request this.

You too…
The answer is pretty unnecessary. Because I have really searched a lot and for a long time. Do you think I would register just to ask if I had found a solution? I haven’t found anything in the documentation or on Google that would solve my problem… maybe in the wrong place, with the wrong terms. Possible. But your answer, sorry, not useful.

Please test it in Developer tools => Template

{% set p1 = states('sensor.power_th')|float(0) %}
{% set p2 = states('sensor.power_el')|float(0) %}
{% if p2 < 10 %}
0
{% else %}
{{ p1/p2 }}
{% endif %}

Or

Thanks, but my issue is not that my “template code” does not work. It works very well.

But if the power_el is zero, this results in an division by zero (message ZeroDivisionError: float division by zero in the dev.tools), and an error in the log which I would like not to have, but in the state “unavailable” (that’s good, because that’s what the COP is: unavailable.
.
So my goal is to have somthing like this:

{% set p1 = states(‘sensor.power_th’)|float(0) %}
{% set p2 = states(‘sensor.power_el’)|float(0) %}
{% if p2 == 0 %}
‘unavailable’
{% else %}
{{ p1/p2 }}
{% endif %}

But as the documentation states:
“The state template must not render to a string, including unknown or unavailable . An availability template may be defined to suppress rendering of the state template.”

But that’s the thing, I can’t add an availability template in the web interface.
I guess I could add the availability template in the configuration.yaml if I would define the sensor there, but then I can not add a device info, which was possible in the UI.
That’s the issue :wink:

The nearest solution I see is:

Sensor in configuration.yaml with availability template

    - name: "COP Helper"
      unique_id: "heatpump.cop.helper"
      state_class: "measurement"
      state: >
           {% set p1 = float(states('sensor.power_th'), 0) %}
           {% set p2 = float(states('sensor.power_el), 0) %}
           {% if p2 >= 10 %}
             {{ p1/p2 }}
           {% else %}
             {{ 0 }}
           {% endif %}
      availability: "{{ float(states('sensor.power_el'), 0) >= 10 }}"

and helper sensor in the web interface with template
{{ states('sensor.cop_helper') }}, added to the device

But I don’t believe that’s the only and nicest way…!?

I’m glad this was not the case, but would you be surprised if I told you that that kind of question fixes more than a quarter of the peoples problems. Many want it done for them, not to actually be helped. The question is often worth asking.

Thanks for not being one of those. :orange_heart:

@NoCoolUsernameLeft
In case you really want to use the GUI (to be able to add it to a device), you can let it render none instead of using the availability template.
It will return unknown as it’s state then, so not unavailable but it won’t throw errors.

{% set p1 = float(states('sensor.power_th'), 0) %}
{% set p2 = float(states('sensor.power_el), 0) %}
{% if p2 >= 10 %}
  {{ p1/p2 }}
{% else %}
  {{ none }}
{% endif %}