Is there a way in Esphome to create button and switches that shows up in Home Assistant automation editor as input triggers in the same way as Tasmota does?
Fixed in 2024.5.0 with event entities support.
Is there a way in Esphome to create button and switches that shows up in Home Assistant automation editor as input triggers in the same way as Tasmota does?
Fixed in 2024.5.0 with event entities support.
The only workaround I’ve found so far is this one, but it is not as straightforward as tasmota’s
You can actually freely choose the events esphome should trigger in ha:
homeassistant.event
ActionWhen using the native API with Home Assistant, you can create events in the Home Assistant event bus straight from ESPHome Automations.
# In some trigger on_...: # Simple - homeassistant.event: event: esphome.button_pressed data: message: Button was pressed
To expose short/long/double presses by “default” from esphome will not work because by “default” there are simply none.
But if you define them you can just add a event like described above and you should end up with something you wish I guess?
Yes this works but it’s not so easy to mantain because events are not auto discovered. I was looking for a way to have the possibilities appear in a drop down list (as tasmota buttons are auto discovered by home assistant).
on this feature!
I’m also looking for user friendly method.
The events solution works but is very cumbersome as events are not auto-discovered.
I tried a workaround with button entities, but as the documentation already mentions:
If you press a button using this action, no button press event will be triggered in Home Assistant.
Attempt:
button:
- platform: template
name: "Button 1 Single"
id: button_1_single
binary_sensor:
- platform: gpio
on_multi_click:
- timing:
- ON for at most 0.5s
- OFF for at least 0.2s
then:
- lambda: |-
id(button_1_single).press();
As expected this didn’t work, but if it did it looked quite allot like Tasmota:
Also something I noticed, when using Tasmota’s multi press triggers these show up in the recently added automation section on the device’s page. But when using ESPHome events they do not.
Tasmota:
ESPHome:
I still like the Tasmota implementation more but I found a ESPHome workaround:
Add a select template sensor to the node’s configuration:
select:
- platform: template
name: "${device_name} button1"
id: "${device_id}_button1"
optimistic: true
options:
- none
- single
- double
- triple
- hold
initial_option: none
And select the according action during a multi press:
on_multi_click:
- timing:
- ON for at most 0.5s
- OFF for at least 0.4s
then:
- logger.log:
format: "Single"
level: INFO
- switch.toggle: shelly_relay_1
- select.set:
id: "${device_id}_button1"
option: "single"
- select.set:
id: "${device_id}_button1"
option: "none"
invalid_cooldown: 0.5s
This results in a device page with working linked automation:
And also is kinda user friendly when configuring automation’s:
Unfortunately HomeAssistant only allows a change to be from-to
instead of any-to
as this would have made the configuration more intuitive. But it works
One added bonus is that HomeAssistant now keeps track of which multi_press
command had been received in the past:
I think for the moment is just easier to stick to tasmota for devices like the Shelly i3. I hope in the future developers will integrate some kind of easy auto discovery without workarounds
Since this topic was constantly hitting my search results, trying to find best possible solution within esphome, in middle of 2023:
While events are still not discovered and not present on device triggers, if you write automation using an esphome event and retain device_id
from data payload, the automation will be linked to the device when looking at the device information. You can find device_id
by listening to events in developers options.
For example, such automation on HA side:
alias: đź’ˇ Entry/LED - Manual
trigger:
- platform: event
event_type: esphome.button_pressed
event_data:
device_id: cd7d0c7fedd5181a06d9e11c473fbd1b # device_id as seen in event listener
[...]
Is tied to device from device page:
So while you still need to know to which event you need to subscribe if you are using events fired from ESP Home, they will be tied to devices and you will know in which automation device is being used.
I see that Home Assistant 2023.8 has brought event entities Event - Home Assistant . Did someone try to implement these in Esphome?
I have finally found a workaround . As of now the only reliable solution is to create template binary sensors in esphome and then set up an automation that listen when that binary sensor gets activated. Unfortunately this leads to duplications in the log. Template buttons unfortunately triggers when the device gets rebooted.
I hope that someday event entities will be supported by esphome Adding support for Home Assistant event entity · Issue #2433 · esphome/feature-requests · GitHub
This example covers a shelly I3:
binary_sensor:
- platform: gpio
pin:
number: GPIO12
mode: INPUT
name: Switch 01
filters:
- delayed_on_off: 50ms
on_release:
- binary_sensor.template.publish:
id: btn_01_release
state: ON
- delay: 30ms
- binary_sensor.template.publish:
id: btn_01_release
state: OFF
on_multi_click:
- timing:
- ON for at least 1s
then:
- logger.log: "Hold"
- binary_sensor.template.publish:
id: btn_01_hold
state: ON
- delay: 30ms
- binary_sensor.template.publish:
id: btn_01_hold
state: OFF
- timing:
- ON for at most 1s
- OFF for at least 0.05s
then:
- logger.log: "Single Clicked"
- binary_sensor.template.publish:
id: btn_01_single
state: ON
- delay: 30ms
- binary_sensor.template.publish:
id: btn_01_single
state: OFF
- platform: gpio
pin:
number: GPIO14
mode: INPUT
name: Switch 02
filters:
- delayed_on_off: 50ms
on_release:
- binary_sensor.template.publish:
id: btn_02_release
state: ON
- delay: 30ms
- binary_sensor.template.publish:
id: btn_02_release
state: OFF
on_multi_click:
- timing:
- ON for at least 1s
then:
- logger.log: "Hold"
- binary_sensor.template.publish:
id: btn_02_hold
state: ON
- delay: 30ms
- binary_sensor.template.publish:
id: btn_02_hold
state: OFF
- timing:
- ON for at most 1s
- OFF for at least 0.05s
then:
- logger.log: "Single Clicked"
- binary_sensor.template.publish:
id: btn_02_single
state: ON
- delay: 30ms
- binary_sensor.template.publish:
id: btn_02_single
state: OFF
- platform: gpio
pin:
number: GPIO13
mode: INPUT
name: Switch 03
filters:
- delayed_on_off: 50ms
on_release:
- binary_sensor.template.publish:
id: btn_03_release
state: ON
- delay: 30ms
- binary_sensor.template.publish:
id: btn_03_release
state: OFF
on_multi_click:
- timing:
- ON for at least 1s
then:
- logger.log: "Hold"
- binary_sensor.template.publish:
id: btn_03_hold
state: ON
- delay: 30ms
- binary_sensor.template.publish:
id: btn_03_hold
state: OFF
- timing:
- ON for at most 1s
- OFF for at least 0.05s
then:
- logger.log: "Single Clicked"
- binary_sensor.template.publish:
id: btn_03_single
state: ON
- delay: 30ms
- binary_sensor.template.publish:
id: btn_03_single
state: OFF
- platform: template
name: Button 1 single press
id: btn_01_single
- platform: template
name: Button 1 hold
id: btn_01_hold
- platform: template
name: Button 1 released
id: btn_01_release
- platform: template
name: Button 2 single press
id: btn_02_single
- platform: template
name: Button 2 hold
id: btn_02_hold
- platform: template
name: Button 2 released
id: btn_02_release
- platform: template
name: Button 3 single press
id: btn_03_single
- platform: template
name: Button 3 hold
id: btn_03_hold
- platform: template
name: Button 3 released
id: btn_03_release
A “template” button or switch is what you use, im not sure where virtual button came from but, it probably confuses people who dont know. Those Esphome Docs are pretty helpful if you use them, right? You know that you can do the opposite as well. You can send HA sensor states or services from a HA based button/switch to an esphome device. For example, if you needed the current outside temperature, you can get that sensor value from HA. This stuff is really helpful and good to learn how to use it.
Ok I have changed the description accordingly.
I don’t know if there Is a Better solution to have physical buttons autodiscovered in HA automation editor as automation triggers (as they do in tasmota), but for now this solution works perfectly fine.
Probably a Better solution It would be to to edit Esphome source code in order to be able tò pass button states directly to HA avoiding to call a HA service to do It. Unfortunately this Is beyond my abilities.
Editing the esphome source code would be a better solution?? You are clearly very confused anout how this works on the most basic level.
Button, switch, sensor, etc states are picked up by HA automatically and you dont need an automation for that.
When you create a button or any entity in esphome, that entity is automatically added to HA as an entity and the states or sensor values are updated in real time. Theres nothing extra you have to do.
You asked how to trigger a HA automation from a esp button press. You can do this 2 ways. Either send a service call to HA that triggers an automation or the other way is just use the button press as the trigger in your HA automation.
I dont understand what are you even trying to do?
Here’s another problem i see. When you created the binary_sensor did you even go look in HA to see that it was there?
You dont need that template button. A template button would be used for example, if you have a motor attatched to the esp board, you could create a template button and add and on_press automation…
After you flash the board, a new button entity will be available in HA and when you push that button in HA it will trigger the esphome automation and you can control the motor that way
I think the problem here Is that you did not ready the thread.
This Is the only way to have physical button presses events autodiscovered in home assistant automation editor. If you use custom events (as you suggest) to achieve the same result you Will find that the automation Will not be linked tò the device in home assistant, and you have to Remember the custom service data and parameters.
If you read the documentation you would know that template buttons presses are not passed from Esphome to home assistant (so that Is why I was proposing tò modify the componenti)
No. Physical buttons or binary sensors states are passed to HA automatically. Theres nothing you need to do extra after you create the entity in esphome. A button press is NOT a HA event, its just a state change and thats what you use in your automations, a state change.
Ive been using esphome and HA for years and im very familiar with the documentation and how it works.
Template buttons are not passed between HA and Esphome? Did you fall and hit your head? Theres no reason to pass a template button presses from esphome to HA! Thats not what they are for. A template button is for creating a button entity in HA that triggers an automation based in the esp device.
You’ve got everything all backwards and dont understand what you’re doing and are using things the wrong way.
I maintain and have built nearly all of these esphome projects and spend hours on here helping amateurs like you so tell me again what i dont understand. You want to argue with me over bare minimum basic functions, the problem is i have receipts to back up what im talking about.
Heres some buttons for you, even though i dont understand.
Now if you want help and explain what your trying to do, i will help you. If you want to insult me and tell me idk what im talking about, well you can keep fumbling around and not figure out what your doing.
What I’m trying to do is to have physical buttons presses autodiscovered (and linked to the device) in Home Assistant automation editor. For now the only solution that I have found is to create template buttons, even if it is not the proper way to do this.
I know that creating events works (as suggested in the documentation), but those are not autodiscovered in the automation. Unfortunately Esphome does not support Event entities as far as I know. I don’t mean to insult you, I’m just trying to find a way to achieve this result that is not a workaround.
Im just comfused why you think your button isnt showing up in HA.
If you still have your button set up as a gpio Binary sensor then it does show up.
Go to Developer Tools and find the binary sensor that is your physical button and watch the state change in developer tools as you push the button.
In HA automation you just use the state change as your trigger or as the condition.
Its that simple…
Define the gpio binary sensor in esphome.
In HA, use the state change for your automation.
Events are specific to HA and what is happening in HA, not esphome. Events is the wrong thing to use here.
Explain what is supposed to happen when you push the physical button. Ill walk you through this so you understand it.
You may think “well its ok, its working like this”
Things only get more complicated as you go further down the HA rabbit hole and if you start off by doing thkngs the wrong way, you’re just going to make things in future harder than they need to be and cause yourself unnecessary problems.