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”.
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?).
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?
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.
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
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.
@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.
No, I wouldn’t. Thats why I wrote: “I googled and tried”
great, thank you very much. That’s perfect. I don’t care about if it’s unknown or unavailable, both are better than division be zero errors in the log or 0.
Thanks very much!