Binary sensor template advise

Hello all,

I have created a binary sensor template, I will use this one for a wasmachine automation.
I found this template on the internet.

It works but I wonder if I can set a “on” value and a “off” value.

For now it’s above 25w and below 25w!, Let’s say I want a higher watt number for on, and a lower number for off, that in combination with the delay_on and delay_off… I can make it more accurate and switch faster.

Is this possible?

‘’’
binary_sensor:
- platform: template
sensors:
dryer_status:
friendly_name: “Dryer”
entity_id: sensor.power_dryer
delay_on:
minutes: 2
delay_off:
minutes: 6
value_template: >-
{{ states(‘sensor.power_dryer’)|float > 25 }}
icon_template: mdi:tumble-dryer
‘’’

Formatting was close but you used the wrong tick marks. You need these ones: ```

And yes you can include some hysteresis in your switching levels by using the threshold binary sensor instead of the template binary sensor:

binary_sensor:
  - platform: threshold
    name: "Dryer"
    entity_id: sensor.power_dryer
    upper: 25
    hysteresis: 5

This will switch on above 30W and switch off below 20W.

EDIT: Unfortunately it does not support a delay on/off which I just noticed you need too.

So you could adjust your binary sensor template to this instead:

value_template: >-
  {% if states('sensor.power_dryer')|float > 30 %}
    {{ true }}
  {% elif states('sensor.power_dryer')|float < 20 %}
    {{ false }}
  {% else %}
    {{ is_state('binary_sensor.dryer', 'on') }}
  {% endif %}

The else statement keeps the binary sensor in it’s last state if not above or below the thresholds (converts on/off binary sensor state to true/false respectively as required by the template).

1 Like

Thx for you’re feedback, I will try it when I’m home.

Does this mean that I have to adjust my automation based on this template, trigger from ‘on’ to ‘true’?

No.

Binary sensors always have the state 'on' or 'off' you can continue to trigger on that no problem.

The template in a template binary sensor just expects a true or false result and converts that to the state 'on' or 'off'.

You did this with your original template, {{ states('sensor.power_dryer')|float > 25 }} the statement is either true (when above 25) or false (when below 25). The binary sensor converts this template result to 'on' or 'off'.

The other option is to use the threshold binary sensor (easier than templates) and just include a for: statement in your trigger, e.g.

binary_sensor:
  - platform: threshold
    name: "Dryer"
    entity_id: sensor.power_dryer
    upper: 25
    hysteresis: 5

automations:

trigger:
  platform: state
  entity_id: binary_sensor.dryer
  to: 'on'
  for:
    minutes: 2
...

and

trigger:
  platform: state
  entity_id: binary_sensor.dryer
  to: 'off'
  for:
    minutes: 6
...