Help needed binary sensor template

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 %}

Two problems:

  1. 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)

  2. 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.

Isn’t the Threshold native integration suitable for this?

Yes it is but it won’t allow an adjustable hysteresis using an input_number.

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 %}

I’m still doing something wrong…

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 %}

I copied it into the template and restarted haos.
soll = 20
ist = 19
hyst = 1
but the binary sensor is ‘off’.

Try again by copying everything Taras wrote into the Developer Tools → Template editor.

That’s not what I asked you to do. Review what I wrote above. We need to see what the Template Editor displays in its results window.

1 Like

Wow, I didn’t know that tool yet, it works nice and reacts very quick on changing entities!

The outcome is this:

soll: 23.5

ist: 19.8

hyst: 0

  Less than: on

So it should work?
But the sensor is still ‘Normal’ or off (depending on where I look?)

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 %}

can be reduced to this:

{{ iif(ist < soll, 'on', 'off') }}
1 Like

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 %}

Thank you very much for helping and teaching me!

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).

Okay, got it.
Added {% else %} {{ this.state }} again.

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.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

2 Likes