Click and long press of tact switch, to trigger HA automations

This feels like a really basic question, but I’ve struggled to find how to do it.
I have an esp32 (s3 zero) with a tact switch connected between one of its pins and ground, with the internal pull-up enabled.

Could anyone please explain what I need to do in the esphome yaml to have both a quick/momentary press and holding it down (for say 500ms) usable in HA, for triggering two separate HA automations?

TIA!

So you have a functional binary sensor in HA? While you COULD do it all in ESPHome, if it’s controlling other things in HA that aren’t on the ESP device itself, I’d just setup a HA automation to do whatever you want. Much easier to modify in the future that way.

Yes, I currently have a binary sensor.

Is it possible to have something to use in HA that has a status of pressed/held/off, or two separate things one for pressed and one for long press… with the press/hold timing happening on the esp device?

Lots of ways to do it. But unless you’re doing some pretty complex timing, I really think a simple HA automation with timing constraints would be easier and quicker.

You are looking for to recognize short click from long?

on_click:
- min_length: 50ms
  max_length: 200ms
  then:
- min_length: 50ms
  max_length: 1000ms
  then:

For both of those, how would you get them to show separately in HA (rather than do things on the esp) please?

If using just a binary sensor, how would you differentiate between a quick press and holding it in HA?

Example above is esphome binary sensor. Line then: is where you do automations. I don’t know what you want to see as output. To see 100ms state change is probably quite worthless. Maybe you want to change state of some sensor or switch?

I’d like to use the two different actions (1 press and 2 hold for a short time) to trigger two automations in HA (or two parts of one automation using IDs and Triggered By)

If using a binary sensor and triggering on it turning on and it turning on for a length of time, holding it triggers both. (I’m not aware of an ‘on for less than’ trigger?)
Having a third thing triggered by a double press would be good too

Not the way above.

Just to have some filtering for interferences.

Still very much doable with esphome binary sensor. At that point you could use triple click and eliminate the long press.

How could I filter for interferences?

What would have to go after the then: for it to be usable in home assistant (not on the esp)?

I was trying to keep the question simple, but maybe some more context would be helpful:
I have an aquarium with various devices connected to HA
I have a 4 gang esphome-controlled mains socket, through which I power the aquariums filter, in-line heater and CO2.
I have an esphome-based IR blaster on the side of the aquarium cabinet - this is the device with the button. This esphome device doesnt have any direct control over the devices I’d like it to affect.
I have an esphome based automatic feeder.

I can currently press the button on the irblaster and it triggers a ‘feeding’ automation, which turns off the filter, heater and CO2, fires the automatic feeder, waits 15 minutes and then turns everything back on.

I’d like a long-press of the button to trigger HA to do other things. Same with a double-press.

50ms is already more than enough.

Only you know. Whatever you need to automate. Since I don’t know your automations on HA side, simple approach would be to change state of some sensor or switch.

How I could achieve this is my question.

I havent found a way to do either of these:

  1. Have the timing logic on the esp, and present the results to HA in a way I can use to trigger different automations. (what goes after the then: on the esp?)
  2. Use just a binary sensor and have HA do the timing to trigger different automations. (what goes in the HA automation trigger?)

Do you have any usable suggestions please?

“Simple approach would be to change state of some sensor or switch.”
How please?

  1. There are so many ways to give a trigger to HA. You could use a template sensor or template number or few template switches to present the result of multiclick. Another approach is using homeassistant.action.
    Native API Component — ESPHome

I cant see how to use pressing the tact switch to trigger a template sensor, template number or template switch.
They seem to be be more of a regular ‘updated every x seconds’ kind of thing?
If its possible to set up something like ‘when the button’s pressed change this template switch’, could you point to an example that includes that please?

homeassistant.action seems like it might be good, but how do you then trigger an automation using it please?

I’m not able to give you the best approach. Maybe homeassistant.action is what you are after.
Anyway, if you want to play with esphome componets, you could use templates to update the state of your binary sensor clicks.

on_click:
- min_length: 50ms
  max_length: 300ms
  then:
    - number.set:
        id: button_num
        value: 1
- min_length: 300ms
  max_length: 2000ms
  then:
    - number.set:
        id: button_num
        value: 2

would update a number component according to your click pattern.

number:
  - platform: template
    id: button_num
    name: "Button Press Number"
    optimistic: true

Third option is to play with home assistant to trigger binary sensor like @brooksben11 suggested. I personally have no idea of that approach.

You will be able to see quite clearly if you read the documentation and look through the many examples they show you.

In the examples where they show that " on_…"

on_...:
  - switch.template.publish: 
      id: template_switch
      state: ON

That “…” will depend on what type of integration your using, whether its a binary_sensor, switch, cover, etc. For example your binary_sensor can use

on_press:

on_click:

on_state:

And so on.....

So, going back to the above example, if you wanted to use a binary_sensor to trigger an action from a template_switch then you look at the “on_…” options for binary_sensor, use the one appropriate for what your doing, lets say you need a on_press:

binary_sensor:
  - platform: gpio
    id: my_button
    name: "My Momentary Button"
    pin: 
      number: D5
      mode:
        input: True
        pullup: True
      inverted: True
     on_press:
       then:
          - switch.template.publish:
               id: template_switch
               state: ON

What about using a Cover…? Cover uses on_open: and on_close

cover:
  - platform: template  
    # ...
    on_open:
      then:
          - switch.template.publish:
               id: template_switch
               state: ON

Lets say you wanted to use that binary sensor to cycle through a list of options in a input_select and you want to use a long press to cycle backwards for example.

binary_sensor:
  - platform: gpio
    id: my_button
    name: "My Momentary Button"
    pin: 
      number: D5
      mode:
        input: True
        pullup: True
      inverted: True
    on_click:
      - min_length: 50ms 
        max_length: 350ms
        then:
           - select.next:
               id: my_select
               cycle: true

      - min_length: 500ms 
        max_length: 1000ms
        then:
           - select.previus:
               id: my_select
               cycle: true

You gotta really lean into those documentations and learn how to use them, get better at understanding them and things will start fitting together and making much more sense, i promise but if you try using a strategy of hoping YouTube or ChatGPT will always figure it out for you like so many others then it’s probably not going to go well for you and you’ll always be unsure of everything.

I didn’t ignore or not read the documentation.
I started with the documentation and didn’t get where I was hoping, so asked. Is that unreasonable? (to be really clear, that’s a rhetorical question)

The on_click, on_press etc havent been the missing piece at all - its what to do with those events to make them usable in HA that I haven’t been able to find.
Maybe you missed the ‘usable in HA’ part of question (and then multiple later explanations of the same).

I havent mentioned YouTube or ChatGPT at any point. By all means ask where I’m coming from, but please don’t assume incorrectly and then chastise for things that haven’t happened.
I believe that can quite fairly be called a d1ck move.

This is the exact approach I’ve read others giving as a reason for giving up on these forums. It doesn’t help anyone. Can you really not find something more constructive to do? (another rhetorical question).

@ Karosms number approach works, but seems pretty ‘cludgy’. As does using a cover.
I’m not looking to cycle through a list.

If the answer to the question I asked (not something else) is in the documentation, you could always just link to it… but I note you haven’t :wink:

To help out anyone who might stumble into this hoping to find how to do something similar… There is a really clean (not cludgy) way to do it, that I found elsewhere.
It’s partly explained in the documentation, but I think its fair to say not fully.

Create a binary sensor in the ESPs .yaml, something like this:

binary_sensor:
  - platform: gpio
    pin:
      number: P6
      inverted: true
      mode:
        input: true
        pullup: true
    name: "Button"
    on_multi_click:
      - timing:
          - ON for at most 350ms
        then:
          - homeassistant.event:
              event: esphome.button_pressed
              data:
                message: IR button was pressed
      - timing:
          - ON for at least 500ms
        then:
          - homeassistant.event:
              event: esphome.button_pressed
              data:
                message: IR button was held

In the automation you want a press/hold/double-press to trigger, add a trigger, chose ‘Manual event’ and set it like:

trigger: event
event_type: esphome.button_pressed
event_data:
  message: IR button was held

Same thing in the visual editor:

Just make sure to make the message: line’s unique and to then enter those into the trigger(s).

If you have any trouble with the automation triggering, you can check what homeassistants receiving in Developer tools>Events.
Enter what you’ve set as the event_type in the ‘Event to subscribe to’ box, click the Start listening button then press, hold or double-click the button.
It should show what HAs received, like this:


(then you can be really sure of the message: that has to go in the automation trigger).

Ill give you that one. The documentation could certainly be a little bettet articulated and more examples wouldnt hurt, obviously.

Those are functions that you would use to create automations on the esphome device itself. You can do basically all the same things in HA as well but, you really dont want to split up the entities on a board by putting all of its logic and automations on HA because, if they ever have an issue pr HA crashes then so does your capabilities to interface with those entities on the esp board.

You only have to create the bare bones configuration for a button/switch/whatever in Esphome for it to be available to use in HA. Most require the Platform type, name, id, and individual options based on whichever entity you are making. To make a button that i can use in HA, this is all it takes…

binary_sensor:
  - platform: gpio
    pin: 2
    id: test_button
    name: "Test Button"

Once you flash that basic configuration then you’ll be able to access that entity in HA. Everything else from the Automations, Filters, etc are all optional and you may or may not need to use any of that stuff every time but, at the bare minimum you should set up the on_press, on_click, on_state, etc and give the esp device a list of instructions for what should happen when “on_click” happens or else you’ve just got a useless button that doesnt do anything.

One of the best things you can do to help learn things more accurately and quickly is by keeping a breadboard with a dedicated “testing” esp board. If your unsure about how something works or what characteristics it may or may not have, then write up a little test configuration on your testing esp board and flash that test/experiment and then see for yourself! Thats what i did when starting out and actually still do it to this day. IDK why but, some people are anxious and act like they have to have perfection with everything before they attempt to flash a configuration onto an esp… Making mistakes and experimenting are excellent ways to learn new things so, dont be one of those anxious perfectionist people and you’ll have a far better time.

Oh No!! That’s impossible, sir! I see everything and i even saw what you did last summer!!! ominous
background music

; )

I didnt miss it though. You’re just confused or overwhelmed like everyone else when they start into this and even includes myself believe it or not…

Esphome and HA work together beautifully but, they arent dependent on each other and in fact, they can both be used completely independent of the other so, you can setup a smarthome system with devices using nothing but Esphome as it has its own capabilities for automations and UI interfaces. It just wouldn’t be as good as HA or as newbie friendly but, definitely possible and thats kind of what those "on_click, on_press, etc automations are for and dont overcomplicate that. You dont have to choose to use either Esphome or HA for your automations and you can use it in any capacity you want. For e example, lets say you have a physical button wired to an esp32 and a string of LED lights. At the bare minimum, you want to create an “on_click” that instructs the esp32 to turn On those LED’S when pressed and if pressed and held or double pressed, that is what you would use to tell the esp32 to turn OFF the LED’S… Then you can go into HA and make an automation that sais, “when you arrive home to turn on those LED’S” and you can make automations from both systems.

You just want to build in reliability and some “insurance” so that if HA has a glitch that you dont accidentally disable all of your physical controllers around your house because all the instructions/automations were handled by HA and without HA, you have a bunch of dumb buttons that dont know what to do without HA telling them, thats bad news, trust me on that one!

Dude… My whole comment was nothing but a huge chunk of my time that i dedicated tp trying to help you and explain things more clear. I didnt accuse you of using AI or even suggest you were using it. I was simply explaining to you that those very popular choices many people do use, they dont actually teach you anything or help you to start understanding more and more of this firmware to where the pieces of the puzzle start fitting together more easily and start making more sense.

I dont personally care about others reasons for giving up and i would advise you not to care either! Lots of people give up and get pissy because they arent willing or interested in making any efforts to learn and aren’t interested in making an investment into themselves and thats to bad because, its the same process for anything else in life and you either decided long ago whether your a quittet or not so, who else quit or why they quit is irrelevant unless you’re the type of person just looking for excuses or the type thats more interested in finding things to be offended by instead of seeing a time consuming good deed that wasnt offensive all but, you chose to interpret the words inorder to be offended and whatever the case may be, i personally don’t care.im here to try and help people and not coddle them like a down syndrome baby.

Now, if you want or need help then im happy to help you but, i say what i think and what i believe to be true and refuse to live in fear of offended other people thats not other peoples responsibility. Thats a parents job to teach the skills needed for someone to be a well adjusted individual that learned long ago that the world doesnt revolve around them or their feelings.

Sometimes i joke, sometimes im straight to the point and sometimes im irritated and can be abbrasive, just like every other human being. Im a big boy though. If there’s a miscommunication or something “could be” interpreted as offensive then just speak up and call it out. If it needs clarification then i can do that and if it was abrasiveness, well im not afraid to admit when im in the wrong and then everyone can move forward. I would recommend you trying to aquire some of those skills too because, my post was not intended to be harsh or to put someone down and im certain of that because ive definitely laid into some people in these forums when they had it coming. You got the kid gloves type of assistance and you really made some pretty far leaps to twist my words into the ones that would offend you and with all seriousness. If you go around seeking to be offended then thats what you’re going to find. Try finding some positive characteristics instead! Life is to short to bicker about foolishness.