How to make a component value configurable from external

I have a binary sensor like this

binary_sensor:
  - platform: gpio
    pin: GPIO33
    filters:
      - delayed_on: 100ms
      - delayed_off: 2000ms

I would like to make the delayed_off value configurable from Home assistant. Is template the right thing for that? How would I need to add it correct. Or better the number component?

1 Like

Maybe it is possible, but in you case I would consider removing the delays here and create a template sensor in HA with the delays. Unless there is a reason the logic needs to be in the module.

without the delay the sensor switches on/off extremely often.

Those filters are not templatable - so no you can’t change them they are effectively hard coded.

You can however write your own filter - Binary Sensor Component — ESPHome

Can you give me an example how that would look? I have no idea how to do that.

Sure - but there are probably other (better) ways of debouncing your sensor - what is it detecting and what are you doing with it?

You need to write C++ code for your own filter.

First declare a couple of homeassistant text sensors for your delays, then in the binary sensor:

binary_sensor:
  - platform: gpio
    pin: GPIO33
    filters:
      - lambda: |-
          off_delay = atoi(id(off_delay_sensor).state.cstr());
          on_delay = atoi(id(on_delay_sensor).state.cstr());
          if x == true { # x is the value of your binary sensor
             # some processing that checks last time sensor was turned off and compares it
             # that plus the on_delay.  If not long enough then
             return false; } # leave off
          else {
             # some processing that checks last time sensor was turned on and compares it
             # that plus the off_delay.  If not long enough then
             return true; } # leave on
          }

This is essentially what delayed_on etc do, but you can provide the timings using a HA text sensor.

If you aren’t familiar with C++ programming you may have to go with a different method.

I am debouncing a PIR Sensor that is switch fast between on/off. But with this filter it works as expected. But I want to make the off value configurable, maybe also the on value.

The off_delay_Sensor is a number component or where does the value come from? You writing home assistant text sensor, so the text sensor is send automatically to esphome? or do I need also a text_sensor in esphome?

I tried to add it like you wrote but I get errors like

off_delay' was not declared in this scope

I also tried to change it to delayed_on and delayed_off, what the naming is in the documentation, with no change

That’s just a framework - there is a lot of coding to do there…

As I said - you have to define text sensors - and probably global variables and a whole lot of stuff and if you aren’t a C++ programmer I would just stick to the hard coded delays.

For those trying to make the delayed_off value configurable, it now works as it now supports templates.

Here is my code that has a config box in HA to set the timeout value:

number:
  - platform: template
    name: "Zone1 Timeout"
    id: zone1_x_timeout
    min_value: 0
    max_value: 600
    mode: box
    device_class: duration
    entity_category: config
    unit_of_measurement: s
    icon: mdi:timer-off
    step: 1
    optimistic: True
    initial_value: 0
    restore_value: True

binary_sensor:
  - platform: template
    name: "Zone1 Presence"
    id: zone1_target_exsits
    device_class: occupancy
    filters:
      - delayed_off: !lambda |-
          return id(zone1_x_timeout).state * 1000.0;