Binary sensor template with different low/high behavior

I have this binary sensor template for my lights.
It turns on when the sun elevation is low and my 2 light sensors are below 40.

I turn off the lights if sensor turns off for some time, but the issue with this is when the lux value is just around 40. The lights have a tendency to turn on/off and that can be annoying.
I would like the sensor template to turn on when the lux value is 40, but turn off when the value is higher (maybe 60?) but unsure how to integrate it into the template.

Does anyone know how to do this?

- platform: template
  sensors:
    tillad_automatisk_lys:
      friendly_name: 'Tillad automatisk lys'
      icon_template: 'mdi:brightness-auto'
      value_template: >-
        {{ (states.sun.sun.attributes.elevation|float < 2)
           or (states.sensor.stue_lys.state|float(40) < 40)
           or (states.sensor.spisebord_lys.state|float(40) < 40) }}

need to incorporate your lights into this then. Add them all to the template and if they all are on, use a different threshold.

or add 3 states. ‘on’, ‘do-nothing’, ‘off’.

Or use the threshold binary sensor. It has hysteresis.

2 Likes

@tom_l
The Threshold Binary Sensor seems to works nicely.
I’ll integrate 2 of them into my binary sensor template.