Call a Script from an Automation, and access Trigger/Entity inside the Script

The Title says it pretty much:

(How) is it possible to get the Entity that triggered an Automation, in a Script called from that Automation?
In the Automations, i can append Data to the call of each Script, and hand over the {{ trigger.entity_id }} by that.

But (how) is it natively possible to evaluate inside the called Script, what triggered the Automation before?

More Context:
I want to streamline my growing Notifications from HA. Depending on Devices and their Values, different notifications get fired.
E.g. if the Temperature in the Freezer is higher that -16°, all Users get a a silent Notification.
If it stays higher than that for more than 30 Minutes, an Alarm gets triggered.
If the Sensor renders “unavailable”, the Admin gets another (repeating) Notification.

There are many Notifications and even more to come. So it would be great if i would only have to call the corresponding Script, which know what Entity / State triggered the Automation before.

You need to pass the trigger variables to the script: https://www.home-assistant.io/integrations/script/#passing-variables-to-scripts

e.g.

action:
  - service: script.notify_level1_user
    data:
      trig_id: "{{ trigger.entity_id }}"
      trig_val: "{{ trigger.to_state.state }}"

And in the script

sequence:
  - service: notify.level1_users # or whatever your notification service is called
    data:
      message: " The sensor {{ trig_id }} has the value {{ trig_val }}"
1 Like

Thanks! Yes, thats how i do it right now:

But as i want to call that generic Script from a lot of different Automations:

Edit:
I thought there might be possibilities with context, parent,… But i just don’t get the Concept, and blindly tested State objects - Home Assistant weren’t successful.

Without passing the information it is not possible. The trigger variables are local to the automations.

You could try passing the entire trigger object to simplify things slightly:

action:
  - service: script.notify_level1_user
    data:
      trig_object: "{{ trigger }}"

Then in your script you should be able to access the properties of that object:

sequence:
  - service: notify.level1_users # or whatever your notification service is called
    data:
      message: " The sensor {{ trig_object.entity_id  }} has the value {{ trig_object.to_state.state }}"

Totally untested. May not work.

As for the previous trigger object properties, you will have to store them globally somehow. Either by publishing to an mqtt topic or by using this trick (read the whole rest of the topic):

1 Like

Too bad :frowning: Thank you very much for instant clarifiing! Saves me a lot of Time for Digging… :+1:

Really interesting how creative ppl get! :wink:
I thought about doing it the ‘cheap’ way - updating Helper in the Automation, and react on those.
Or i might come back to my first thought, and continue with (trying to) Blueprint that, namely baked-in data passing to the Script.

Thank you so much for both your instant and clear Clarification, and possible Solutions! :+1:

Consider the Alert integration.