Passing variables to wait_for_trigger in a script

Hi all,

Consider the following script, for some reason the script engine says data_template is an invalid key.
I’ve also tried several other combinations like using data instead of data_template with no luck.

Any help would be greatly appreciated.

toggle_irrigation:
  fields:
    main_switch:
      description: 'The switch used to to turn on/off'
      example: 'switch.sonoff_s20_02'
    alternate_switch:
      description: 'The switch we make sure is turned off'
      example: 'switch.sonoff_s20_05'
    mode:
      description: 'Is it turning on or off the irrigation'
      example: 'turn_on'

  sequence:
    - wait_for_trigger:
        - platform: state
          data_template:
            entity_id: "{{ main_switch }}"
            to: 'off'
      timeout:
        minutes: 5
      continue_on_timeout: true

    - wait_for_trigger:
        - platform: state
          data_template:
            entity_id: "{{ alternate_switch }}"
            to: 'off'
      timeout:
        seconds: 30
      continue_on_timeout: false

    - service: switch.turn_on
      data:
        entity_id: "{{ main_switch }}"

    - wait_for_trigger:
        - platform: state
          data_template:
            entity_id: '{{ main_switch }}'
            to: 'on'
      timeout:
        seconds: 30
      continue_on_timeout: true

    - delay: 00:00:10

    - service: switch.turn_off
      data:
        entity_id: '{{ main_switch }}'

    - wait_for_trigger:
        - platform: state
          data_template:
            entity_id: '{{ main_switch }}'
            to: 'off'
      timeout:
        seconds: 30
      continue_on_timeout: true

    - service: >-
        input_boolean.{{ mode }}
      entity_id: input_boolean.irrigation_state

Not sure if you can template anything in the wait_for_trigger, but try this:

    - wait_for_trigger:
        - platform: state
          entity_id: "{{ alternate_switch }}"
          to: 'off'

data/data_template is not an option for wait_for_trigger.

Triggers use value_template instead of data_template. Try that.

1 Like

Tried that with no luck, but thanks :slight_smile:

Thanks for the very prompt response, it worked!

2 Likes

I’m trying this exact thing and getting an error message saying that “value_template” is not a valid key: Message malformed: extra keys not allowed @ data['value_template']

Here’s the script:

alias: test
variables:
  main_entity: media_player.office
sequence:
  - wait_for_trigger:
      - platform: state
        value_template:
          entity_id: '{{ main_entity }}'
          to: 'playing'
mode: single

Looking at source code, value_template doesn’t appear to be defined for triggers of type state (GitHub: triggers/state.py), but it does seem to exist for numeric_state (GitHub: triggers/numeric_state.py).

So, how DID you manage to get that working? :slight_smile:

EDIT an hour later: I managed to get the logic working using platform: template instead of platform: state

alias: test
variables:
  main_entity: media_player.office
sequence:
  - wait_for_trigger:
      - platform: template
        value_template: '{{ is_state(main_entity, "playing") }}'
mode: single
2 Likes