Response_variable returned inside if statment fails

Below are two simple scripts

  1. Version A & B to return a value when called
  2. To receive a value and notify if true

You would expect when running script 2 that the conditional would return true and it would return a notify if either script 1.A or 1.B was used.

However it only seems to work when 1.A is used. 1.B fails to return any object.

Why? It has to be something to do with the stop being nested inside the if statement in 1.B but i can’t seem to work out why that would effect anything. I have checked the Variable docs page and i don’t think it is a scoping issue?

  • Core 2024.1.3
  • Supervisor 2023.12.1
  • Operating System 11.4
  • Frontend 20240104.0

1.A

alias: Test Return A Value
variables:
  testingvar:
    value: true
sequence:
  - stop: All Done
    response_variable: testingvar
mode: single

1.B

alias: Test Return A Value
variables:
  testingvar:
    value: true
sequence:
  - if:
      - condition: device
        type: is_off
        device_id: someidhere
        entity_id: someidhere
        domain: switch
    then:
      - stop: All Done
        response_variable: testingvar
    else:
      - stop: All Done
        response_variable: testingvar
mode: single

2

alias: Test - Receive A Value
sequence:
  - service: script.test_return_a_value
    data: {}
    response_variable: recivedvar
  - condition: template
    value_template: "{{recivedvar.value}}"
  - service: notify.mobile_app_a
    metadata: {}
    data:
      message: test
mode: single
1 Like

This is why:

https://www.home-assistant.io/docs/scripts#scope-of-variables

Hi Tom

Yes, i did think it was a scoping issue.

However if you check my code against the docs you linked you will see that I am not trying to update the variable inside the IF statement, I am only trying to reference the globally scoped variable.

As per the documentation here the variable should be available anywhere in the script to reference (not update). But it does not seem to be.

Would you mind taking a closer look and clarifying.

Cheers

1 Like

Just throwing this out here. I was running into a similar problem. Turns out that if I made the mode of the called script parallel instead of queued, it worked. (the code above is single, I didn’t try that)