Simulate a device trigger for use in GUI

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?

I thought about trying something like:

- type: entity-button
  name: Kitchen Light On
  entity: light.kitchen_light_level
  service: homeassistant.turn_on
    data: binary_sensor.switch_158d0002241b37

(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.

Any ideas?

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…)

alias: Kitchen Remote Button 1
initial_state: 'on'
trigger:
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0002241b37
      click_type: single
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0002241b37
      click_type: double
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0002241b37
      click_type: long_click_press

action:
  - service: script.toggle_to_max 
    data_template:
      entity_id: '{% if trigger.event.data.click_type == "single" %} light.kitchen_light_level {% endif %}'
  - service: automation.turn_off
    data_template:
      entity_id: '{% if trigger.event.data.click_type == "single" %} automation.kitchen_light_auto_on_motion {% endif %}'
  - service: automation.turn_off
    data_template:
      entity_id: '{% if trigger.event.data.click_type == "single" %} automation.kitchen_light_off_after_2mins {% endif %}'

  - service: light.turn_off
    data_template:
      entity_id: '{% if trigger.event.data.click_type == "double" %} light.kitchen_light_level {% endif %}'
  - service: automation.turn_off
    data_template:
      entity_id: '{% if trigger.event.data.click_type == "double" %} automation.kitchen_light_auto_on_motion {% endif %}'
  - service: automation.turn_off
    data_template:
      entity_id: '{% if trigger.event.data.click_type == "double" %} automation.kitchen_light_off_after_2mins {% endif %}'
      

  - service: automation.turn_on
    data_template:
      entity_id: '{% if trigger.event.data.click_type == "long_click_press" %} automation.kitchen_light_auto_on_motion {% endif %}'
  - service: automation.turn_on
    data_template:
      entity_id: '{% if trigger.event.data.click_type == "long_click_press" %} automation.kitchen_light_off_after_2mins {% endif %}'

The way the above works is to create 3 modes for the light: ON, OFF and AUTO (motion controlled)

You can just create an input_boolean and then use that as another trigger in your automation.

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 :wink:

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.

ie:

  - entity: input_boolean.party_mode
    icon: mdi:glass-cocktail
    tap_action: call-service
    service: automation.trigger
    service_data:
      entity_id: automation.cube__party_mode_shake_air

The above works in Lovelace when I tap an entity in a glance card

Oops, you’re right. I didn’t scroll down in the automation.

It’s too early…or late… :sleeping:

As far as the extra data in the service call I have no idea.

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)

Well I can confirm that I haven’t been able to get it to work with my attempts so far.

  - id: testcard
    type: entity-button
    entity: light.kitchen_light_level
    tap_action: call-service
    service: automation.trigger
    service_data:
      entity_id: automation.kitchen_remote_button_1
      platform: event
      event_type: click
      event_data:
      entity_id: binary_sensor.switch_158d0002241b37
      click_type: single

…didnt work. Hopefully from the above someone with the know-how will understand what I’m trying to acheive and have the answer…

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
image

Then I added this in my automations to trigger the scene when the physical button is pressed.

- alias: 'Inovelli - push/hold A'
  trigger:
    - event_type: zwave.scene_activated
      platform: event
      event_data:
        entity_id: zwave.inovelli_nzw30
        scene_id: 2
        scene_data: 1
  action:
  - service: scene.turn_on
    entity_id: scene.long_press_a

- alias: 'Inovelli - 2 tap A'
  trigger:
    - event_type: zwave.scene_activated
      platform: event
      event_data:
        entity_id: zwave.inovelli_nzw30
        scene_id: 2
        scene_data: 3
  action:
  - service: scene.turn_on
    entity_id: scene.2_tap_a

- alias: 'Inovelli - 3 tap A'
  trigger:
    - event_type: zwave.scene_activated
      platform: event
      event_data:
        entity_id: zwave.inovelli_nzw30
        scene_id: 2
        scene_data: 4
  action:
  - service: scene.turn_on
    entity_id: scene.3_tap_a


- alias: 'Inovelli - push/hold B'
  trigger:
    - event_type: zwave.scene_activated
      platform: event
      event_data:
        entity_id: zwave.inovelli_nzw30
        scene_id: 1
        scene_data: 1
  action:
  - service: scene.turn_on
    entity_id: scene.long_press_b

- alias: 'Inovelli - 2 tap B'
  trigger:
    - event_type: zwave.scene_activated
      platform: event
      event_data:
        entity_id: zwave.inovelli_nzw30
        scene_id: 1
        scene_data: 3
  action:
  - service: scene.turn_on
    entity_id: scene.2_tap_b

- alias: 'Inovelli - 3 tap B'
  trigger:
    - event_type: zwave.scene_activated
      platform: event
      event_data:
        entity_id: zwave.inovelli_nzw30
        scene_id: 1
        scene_data: 4
  action:
  - service: scene.turn_on
    entity_id: scene.3_tap_b

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

It’s either that or try to pass variables to the script so that I don’t have to split the scripts up

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

Edit. Maybe this example can be helpful