Custom binary sensors for 3 value ranges

Hello guys,
sorry for the newbie question: I tried to search documentation everywhere but couldn’t find a solution that fits.

What I want to do
I need a binary sensor that works like this:

  • state is OFF when value is less than 600
  • state doesn’t change when the value is between 600 and 1150 (if it was ON, it has to remain ON, if it was OFF it has to remain OFF)
  • state is ON when the value is higher than 1150

Why
I want to monitor the CO2 in my house and I want HA to:

  1. send a voice message asking to open the windows if the CO2 is higher than 1150
  2. send a voice message saying we can close the windows if the CO2 is lower than 600, BUT ONLY if a message asking to open the windows was already sent (i.e. CO2 has been higher than 1150)

Example:

T1 - CO2: 400 => state: off
T2 - CO2: 800 => state: off     // CO2 between 600 and 1150: no changes to the state
T3 - CO2: 1200 => state: on     // CO2 too high: change the state to `ON`
T4 - CO2: 800 => state: on      // CO2 between 600 and 1150: no changes to the state
T5 - CO2: 550 => state: off     // CO2 below 600, change the state to off

Is there any way to create such sensor?
Is there any better way to achieve what I’m trying to do?

I really appreciate any help you can provide.
Massimiliano

Use a threshold sensor with hysteresis:

binary_sensor:
  - platform: threshold # On at 1150, off at 600
    name: CO2 Alert
    entity_id: sensor.co2
    upper: 875
    hysteresis: 275

Turns on at upper + hysteresis and off at upper - hysteresis.

1 Like

I looked a lot on threshold sensors and I didn’t think about a so simple solution…

Thank you very much!!!

1 Like