I’m trying to script my first template sensor. I need a boolean to be ‘on’ if the temperature in a room is lower than the setpoint, and it should be ‘off’ when the temperature is higher than the setpont plus some hysteresis. I made a helper called ‘hysteresis’ which holds the value ‘1’ at the moment.
This is my sensor template, the yaml is okay, but it won’t update. Also the value of the hysteresis is not updateing.
I did a restart.
# Bepaal of er warmtevraag is in de woonkamer
template:
- binary_sensor:
- name: "warmtevraagWoonkamer"
state: >
{% set soll = states('sensor.room_setpoint_ovrd_thermostat_kamerthermostaat')|float %}
{% set ist = states('sensor.room_temp_thermostat_kamerthermostaat')|float %}
{% set hyst = states.hysteresis_input %}
{% if soll > ist + hyst %}
off
{% elif soll < ist %}
on
{%endif %}
This is invalid: states.hysteresis_input
If you have an Input Number helper named “hysteresis” then it should look like this: states('input_number.hysteresis') | float(0)
Your if-elif is missing an else. If the value of sol is not greater than ist+hyst and not less than ist, the template reports nothing.
Thanx for helping, much appreciated!
Now the hysteresis is also updated and the state of the sensor is set to off which is shown as ‘Normal’ with the remark: no heat detected. I wasn’t expecting anything other than ‘off’, but at least it’s telling me the correct thing.
So I turned up the thermostat, The ‘sol’ is now much higher than the ‘ist’, but the binary sensor is not updated to ‘on’. I saw I clumsily mixed up ‘ist’ and ‘soll’, but that is correct now. Still the sensor doesn’t 7show as ‘on’ .
# Bepaal of er warmtevraag is in de woonkamer
template:
- binary_sensor:
- name: "warmtevraagwoonkamer"
state: >
{% set soll = states('sensor.room_setpoint_ovrd_thermostat_kamerthermostaat')|float %}
{% set ist = states('sensor.room_temp_thermostat_kamerthermostaat')|float %}
{% set hyst = states('input_number.hysteresis') | float(0) %}
{% if ist > soll + hyst %}
off
{% elif ist < soll %}
on
{% else %}
off
{%endif %}
Copy-paste the following into the Template Editor and then let us know what it reports
{% set soll = states('sensor.room_setpoint_ovrd_thermostat_kamerthermostaat')|float(0) %}
soll: {{ soll }}
{% set ist = states('sensor.room_temp_thermostat_kamerthermostaat')|float(0) %}
ist: {{ ist }}
{% set hyst = states('input_number.hysteresis') | float(0) %}
hyst: {{ hyst }}
{% if ist > soll + hyst %}
Greater than: off
{% elif ist < soll %}
Less than: on
{% else %}
Otherwise: off
{% endif %}
The template I posted above, for use in the Template Editor, has no influence on your Template Binary Sensor. Its purpose is to examine how the calculations behave.
Basically it’s providing the correct result (on) but I’m concerned that the value of hyst is zero.
Go to Developer Tools > States and confirm you have the following entity: input_number.hysteresis
If it is present, what is its value? Is it zero?
If it’s missing, then you either overlooked to create it or your Input Number entity has a different entity_id.
EDIT
BTW, all of this:
{% if ist > soll + hyst %}
off
{% elif ist < soll %}
on
{% else %}
off
{%endif %}
I think, if I would use that short line, I would miss the poiny of the hysteresis?
You were correct about the hysteresis value: the correct name was ‘input_number.hysteresis_input’
I have now left out the header stuff, deleted the template and started over.
First it still did not work. But after I changed ‘<’ into ‘<=’ it suddenly started working, even when I took away the '='again. I don’t understand what happened there, but bottom line: this is my working tamplate:
{% set soll = states('sensor.room_setpoint_ovrd_thermostat_kamerthermostaat')|float(0) %}
{% set ist = states('sensor.room_temp_thermostat_kamerthermostaat')|float(0) %}
{% set hyst = states('input_number.hysteresis_input') | float(0) %}
{% if ist > soll + hyst %}
off
{% elif ist < soll %}
on
{% else %}
off
{% endif %}
The shorter version does exactly the same thing as your longer version.
Look carefully at how you have designed its logic. Under what conditions does it report on? Only when ist is less than soll. For everything else it reports off. It behaves like this:
{% if ist < soll %}
on
{% else %}
off
{% endif %}
In other words, you have designed it in a way where the only condition that turns on the binary_sensor is if ist is less than soll. Any other value of ist results in off.
If that’s not how you want it to work then you will need to redesign the logic.
Perhaps what you want is something like this:
{% if ist > soll + hyst %}
off
{% elif ist < soll %}
on
{% else %}
{{ this.state }}
{% endif %}
If ist is not greater than soll + hyst and not less than soll then it reports whatever is its current state (on or off).
Mmm, I only inserted that last ‘else’ because I though it’s needed for the syntax of Jinja.
But now it’s working, I can test if I still need it. And as it turns out: it’s working nicely without.
Thank you for sticking with me and coming back on it!
Also changed the wording a bit…
{% set setPointWoon = states('sensor.room_setpoint_ovrd_thermostat_kamerthermostaat')|float(0) %}
{% set currentWoon = states('sensor.room_temp_thermostat_kamerthermostaat')|float(0) %}
{% set hyst = states('input_number.hysteresis_input') | float(0) %}
{% if currentWoon > setPointWoon + hyst %}
off
{% elif currentWoon < setPointWoon %}
on
{% endif %}
It’s needed to ensure the template always reports a value. Your latest version makes the same mistake as your first version.
Both your initial version and latest one overlook to consider the possibility that currentWoon may be greater than setPointWoon but less than setPointWoon + hyst. In other words, when the value is within the “hysteresis zone”.
If currentWoon is not greater than setPointWoon + hyst and it’s not less than setPointWoon, your template will report nothing which will cause the binary_sensor’s state to be unknown.
In the template I posted above, when the value is within the hysteresis zone, it simply reports the existing state (on or off). If that’s not what you want then you need to decide what you want it to report when the value is within the hysteresis zone because reporting nothing at all isn’t a viable option (unless you want the binary_sensor’s state to be unknown).
That makes the most sense to me because it helps to implement the behavior of hysteresis.
Please consider marking my post above with the Solution tag. It’s the post where I had first suggested to employ this.state.
By marking the post with the Solution tag, it will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.