Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 95414

Pass existing object to script

$
0
0

@Crocmagnon wrote:

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

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 95414

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>