esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
291 stars 36 forks source link

UDP binary sensor not working for buttons #6271

Closed bruxy70 closed 2 weeks ago

bruxy70 commented 1 month ago

The problem

I have a binary sensor for a gpio input connected to Hoermann RF receiver. When the button is pressed, the receiver will close the relay (and therefore the gpio binary_sensor) for 0.5s (and then open it again). When I configure this binary sensor as UDP transmitter, it will not send the signal. As a workaround, I configure an on_press automation to toggle a relay, and have a template binary sensor to return the relay state. And then use this as UDP transmitter and it works fine (as the binary sensor state will toggle on each 0.5s impulse and stay there).

Which version of ESPHome has the issue?

2024.9.0

What type of installation are you using?

Home Assistant Add-on

Which version of Home Assistant has the issue?

2024.9.2

What platform are you using?

ESP8266

Board

sonoff basic + Hoermann RF receiver

Component causing the issue

UDP

Example YAML snippet

udp:
  encryption: !secret udp_encryption_key
  binary_sensors:
    - switch_garaz

binary_sensor:
  - platform: gpio
    id: btn_garaz
    pin:
      number: ${button_gpio}
      mode: INPUT_PULLUP
      inverted: True

Anything in the logs that might be useful for us?

No response

Additional information

No response

clydebarrow commented 2 weeks ago

It does work (some of the time) but since UDP is an unreliable protocol it can't effectively be used to transmit events, only state. Since in your case the state only persists for half a second, there is a good chance that the receiving node will not see the change to true and it will not be re-broadcast since the sensor goes back to false almost immediately.

Your work-around using a switch toggle is a good solution, as this captures a longer-lasting state, and you can use a state change in the receiver to recognise that a pulse occurred. You could also use a Pulse Counter Sensor which will increment its value when a pulse is seen. This also effectively converts events to a state which can be relatively reliably transmitted by UDP.