Pass trigger.id to Script without YAML syntax?

I’m able to pass a variable to the scene selector and stay in the visual editor mode rather than YAML mode:

This is the YAML just to prove it really is a variable (and I tested it working):

I was wondering if it’s possible to do the same with "{{ trigger.id }}"?

This is what it looks like when I try to call my Script:

If it weren’t for that dynamic Trigger ID, I could have a nice clean UI like this:

Why is it that only scene selection allows variables in the visual editor? Also, is there a way I can fix my Trigger ID field to allow dynamic values like the scene selector?

Well, this kinda sucks, but I can do a Template for the Trigger ID instead of a Select:

That works, but it doesn’t give me any documentation as to the names I’m expecting in the script for these trigger names. Is there a way to document that cleanly?

Why not just keep the select, but make it not required… then template in a default of trigger.id if the input field’s value is undefined?

1 Like

I didn’t realize I could do that! Great idea :slight_smile:! I’ll try that out.

I tried that:

But then it kept me in the YAML mode as soon as I did it.

This is me calling a script that accepts data:


    - service: script.siren_on
      data:
        on_time_fld: 240
        volume_fld: 100

That’s all it takes.

Calling this script:

siren_on:
  description: 'Cycle pattern and timer for siren(s)'
  trace:
    stored_traces: 10
  fields:
    on_time_fld:
      name: Run the siren this long in seconds
      description:
        Number of seconds that the timer should sound
      required: true
      selector:
        number:
          min: 10
          max: 360
          step: 10
          mode: slider
      example: "35"
    volume_fld:
      name: How loud the siren
      description:
        Number from 0 to 100% volume level.
      required: false
      selector:
        number:
          min: 0
          max: 100
          step: 1
          mode: slider
      example: "45"
  variables:
    cycles: >
      {% if on_time_fld > 14 %}
        {{ (on_time_fld/15) | round(0,1) | int }}
      {% else %}
        1
      {% endif %}
    volume: >
      {% if volume_fld | default(0) %}
        {# Scale back down to 0 > 1. #}
        {{ volume_fld / 100 }}
      {% else %}
        0
      {% endif %}
  sequence:
  - service: switch.turn_on
    data: {}
    target:
      entity_id:
        - switch.front_panorama_l_siren_on_event
        - switch.front_panorama_r_siren_on_event
  - repeat:
      count: '{{ cycles }}'
      sequence:
      - alias: "Cycle siren 'cycles' times"
        service: siren.turn_on
        data:
          volume_level: '{{ volume }}'
          duration: "4"
        target:
          entity_id:
          - siren.front_panorama_siren
          - siren.front_panorama_siren_2
      - delay:
          seconds: 15
  - service: switch.turn_off
    data: {}
    target:
      entity_id:
        - switch.front_panorama_l_siren_on_event
        - switch.front_panorama_r_siren_on_event
  mode: restart
  icon: mdi:volume-high

I think any form you make trigger_id will make it look like a template.

Don’t try to define it in the selector… define it elsewhere:

fields:
  trigger_id:
    selector:
      select:
        options:
          - One
          - Two
          - Default
    name: trigger_id
    default: Default
    required: true
sequence:
  - variables:
      trigger_id: "{{ trigger.id if trigger_id == 'Default' else trigger_id }}"

Thanks for offering a possible solution!

I’ve been unsuccessful at getting variables to work in the past.

What is this code doing? Automatically setting the trigger_id in the script? Aren’t scripts lacking the trigger context?

You likely ran afoul of the scoping rules.

Assuming you have an input field trigger_id and some value passed from the originating automation trigger.id, it overwrite the value of trigger_id with the value from trigger.id if the value of the input field is “Default”.

Wasn’t the whole thing from your original post that you were passing the trigger variable from you originating automation?

When you don’t post the current version of the script in question we can only guess what you are trying to do…