Why isn't binary GPIO sensor obeying on_click different lengths?

This is the format that’s in the manual about the binary sensor’s on_click:

binary_sensor:
  - platform: gpio
    # ...
    on_click:
    - min_length: 50ms
      max_length: 350ms
      then:
        - switch.turn_off: relay_1
    - min_length: 500ms
      max_length: 1000ms
      then:
        - switch.turn_on: relay_1

I have set it up like this:

binary_sensor:
  - platform: gpio
    pin:
      number: 14
      mode: INPUT_PULLUP
      inverted: True
    name: "1"
    filters:
      - delayed_on: 20ms
    on_click:
    - min_length: 50ms
      max_length: 750ms
      then:
        - mqtt.publish:
            topic: madmax/esp/response/madmaxfrontknapper/1kort
            payload: "kort"
    - min_length: 2000ms
      max_length: 4000ms
      then:
        - mqtt.publish:
            topic: madmax/esp/response/madmaxfrontknapper/1lang
            payload: "lang"

Without input pullup there was a steady stream of reaction coming, so I had to use that. But I haven’t seen antyhing about that being a problem for on_click. So can somebody please tell me why I get “1kort” with payload “kort” (short in Norwegian) both when I click between 50 and 750 ms and when I click 2000 to 4000 ms?

Your switch contacts could be bouncing. You have fixed this with the delayed on, but they could also bounce when the switch is released.

Try a 10ms delayed on and delayed off. That should be sufficient for a good quality push button.

EDIT: scratch that. The min on time of 50ms should prevent switch off bouncing being an issue. Not sure what is going on.

Thanks! That removed the short press being interpreted as a long press, but the long press still doesn’t come through. I tried doubling both to 20ms too, but still no go. :frowning: Is there anything else I can try in addtion to that? I’m getting both the on and the off on MQTT when I let go of the switch after around trhee seconds, so it does interpret the press, and I don’t get any extra presses or releases in the built-in binary_sensor/1/state MQTT message.

I’m using these piezo switches, I think they should be pretty good: https://a.aliexpress.com/_mKmUHmp

Yeah I just edited my post as you were replying. Not sure what is going on.

Sorry, edited this post instead of putting in a new one… This was just a question if it was possible in any way…

It should definitely be possible. Try searching the ESPHome forum for short and long click examples.

It’s the switches… Short circuiting the cables gives me the expected results. Annoying as heck, because I ordered those switches since the application is an open fishing boat, and I have to use something that’s completely waterproof. Are there any tricks I can use as a workaround? I tried with a 50 ms delayed on and off, but it still more often than not say “short” on long presses, even if the on and off through the built-in MQTT message doesn’t come until the long press is released.

Unfortunately I can’t see the Ali express link (I’m on a ship sharing an internet connection with 140 other people, also it might be blocked). Can you post a cropped and minimised screenshot of the switch type?

H4a66a0e52706427c800260c5320ee065S.jpg_220x220q90.jpg_

It’s this one. Another thing: Is it possible to make it send the “ON” and “OFF” from the built-in MQTT status sending while the button is pressed, not both ON and OFF after I release? That way I could do the calculation in Node-RED or Hass.

Can you copy and paste the specs for the switch?

I have a sneaking suspicion that they do not operate like normal push buttons that simply short contacts together while pushed. I think the mechanical force of pressing the switch will generate a short voltage pulse and when the switch is released another short voltage pulse of the opposite polarity will be sent (piezoelectric effect).

Try searching alix for IP68 push button instead.

1 Like

Damn, they take a month or two to arrive… Here are the specs, if they tell you anything:

Product Specification
Mounting Hole: 16MM 19MM 22MM 25MM
Material: Aluminum anodized or stainless steel
Working Temperature: -40-70℃
Storage Temperature: -40-70℃
Working humidity: 10%-99%
Input Voltage: 3-24V/DC
Input Current: ≤2A
Contact Resistance: <5mΩ
Pulse Time: <50MS
Working Pressure: 3-8N
Protection Garde: IP68 IEC529
The Type Of Connection: 0.22 quare wire

This piezoe switches is heavy duty, non-spark, non-magnetic, corrosion resistant, lighting resistant, damage resistant and designed for indoor and outdoor use in inclement weather.
USES solid-state piezoelectric switch technology and has a dry contact NO pulse sensor that reacts when touched by the finger. No physically moving parts.
It has a very long life, can be used more than 200,000 times
The piezo button switches has been tested and the waterproof rating is IP68.
Can be used in severe weather conditions from -40°C to 70°C (-40°F to 158°F).

Yep, it outputs a single pulse no matter how long you hold it:

Seems there is a FET in there as well to convert the voltage to a contact closure though rather than outputting the piezo voltage.

Pretend you’re talking to an electronic idiot…OK, you are talking to an electronic idiot! :wink: Does that mean that I can’t get it to do long presses reliably no matter what I do? As I say, they work sometimes, maybe 50 % of the time.

Correct. It is the wrong sort of switch. The only thing that will change the output pulse length is how hard you press it. The harder and faster you press the piezo crystal behind the switch the more voltage it generates, keeping the FET switch on longer (always less than 50ms for this switch though). Have a read of this:

Thanks, but that seems illogical to me. Because when I set it to have a short press from 50 to 500 and long press from 501-1500 ms, it does obey and shows “long” around 50 % of the time. If it was always doing less then 50ms, that would never work. Or is there something else going on that I don’t understand either? That would be no surprise, of course… :laughing:

Hard to say without an oscilloscope to observe the switch output. The fact that it works reliably when you short the wires together is the biggest giveaway.

OK, thanks. So there is no signal what so ever coming when I press, only when I release? Which is why I can’t get it to send the “ON” when I press and then the “OFF” when I release. Maybe I can work on multiple presses of the short one, so one press is this, two presses is that and three presses is a third thing. I’ll try something like this when I get the time:

on_multi_click:
- timing:
    - ON for at most 1s
    - OFF for at least 3s
  then:
    - logger.log: "1 click"
- timing:
    - ON for at most 1s
    - OFF for at most 1s
    - ON for at most 1s
    - OFF for at least 3s
  then:
    - logger.log: "2 clicks"
- timing:
    - ON for at most 1s
    - OFF for at most 1s
    - ON for at most 1s
    - OFF for at least 0.2s
    - ON for at most 1s
    - OFF for at least 3s
  then:
    - logger.log: "3 clicks"

Yes, multi-click would work.

Great, thanks! I can probably live with that just as well as I could with different lengths. It may even be better, especially since it’s a bit difficult to hold the finger steady in a 24 aluminum crawfish boat in a bit of waves…

Yep, that’s reliable. Thanks again, @tom_l! As long as I hit the button with normal accuracy this code gives me three alternatives from one button:

binary_sensor:
  - platform: gpio
    pin:
      number: 14 #usable pins 14, 16, 17, 18, 19, 21, 22, 23
      mode: INPUT_PULLUP
      inverted: True
    name: "1"
    filters:
      - delayed_on: 10ms
      - delayed_off: 10ms
    on_multi_click:
    - timing:
        - ON for at most 1s
        - OFF for at least 1s
      then:
        - mqtt.publish:
            topic: madmax/madmaxfrontknapper/knapp/1
            payload: "1"
    - timing:
        - ON for at most 1s
        - OFF for at most 1s
        - ON for at most 1s
        - OFF for at least 1s
      then:
        - mqtt.publish:
            topic: madmax/madmaxfrontknapper/knapp/1
            payload: "2"
    - timing:
        - ON for at most 1s
        - OFF for at most 1s
        - ON for at most 1s
        - OFF for at most 1s
        - ON for at most 1s
        - OFF for at least 1s
      then:
        - mqtt.publish:
            topic: madmax/madmaxfrontknapper/knapp/1
            payload: "3"
      invalid_cooldown: 1001ms
1 Like