Using action selector output in script

Hi, I am creating a script with a field that uses an action selector, like so:

fields:
  my_action:
    description: The action to run
    selector:
      action:

Later, I want to use the output of this field as the content of a sequence array. Like so:

sequence:
  - alias: Check condition
    choose:
      conditions:
        - condition: template
          value_template: "{{ my_condition }}"
      sequence: "{{ my_action }}"

However, I get a validation error from the editor when I try to do this. What is the proper way to use this action selector output as the value of a sequence array? Thanks!

2 Likes

Same issue here. What exactly is the purpose of action selectors? Are they only used for blueprints?

Later edit, as I know this question will pop up: what is the use case?

I have an automation blueprint which takes in a climate component and a binary sensor (window sensor) and checks whether the window being open should trigger any automatic action. The action to be taken is defined at the automation level (as a blueprint input).

blueprint:
  name: Take default action based on window openings
  description: bla
  domain: automation
  input:
    hvac:
      name: HVAC thermostat
      description: The climate component which controls the HVAC in the room/area
      selector:
        entity:
          domain: climate
    window_sensor:
      name: Window/door sensor
      description: The window or door sensor to be monitored
      selector:
        entity:
          domain:
            - binary_sensor
            - input_boolean
    turn_off_actions:
      name: Turn down HVAC sequence
      description: The action(s) to be taken if cooling/heating needs to be turned down/off in case of an open door/window
      selector:
        action:
    turn_on_actions:
      name: Turn HVAC back up sequence
      description: The action(s) that need to be taken once the disruptive condition is over (i.e. door/window is closed again)
      selector:
        action:

mode: single

variables:
  hvac: !input hvac
  window_sensor: !input window_sensor

trigger:
  - platform: state
    entity_id: !input window_sensor
    to: "on"
    id: "window_open"
  - platform: state
    entity_id: !input window_sensor
    to: "off"
    id: "window_closed"

condition:
  - condition: state
    entity_id: !input hvac
    state:
      - heat
      - cool
action:
  - alias: "Act depending on window state"
    choose:
      - conditions:
          - alias: "If window is open"
            condition: state
            entity_id: !input window_sensor
            state: "on"
        sequence:
          - service: script.notify_me_window
            data:
              hvac: "{{hvac}}"
              window_sensor: "{{window_sensor}}"
              action_to_take: "{{turn_off_actions}}"
              action_if_cancelled: "{{turn_on_actions}}"

      - conditions:
          - alias: "If window is closed"
            condition: state
            entity_id: !input window_sensor
            state: "off"
        sequence:
          - service: script.notify_me_window
            data:
              hvac: "{{hvac}}"
              window_sensor: "{{window_sensor}}"
              action_to_take: "{{turn_on_actions}}"
              action_if_cancelled: "{{turn_off_actions}}"

Then, since I want to 1. do the same thing in all rooms with a climate/window sensor combo and 2. share the blueprint, I’d like to have it call a script which does pretty much the same thing: take action and notify me about whether I want to revert the said action.

However, the script should have the notifications follow the same pattern, but the action(s) to be taken vary between rooms. As a consequence, I want to define the actions in the calling automation blueprint instance and use the actions as a part of what actions the script should do.

notify_me_window:
  alias: Notify me about HVAC action
  description: Notify that action was taken and offer the possibility to revert said action
  fields:
    action_to_take:
      name: Action to take by default
      description: This action will be taken by default when the notification is issued
      selector:
        action:
      default: []
    action_if_cancelled:
      name: Action if cancelled
      description: This action will be taken if the user decides to undo the automatic modification via notification UI
      selector:
        action:
      default: []
  variables:
    initial_actions: >
      {{ action_to_take + [{'service': 'notify.mobile_app_my_phone', 'data': {'actions': [{'action': 'CANCEL_HVAC_ACTION', 'title': 'Undo'}]}}] }}
  sequence: "{{initial_actions}}"
  mode: single

The clicking on “Cancel” part is not handled in the script, but that’s not really important at this stage. What’s more important is how to inject some user-defined actions in between some static actions of a script, be it for the entire script or as in @outdoorsgeek 's example, in a choose branch, for example.

2 Likes

Following, trying to do the same thing. I agree, I don’t see the point of the action selector if it seems you can’t execute the action…

Same here, Doesnt work on scripts.
Anyone got this working?

I see some Blueprint examples working fine.

input:
    actions:
      name: Actions
      description: ...
      selector:
        action: {}
- choose: []

  default: !input actions

Found this work around here.
I works with some limitations, but it works

If I’m reading that right, it sounds like you can’t use ANY action you want, you have to hard code all executable actions in the “runner” script and simply populate their parameters. Right?