Can a nested automation trace the original trigger event?

If you have a situation where one automation calls another, is there any mechanism for the last automation that is called to trace back the original trigger event that set the chain off?

For example, if I have one automation that detects when motion sensors are tripped, and this automation then triggers three separate automations depending certain conditions. One manage how the lights react, one to manage how the CCTV reacts, and one to send to notifications to key holders.

I have the three different automations to carry out the actions, lights, notifications and CCTV as separate automations, because I call them in multiple ways from multiple places, and I want a single place to store the actions so I only need to change or update them once.

I’m using automations rather than scripts because it’s going to be easier for someone to come along later on and see what’s happening or to make changes (It’s frankly easier to document using screen shots than code annotations).

At the moment I’m using storing data on what is triggered using helpers, which the automations read in and act on. Is there any way that they can pick up the original trigger action directly?

For example, if a motion sensors is triggered I store the name of the motion sensors in a helper, and then trigger the notification automation. The notification automation then reads the contents of the helper and adds it to the notification to tell the recipient which motion sensor has been triggered.

I’d like the notification automation to be able to pick up the trigger ID set in the original automation, or to check which sensor has been tripped directly, without having to use a helper (I simply have too many of these and it’s getting messy).

Ideally I’d like to do it without having to search the Last Triggered events in every sensors to see which one was triggered most recently, as I’d like the automations to be unaware of the sensors, as not all of my devices are visible in the same way which is why I have one automation that gets inputs and another than manages outputs (I only need to change or update the inputs on one place)

Is this possibly\practical?

EDIT: Please remove the “Solved” tag. This hasn’t been solved because it turns out that scripts can’t do this either.

That is not how automations are supposed to be used. Manually triggering automations should be used for testing only.

Use scripts. Then you will get context of the calling automation.

1 Like

That is not how automations are supposed to be used. Manually triggering automations should be used for testing only.

Sorry, I wasn’t clear, the automations aren’t being triggered manually, they are being triggered by event.

For example. I have one automation that’s triggered by my contact sensors, and one that’s being triggered by motion sensors. I split them up as the conditions for each one are different and having them all together in a single automation just made the automation unwieldy.

If those conditions are met they then trigger a further automation. Such as one to notify key holders.

This is done so that I don’t need to have multiple copies of the code to run the notification, otherwise I’d need to change it in multiple places every time I tweaked it.

I’m trying to avoid using scripts because I want to do as much as possible in the GUI. I everything to be accessible to non programmers.

So you are not triggering one automation manually from another automation, as described in your example?

Perhaps share your automation config to clarify things.

So you are not triggering one automation manually from another automation, as described in your example?

Sorry, I’m still not being clear. Nothing is being triggered manually, I’m using the normal series of events and conditions from the GUI.

Perhaps share your automation config to clarify things.

I’m just using the normal YAML to set the state of a helper in one place, and then reading it back in in another place.

Please share an example config.

Please share an example config.

I’m not really sure what you want to see. I’m just using standard service calls form the GUI. If an event is triggered on device and if certain conditions are met then perform one or more actions depending on which conditions are true

service: automation.trigger
metadata: {}
data:
  skip_condition: true
target:
  entity_id: automation.alarmevent_notification
service: automation.trigger
metadata: {}
data:
  skip_condition: true
target:
  entity_id: automation.alarmevent_Camera
service: automation.trigger
metadata: {}
data:
  skip_condition: true
target:
  entity_id: automation.alarmevent_lights
service: automation.trigger
metadata: {}
data:
  skip_condition: true
target:
  entity_id: automation.alarmevent_siren
service: automation.trigger
metadata: {}
data:
  skip_condition: true
target:
  entity_id: automation.alarmevent_smartwater
service: automation.trigger
metadata: {}
data:
  skip_condition: true
target:
  entity_id: automation.alarmevent_baydoorlockdown

I’m probably still not being clear with what I’m looking for.

I have several automations that deal with different types of input (a panic buttons being pressed, motion being detected, and so on). The different conditions are complicated so having one automation to deal with everything is simply too unwieldy.

The automations essentially trigger the same outputs (setting off an alarm, locking bay doors down), so rather than copying and pasting the code for these actions into each automation I’ve put them in their own separates automations so that if I make any changes I only have to do it in one place.

All that I’m really looking for is whether or not there is a way for the automation being called to see what the trigger ID was on the automation that is calling it.

This is usually pretty simple in most programming languages, you just put it as a variable in a function call. But YAML doesn’t seem to include these, or at least not using a syntax that I know.

Right now I’m simply saving it to a helper and then reading it in later on.

I’m not using scripts because I don’t want to give anyone access to scripts, I want to stick with the GUI.

If it’s simply not possible to do what I’m asking, please just say so.

I’m looking to make what I’m doing smarter or simpler, rather than to change how I do it entirely, as this way is good for me. Scripts are not good for me right now.

This is what I am talking about. Do not trigger automations manually.

1 Like

This is what I am talking about. Do not trigger automations manually.

I think that we’re using a different definition of the word “manual”.

The code that you’re highlighting is being triggered by an event and a complicated series of condition, the process is entirely automated using the GUI in HA. I’ve simply split the action out into its own separate automation so that I call it from several different places and only maintain one copy of the code.

For example, pressing a panic button or triggering a motion sensor will cause the exact same thing with the CCTV based on whatever conditions are met, so rather than having two separate copies of the code for the CCTV I’ve bundled everything up into a single automation so that any changes that I make only need to be made in one place.

As I understand it, this is the recommended way of using things like scripts, I’m just using an automation instead because it’s for a non-programmer to do things like adding in new lights using the automation GUI than using scripts.

Could you please go back to my original question, all I’m really interested in is knowing if there is neater way to pass the trigger ID to the automation than writing it to a helper.

If this isn’t possible, could you please just say so.

By triggering an automation from the outside, you are skipping both triggers and conditions in that automation. So you are reducing an automation to a script with lots of extra baggage. Calling a script is very similar to triggering an automation to the reader, but that reader will be confused by conditions that are not evaluated and triggers that weren’t fired. So your whole point of it being easier to understand is arguably not true. Make a script, call it from each automation that needs it. Pass whatever information you want to it. The script name can be a better description than that of the automation anyway. It can’t get any clearer than that.

1 Like

With all due respect, could we please return to the original question.

I’m not saying that you are wrong, just that this isn’t the right solution for me at this time.

It is obviously not the right solution or you would not be asking about how to trace the actions.

If you follow the advice from Edwin and I you won’t have this issue.

2 Likes

It is obviously not the right solution or you would not be asking about how to trace the actions.

It’s running like a dream. I’m simply looking to reduce the number of helpers on my system.

If you follow the advice from Edwin and I you won’t have this issue.

I don’t have anyone that I trust to be able to correctly edit scripts or to properly document what they did. Limiting people to the automation GUI is much safer.

Would you allow a non-coder to access you’re scripts?

Could we please just stick with the original question.

I understand that you have your way of doing things, and that you’ve probably invested a lot of effort into scripting everything, and I’m not saying that you are wrong, but it’s not the best solution for me at this time.

You know there is a GUI for scripts too?

And it is the right way for a reason. One you have discovered yourself.

3 Likes

OK, I understand that you mean well, and that you’re obviously passionate about this, so I encourage you to explore it with other users in other threads, but I’m not in a position right now where I can re-write a live system that I’m still figuring out myself.

I’m just looking for an answer to my original question so that I can possibly make a small tweak to my code to reduce my use of helpers.

Would it be possible if we could please concentrate on that one thing, and put a pin in everything else for later?

If I want to move over to scripts, I’ll start a fresh thread for that.

I believe you already got your answer but you just refuse to listen.
It does not work, and you should move over to scripts.

3 Likes

I believe you already got your answer but you just refuse to listen.
It does not work, and you should move over to scripts.

I have listened to several people making alternative suggestions, I’ve acknowledged that they’ve made them, and I’ve said that this may be the direction that I will choose to go in the future, but it’s not the right direction for me at this moment in time.

I’m simply not in a position to make such a dramatic change to a live system at this time.

So, could we please return to the original question.

Respectfully. You’ve had multiple respectful. Responses including Tom’s direct response in post 2…

Thats not how automations work. Don’t do that except in testing for the EXACT reason you came here for help.

Im sorry you don’t LIKE the answer means you have to rethink the entire structure of your automations but it’s unfortunately true.

If you want waht your original question asked. You don’t fo it that way because you get exactly what you got. If you don’t want that do what Tom said.

4 Likes

Respectfully. You’ve had multiple respectful. Responses including Tom’s direct response in post 2…

I have read and acknowledged each of these suggestions, but I’m not actually able to actually implement them, what else do you want me to do?

Im sorry you don’t LIKE the answer means you have to rethink the entire structure of your automations but it’s unfortunately true.

Again, with respect, all that people have said is that this isn’t best practice.

Could you please just say in clear and unambiguous words “No, there is no mechanism in HA to allow one automation to determine the trigger I’d from another. It categorically is not a function that HA can do”.

If it’s not possible, then please can people just say so and stop posting.

If I want to try scripting at a later date I’ll open a new thread about it

If you don’t want that do what Tom said

Tom said

That is not how automations are supposed to be used.

I took note of this, and put a pin in it for a possible future re-write of my system, it’s over, it’s done. I’m not currently able to follow Tom’s suggestions, I’d just like a straight answer as to whether it’s mechanically possible.

There is not much you need to do to make it scripts.
You probably just need to copy what is below action: to your new scripts and press save.
That’s it.
If you’re smart you save your scripts with the same name as the automation and you can just make a search and replace of automation to script (perhaps something slightly more).
But that is the drastic change you need to move over to scripts.

2 Likes