Passing variable in automation

Hello,
I would like to use a variable in an automation from a script but not managing to do that. Could you give me some help?
Automation looks like this:

alias: Szivattyu viznyomas kisebb mint 1 1:30ra, szivattyu ujrainditas 3x
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.water_pressure_meter_pool_pump_pressure
    below: 1
    for:
      hours: 0
      minutes: 1
      seconds: 30
condition: []
action:
  - action: script.kor_1_4_kikapcsolas
    metadata: {}
    data: {}
    response_variable: active_zone
  - action: script.szivattyu_ujrainditas_3x
    metadata: {}
    data: {}
  - if:
      - condition: state
        entity_id: script.kor_1_4_kikapcsolas
        attribute: "1"
        state: "1"
    then:
      - type: turn_on
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: a97746eaddf6d27f6ade786cc302d7a3
        domain: switch
  - if:
      - condition: state
        entity_id: script.kor_1_4_kikapcsolas
        attribute: "2"
        state: "2"
    then:
      - type: turn_on
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: c8fd52b4715aa0bd05c834e20e200723
        domain: switch
mode: single

Script is like this:

alias: Kör 1-4 kikapcsolás active_zone változó visszadása
sequence:
  - if:
      - condition: device
        type: is_on
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: a97746eaddf6d27f6ade786cc302d7a3
        domain: switch
    then:
      - type: turn_off
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: a97746eaddf6d27f6ade786cc302d7a3
        domain: switch
      - variables:
          active_zone: 1
  - if:
      - condition: device
        type: is_on
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: c8fd52b4715aa0bd05c834e20e200723
        domain: switch
    then:
      - type: turn_off
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: c8fd52b4715aa0bd05c834e20e200723
        domain: switch
      - variables:
          active_zone: 2
description: ""

Thank you.

Variables have scope local to the script. If you want a script to return a variable you need to use the stop action with the response_variable option. See this portion of the docs.

Thank you. Appreciate the help.
I modified the automation and the script, but still no success.

Automation

alias: Test automation
description: ""
trigger: []
condition: []
action:
  - device_id: bbfc3144e6c5b8a7dc857d666c4c7d50
    domain: mobile_app
    type: notify
    message: Test
  - action: script.teszt_szkript_valtozo_visszaadasa
    data: {}
    response_variable: active_zone
  - device_id: bbfc3144e6c5b8a7dc857d666c4c7d50
    domain: mobile_app
    type: notify
    message: Response variable "{{ active_zone }}"
mode: single

Script

alias: Teszt szkript valtozo visszaadasa
sequence:
  - if:
      - condition: device
        type: is_on
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: a97746eaddf6d27f6ade786cc302d7a3
        domain: switch
    then:
      - type: turn_off
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: a97746eaddf6d27f6ade786cc302d7a3
        domain: switch
      - stop: ""
        response_variable: "1"
  - if:
      - condition: device
        type: is_on
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: c8fd52b4715aa0bd05c834e20e200723
        domain: switch
    then:
      - type: turn_off
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: c8fd52b4715aa0bd05c834e20e200723
        domain: switch
       - stop: ""
        response_variable: "2"
  - if:
      - condition: device
        type: is_on
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: fe3e7b35e1018f2dd8bdc3941c8d9a82
        domain: switch
    then:
      - type: turn_off
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: fe3e7b35e1018f2dd8bdc3941c8d9a82
        domain: switch
      - stop: ""
        response_variable: "3"
  - if:
      - condition: device
        type: is_on
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: 98d70031404983a8b5692e6f556181db
        domain: switch
    then:
      - type: turn_off
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: 98d70031404983a8b5692e6f556181db
        domain: switch
      - stop: ""
        response_variable: "4"
description: ""

The response variable must be a mapping of key-value pairs. And in your script, the value you assign to response_variable is the name of the variable to return. Which needs to be defined elsewhere in the script.

In the script, here’s an example that should work for the the part of the first if statement:

    then:
      - type: turn_off
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: a97746eaddf6d27f6ade786cc302d7a3
        domain: switch
      - variables:
          response:
            value: 1
      - stop: ""
        response_variable: response

Than, in your automation, you need to access the correct key/value pair that is returned. So your message line would be:

message: Response variable {{ active_zone.value }}

Unrelated, but you’d also benefit from avoiding devices in your triggers and actions.

Changed the code, thanks for the assistance. Now mobile device push notification returns like this:

Automation

alias: Teszt automatizmus
description: ""
trigger:
condition:
action:

* device_id: bbfc3144e6c5b8a7dc857d666c4c7d50
domain: mobile_app
type: notify
message: Teszt szkript
* action: script.teszt_szkript_valtozo_visszaadasa
data: {}
response_variable: active_zone
* device_id: bbfc3144e6c5b8a7dc857d666c4c7d50
domain: mobile_app
type: notify
message: Response variable "{{ active_zone.value }}"
mode: single

Script

alias: Teszt szkript
sequence:
  - if:
      - condition: device
        type: is_on
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: a97746eaddf6d27f6ade786cc302d7a3
        domain: switch
    then:
      - type: turn_off
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: a97746eaddf6d27f6ade786cc302d7a3
        domain: switch
      - variables:
          response:
            value: 1
      - stop: ""
        response_variable: response
  - if:
      - condition: device
        type: is_on
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: c8fd52b4715aa0bd05c834e20e200723
        domain: switch
    then:
      - type: turn_off
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: c8fd52b4715aa0bd05c834e20e200723
        domain: switch
      - variables:
          response:
            value: 2
      - stop: ""
        response_variable: response
  - if:
      - condition: device
        type: is_on
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: fe3e7b35e1018f2dd8bdc3941c8d9a82
        domain: switch
    then:
      - type: turn_off
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: fe3e7b35e1018f2dd8bdc3941c8d9a82
        domain: switch
      - variables:
          response:
            value: 3
      - stop: ""
        response_variable: response
  - if:
      - condition: device
        type: is_on
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: 98d70031404983a8b5692e6f556181db
        domain: switch
    then:
      - type: turn_off
        device_id: a6913f8046ace6be5a67735bde157105
        entity_id: 98d70031404983a8b5692e6f556181db
        domain: switch
      - variables:
          response:
            value: 4
      - stop: ""
        response_variable: response
description: ""
fields: {}

Sorry for my noobness :slight_smile: I am really lack of programming skills

Sorry that was my fault. Remove the quotes around the template. They are not needed if the template comes after text.

message: Response variable {{ active_zone.value }}

I edited my response above to match.

No success yet :frowning: push notification returns now without quotes but not giving back the variable. However if statement in the script fulfills. Do you have any idea? Thank you in advance.

There has been a few posts on this issue in the past:

https://community.home-assistant.io/t/cant-get-response-variable-when-stop-is-in-nested-block-of-script/608982

https://community.home-assistant.io/t/define-a-different-response-variable-in-conditionals-doesnt-work/605475

Basically you need to perform the logic for the response variable outside of If/Thens or Choose actions.

https://community.home-assistant.io/t/has-anyone-succeeded-in-working-with-response-variables/618453/17

Just for clarification, what do you want the response to return when multiple switches are “on”?

Well I learned something today, but that seems like a bug that needs to be fixed. Or if it’s not a bug it needs to be documented that the stop action’s response_variable option can only be used at the first level of the automation or script. Which is unique to the stop action; other actions have no problem using locally scoped variables.

Here’s a test I put together to confirm. Running with the condition template set to true it will send a notification that the variable is set to if_statement however the stop action returns nothing. Changing the template to false will show the variable is set to main_level and the stop action correctly returns {"value": "main_level"}

alias: Test variable scope
description: ""
sequence:
  - variables:
      my_var:
        value: main_level
  - if:
      - "{{ true }}"
    then:
      - variables:
          my_var:
            value: if_statement
      - action: notify.persistent_notification
        data:
          message: In "if" statement my_var is {{my_var.value}}
      - stop: If
        response_variable: my_var
  - action: notify.persistent_notification
    data:
      message: At main level my_var is {{my_var.value}}
  - stop: Main
    response_variable: my_var
mode: single

I’ll file a bug report later this week and see where it goes.

Edit: 99352 opened a year ago and went stale and got closed 6 months ago.

It seems in general the stop action was a poorly-thought-out implementation but nobody seems to want to fix it. Other stale and closed issues: 116429 104218

You can try something like this in your script. This uses your original logic, which was to provide only the first matching switch in the order they were listed. This code could be compacted but I tried to separate out the variables into chunks so its more clear what each step does, and it should be easier to modify if you decide you want to take action on more than one switch at a time. They key thing here is that, as @Didgeridrew suggested, the logic is performed in a template and exists at the main level of the script. The stop action also appears at the main level of the script. So the response variable is correctly returned.

This script will return a response {'value': 0} if there is no matching switch that is in the ON state.

script:
  test_var_pass:
    alias: Test to see how to pass a variable
    sequence:
      - variables:
          entity_dict:
            switch.switch_one: 1
            switch.switch_two: 2
            switch.switch_three: 3
          entity_on_dict: >
            {% set ns = namespace(output_kvps=[(None, 0)]) %}
            {% for k, v in entity_dict.items() %}
              {% if states(k) == 'on' %}
                {% set ns.output_kvps = ns.output_kvps + [(k, v)] %}
                {% set ns.output_kvps = ns.output_kvps | rejectattr(1, 'eq', 0) | list %}
              {% endif %}
            {% endfor %}
            {{ dict.from_keys(ns.output_kvps) }}
          lowest_value_match_number: "{{ entity_on_dict.values() | min }}"
          lowest_value_match_entity: "{{ entity_on_dict.items() | selectattr(1, 'eq', lowest_value_match_number) | map(attribute=0) | first }}"
          response:
            value: "{{ lowest_value_match_number }}"
      - if:
          - condition: template
            value_template: "{{ lowest_value_match_number != 0 }}"
        then:
          - action: switch.turn_off
            target:
              entity_id: "{{ lowest_value_match_entity }}"
      - stop: Completed
        response_variable: response

Perhaps in the future there will be a bug fix where we don’t have to go through this effort and can use the stop action w/ response variable at indented levels in the code.

Thank you for your effort. I will give a try to merge it with my code.