How to debounce my door

I have a door in my room on which I have connected a handmade sensor (which checks if the door is open or closed). It’s a binary sensor connected to a Raspberry pi’s GPIO. Now, when I’m opening and closing the door, there’s some bouncing happening (the door has some suspension and the closing and opening motion causes the sensor to trigger about 2-3 extra times when closing/opening the door). Now… Here’s my problem: I want the Home Assistant instance to IMMEDIATELY recognize when the door is opened (because I want to set the music quieter when somebody enters my room). I am aware there’s the delay_on and delay_off config options in the Template integration. However, this causes HASS to respond to the sensor after the time has passed. What I want is HASS to respond immediately and then ignore sensor triggers for a certain time period.

Example:

I open my door. HASS immediately sets the music quieter. While opening the door, the sensor gets some extra false triggers however HASS ignores any false triggers for 1 second after the first trigger has been recognized.

Check out:

Example throttled automation

You could use that technique to change an Input Boolean to track the state of the door (instead of using a template binary sensor.) You would also need to make sure the Input Boolean is set appropriately when Home Assistant starts. (Hint: see Home Assistant trigger.)

For the above approach to work, both the on and off actions should be in the same automation. If you do not want that, another approach would be to start a short timer, and check if the timer is running or not to detect subsequent triggers. This pretty much amounts to the same thing, but you can use the same timer across different automations, and/or do other stuff if the timer is running.

Also, subsequent bounces could prolong the timer, which isn’t possible with the above. You might want to consider to use the timer end to see if the state of the sensor corresponds with expectation, to make sure you didn’t ignore a relevant change.

That sounds like a challenge! LOL

How about something like this:

- mode: single
  max_exceeded: silent
  trigger:
    - platform: state
      entity_id: binary_sensor.gpio
  action:
    # Set the helper to the new state of the gpio pin
    - service: "input_boolean.turn_{{ trigger.to_state }}"
      entity_id: input_boolean.door_open
    # Wait for gpio to stop changing (up to 1 sec after each change.)
    - repeat:
        sequence:
          - wait_for_trigger:
              - platform: state
                entity_id: binary_sensor.gpio
            timeout: 1
        until: "{{ not wait.completed }}"
    # Make sure helper reflects final state of gpio pin.
    - service: "input_boolean.turn_{{ states('binary_sensor.gpio') }}"
      entity_id: input_boolean.door_open

Looks good to me.

Have you considered spring loaded hinges as used in garage entry doors to stop the bounce. This would simplify all automations and keep the door closed except for when someone walks through it.

Hahahaha… music quieter… mmm-hmm. Screams porno panic…

hmmm, not sure if @Edwin_D ever tried this, because I get

automation 89: Error executing script. Error for call_service at pos 1: Template rendered invalid service: input_boolean.turn_<state binary_sensor.kitchen_door=on; friendly_name=Kitchen Door, device_class=door @ 2023-03-27T15:34:41.926482+01:00>

when I try to test this

  - mode: single
    max_exceeded: silent
    trigger:
      - platform: state
        entity_id: binary_sensor.kitchen_door
    action:
      - service: "input_boolean.turn_{{ trigger.to_state }}"
        entity_id: input_boolean.kitchendoor
      - repeat:
          sequence:
            - wait_for_trigger:
                - platform: state
                  entity_id: binary_sensor.kitchen_door
              timeout: 1
          until: "{{ not wait.completed }}"
      - service: "input_boolean.turn_{{ states('binary_sensor.kitchen_door') }}"
        entity_id: input_boolean.kitchendoor

any ideas most welcome! :slight_smile:

Thanks!

I didn’t test it, as I was referring to the approach and not the fine details. But for the service call to succeed, “.state” was missing. Use this instead:

   action:
      - service: input_boolean.turn_{{ trigger.to_state.state }}

(and no, I didn’t test the whole thing this time either)

1 Like

Cool! Thanks, that did the trick! :slight_smile: