Add a service to fire an event

It would be good to have a FireEvent service that allowed an event to fire…
This way we could have an Entities Card with call-service rows that trigger automatons associated with the event that is fired.

You can do that in an entities card with the special row element call servcie.

The service is automation.trigger

Cool… didn’t know about that one …
However my event is still not firing … here is the yaml:

  - type: call-service
    name: Home Control - Living Room TV
    action_name: Cast
    service: automation.trigger
    service_data:
      entity_id: automation.control_on_living_room_tv_on

… any ideas?

Ok … home assistant wasn’t firing ANY events … had to restart it!
… working now tho :slight_smile:

I know this is “solved”, but the accepted solution does not really solve the actual initial issue.
I want to fire an event from a service-call, not just trigger an automation. As I have events that will do multiple things, and automations that behaves differently based on the event that triggers them.

3 Likes

Yeah this is NOT solved.
I’m surprised one can’t simply fire an event from a button or whever, the same way it’s done in Developer Tools.

Firing events is specially relevant now that there are trigger_id’s and Choose, so an automation can have multiple triggers that make the automation go different routes…

Well,
In all fairness, it is pretty trivial to fire an event from a script. It is an extra (possibly unneccesary) step, but a simple script with

sequence:
- event: MY_EVENT
  event_data:
    name: myEvent
    customData: "{{ myCustomVariable }}" 

can be triggered from a button or whatever.

But I do agree that it should be possible to fire events directly.

I actually did it before reading your comment, the way you mentioned!

For anyone interested, I solved it via ONE script (for all), and expanding the data in the triggers.

First, the automation you want to build. Especially important the triggers info: id, event:data and command within. It’s a bit more than the simple use of triggers.

alias: Comedor y TV

trigger:
  - platform: event
    event_type: command
    id: comedor
    event_data:
      command: comedor
  - platform: event
    event_type: command
    id: tele
    event_data:
      command: tele

condition: []

action:
  - choose:
      - conditions:
          - condition: trigger
            id: tele
        sequence:
          - scene: scene.tele
      - conditions:
          - condition: trigger
            id: comedor
        sequence:
          - scene: scene.comedor_2
    default:
      - scene: scene.comedor_2
  - scene: scene.comedor
  - service: script.tado_home
    data: {}
mode: single

Second, the button calling the script with the data.

type: button
tap_action:
  action: call-service
  service: script.convert_to_event
  service_data:
    command: 'comedor'

You can re-use the tap_action part to any button, double_tap_action or hold_action!

And finally, the script to convert commands into events. Very simple:

alias: Convert to event
sequence:
  - event: command
    event_data:
      command: '{{ command }}'
mode: single

The event is command, and it contains event_data with command: and the actual command to be passed (from the button in step 3, to the automation in step 1).

Let me know if there’s a simpler way to do this!

4 Likes

You might find this repository interesting :grinning:

Please add custom event firing from UI controls. In the UI world of programming, all UI controls fire events, so allowing the same to fire events in Home Assistant would be a very natural behavior (actually, it’s almost unnatural that this isn’t an option).

I’m trying to set up a relatively complicated UI / dashboard, and the requirement of setting up scripts or automations for every control becomes tedious and doesn’t scale very well.

2 Likes

I found it very confusing, there’s no syntax to fire custom events directly. Just spent way too much time trying to fit script code into tap_action code, to no avail, obviously. Why can’t there be a service/action (whatever the name) for it to call?