Pass existing objects to script

Hi :wave:
After doing some research in the docs and the forum and making experimentations, I didn’t find any way to pass an existing object to a script during an automation.
Say I want to access some attributes of the trigger object in a script, the only way I found was to pass the attributes I want to access in the data_template when calling my script. But this is not very DRY since when I’ll need to update my script to access more attributes of this object, I’ll also need to updates all the callers.

Would that be something thay may be worked on?
Please let me know if the subject is already discussed some place else!

Below you’ll find some examples of currently working code and target.

Examples

Currently working code

# script
turn_on_light_low:
  alias: Turn on light low
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: "{{ entity_id }}"
        brightness_pct: "{{ states['input_number'][object_id + '_low'].state | default(20) | int(default=20) }}"

# automation (caller)
# ...
action:
  - service: script.turn_on_light_low
    data_template:
      entity_id: "{{ trigger.entity_id }}"
      object_id: "{{ trigger.to_state.object_id }}"

What I’d like to achieve

# script
turn_on_light_low:
  alias: Turn on light low
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: "{{ trigger.entity_id }}"
        brightness_pct: "{{ states['input_number'][trigger.to_state.object_id + '_low'].state | default(20) | int(default=20) }}"

# automation
# ...
action:
  - service: script.turn_on_light_low
    data_template:
        trigger: "{{ trigger }}"
# I know this wouldn't work as is in Jinja templating,
# it's just to express the idea: an easy way to pass objects to scripts

Even better

# automation
# ...
action:
  - service: script.turn_on_light_low
  # Nothing more, existing objects in the automation scope could be passed to scripts

you have access to hass object in your scripts so you can do anything there:

hass.states.get('sensor.contact_always_open').state
hass.services.call('timer', cmd, args)

is that what you’re after?
but keep in mind that everything that comes to script via data is a string.

protocol = data.get('protocol')
code = int(float(data.get('code', 0)))
state = data.get('state', None)

Nope :slight_smile: I need to access some attributes of the trigger object available in the automation.

Yes I’m aware, that’s why I’m proposing to allow passing full-fledged objects :wink:

I’ll move this post to the Feature Request section, which seems more appropriate.