How do you trigger complex automations from UI?

Let’s imagine you created very complex automation, via blueprint or manually. For example, the one for the two-button switch controlling multiple lights. So, multiple triggers, then conditions, a mix of scenes and service calls.

How would you trigger such automation from the UI? automation.trigger won’t work, as there’s no way to specify a trigger ID, scene.apply also isn’t the option as not every step is a scene.

One solution is to employ events for that:

  • Automation triggers pre-defined event(s) on device actions (e.g. click)
  • Same automation does the work, using event triggers
  • Lovelace UI components emit triggers

The issue of the solution above is that lovelace isn’t able to natively emit events. I have to write a script for that, and I need one script per event type, as event types aren’t “templatable”.

Do you know a better solution?

The automation editor is only intended for relatively simple automations. If you need something more complex you need to use YAML.

Why would you want to do that?

The automation’s trigger defines when it should begin to evaluate its condition and potentially execute its action.

The automation.trigger service is a misnomer because it doesn’t actually exercise the automation’s trigger but simply executes its action (optionally evaluates its condition). The “Run Actions” button is more accurately named because that’s the extent of what it achieves, effectively treating the automation like a script. However, this isn’t useful if the automation’s action references the trigger variable because that’s only defined when the automation is actually triggered by one of its triggers.

The only way to exercise an automation’s trigger is to actually trigger it via one of its triggers. If you add an Event Trigger just for testing purposes, it can allow you to trigger the automation via the UI (see below) but what purpose does that serve?

Developer Tools > Events

Screenshot_20211207-020530~2

Hi,
I’m using YAML already. I have no issues creating automations, but I would like to precisely trigger them from the lovelace dashboards

Of course, I can trigger events from the Developer Tools and it works. But not from the Lovelace dashboards, and that’s what I’m trying to achieve.

Right now it works like this:
Lovelace dashboard → call script → emit event → trigger automation (right part of it)

I would like to shorten it to:
Lovelace dashboard → … → trigger automation (right part of it)

I am confused, an automation is exactly that, something that happens automatically via set events.

I assume you want to trigger a set of actions via a Lovelace button for example then surely you want a script not an automation? As that the only difference between automation and script, one is automated and one is requested.

2 Likes

What exactly do you mean when you say “trigger automation”?

Unlike a script, an automation isn’t normally triggered manually. It listens for events and triggers automatically.

What is your application that requires manually triggering an automation?

Yeah all you need is:

Lovelace dashboard → call script

Also, apologies I completely misread your question before.

It’s an automation, because I create re-usable automations from blueprints (created manually). For example a blueprint for curtain motors. It has a wireless switch, sunrise/sunset bindings, other date and time conditions attached. As I have multiple curtains installed, I just re-use them and never copy the automation code. And then, I would like to, in addition to the automation triggers, call parts of automations from lovelace buttons.

type: button
name: Example
show_state: false
tap_action:
  action: call-service
  service: automation.trigger
  service_data:
    entity_id: automation.whatever

Then either strip out the parts you want and put the actions in a script and call this script - simple cut and paste exercise.

Or maybe create a Boolean and ad that as a trigger in the already existing automations.

Put those parts in scripts. Call the scripts from the automation or Lovelace.

I think the suggestion to use scripts is good but in this case blueprints are being used to generate automations.

Call parts of the automation!

I would assume if you want to simply trigger the whole automation the simply adding a helper as an additional trigger would suffice here?

However if as the statement implies “part of the automation” then I can’t see how this would ever be possible unless you strip out the “part” and place it in a separate script.

Maybe I am missing something but I don’t use blueprints at all - find them too limiting

Ok, use scripts and take off the training wheels.

An easy way for test purposes inside the automation to manual trigger a special trigger … this is what I would like as well.

@tom_l @123

Using scripts is a nice suggestion, but they’re less convenient compared to the blueprints

  1. They’re not portable (single file per project as in case of blueprints)
  2. There’s no configuration UI to set of variables
  3. I’ll have to pass all necessary arguments to the script in order to have all conditions evaluated

One example:
As I mentioned above, I have a blueprint/automation for a two-button switch, controlling 3 lights using 3 scenes. The scenes are: “only left light”, “only right light”, “both lights”. The brightness/color and temp are different, depending on the mode.

Then I have a blueprint, where I first set selector for a switch, lights, scenes

and then my conditions part looks like this (pseudo-code):

if left_button click then
  if left_light is ON:
    apply "only right light" scene
  else
    apply "both lights" scene
etc, etc

So I could theoretically move this part to script and call it from the Lovelace button or whatever, but then I’ll have to pass all variables to this script: lights, scenes, other parameters I’ve configured in automation. Doing the same multiple times - not my approach

Does using a Button card, to manually trigger an automation from the Lovelace UI, meet your requirements (see example posted above)?

Or are there other constraints/needs?

Absolutely not, as there’s no way to set a trigger_id, in order to execute the specific part of the automation. My blueprint is basically a set of triggers and one big switch statement by trigger_id

And there it is; evidence of your misunderstanding of how automations are designed to work. You still believe there’s some way to artificially induce a trigger without the actual trigger ever occuring. It simply doesn’t work that way.

A trigger_id is produced when the trigger occurs automatically, not manually (manually “triggering” an automation doesn’t actually exercise its triggers). It’s vital to understand that a trigger is a listener and it listens for state-changes/events. To cause the trigger to fire the listener must detect the presence of the specific state-change/event its monitoring. You’re seeking some means of either bypassing the listeners or fool them into detecting a non-existent state-change. The only way to satisfy a listeners requirements is to cause the state-change/event its designed to detect.

1 Like