Is it possible to simulate the action of pressing a physical button to pretend to HA that the button was pressed?
I have a number of Xiaomi wireless buttons which I use as light switches throughout the house. I am in the process of creating some Lovelace screens and was hoping to create an entity_button which if pressed, has the same effect as pressing the Xiaomi button. Is this possible?
(where binary_sensor.switch_158d0002241b37 is the Xiaomi button)
The reason for this is that the I don’t just want to turn the light off, I want to simulate what a button press will do, in this case: turn 2x automations off and turn the light on. My issue is that the Xiaomi buttons have 3 functions: single_click, double_click and long_press. Doing the above (it it was to work) would be like a never ending long_press which I don’t want. I in fact need to simulate all 3 functions using 3 entity_buttons.
To give a bit more info on this, I have the full automation for the Kitchen Xiaomi button below. One thing I did think of was to move the actions for each click type into 3 separate scripts and then I could call each script from a press of the entity_button, but it seems a bit messy to split up this automation. I have identical automations for other lights in the house so I would end up with a lot of code if I split them all up. (I would of course keep them in packages to contain it somewhat…)
not ready because the trigger has data attached, ie: single_click, double_click or long_press (see automation above). I need the action part of the automation to evaluate that data to work out which action to run. Otherwise I would have done that
I have just figured out that I can trigger an automation with a service call. The question now is can I pass a simulated single_click, double_click or long_press along with that. I don.t think I can.
I’m going to give it a try and see what happens. The good thing about testing things like this in lovelace is that it doesn’t crash HA! (ie: prevent boot, no need to restart HA to enable changes)
I’ll be honest, there is probably a better way to this but hopefully this very simple example may provide you a start point or at least some hints. If nothing else I think I at least understand what you’re trying to do.
So one way possible way to do this would be using scenes. You could move the actions you have defined into a new scene (one new scene per each action) then use the event triggers you have created to activate the scene instead of performing the action directly. Having created a scene in Home Assistant will then give you “something to click” in the GUI to activate the same action as press the button.
This specific example may not be practical in the real world but it provided a basic starting point to build from.
In my example I have a zwave switch that supports different button pushes similar to yours (long press, double tap, triple tap, etc)
I have created these simple scenes for testing in Home Assistant
- name: 'long press A'
entities:
group.bedroom:
state: on
- name: '2 tap A'
entities:
light.bedroom_ceiling:
state: on
- name: '3 tap A'
entities:
light.bedroom_lamp
state: on
- name: 'long press B'
entities:
group.bedroom:
state: off
- name: '2 tap B'
entities:
light.bedroom_ceiling:
state: off
- name: '3 tap B'
entities:
light.bedroom_lamp
state: off
In the GUI it looks like this
Then I added this in my automations to trigger the scene when the physical button is pressed.
I am far from an expert when it comes to scenes and automations so I wouldn’t be surprised if there is a better to this but hopefully this can at least point you in the right direction
I was starting to think that something along those lines is the only way to achieve this, although I will use scripts rather than scenes because I need to set automations on and off as well as turn the light on /off
I’m pretty sure you can use a scene to set your variable and trigger your script using the set variables but some one with more know-how would have to chime in on that.
You can also just add multiple actions to a scenes so it could both set an automation on/off change the lights as well