I’m trying to return dynamic responses from a script based on conditions within the script itself. The below code is not what I’m actually trying to implement but simplified proof of concept script. When I run this script I do not receive a result. Is something like this possible? Am I doing something wrong?
while @Didgeridrew is totally right and is the most common mistake in using variables,
in this particular case, in this example, op might be hitting somethign else because they aren’t tryin to change the variable and also they are trying to use it within the same block…
I was able to achieve it with the following. But my real script would be more complicated and I was hoping I could use more gui blocks instead of so much hand coded stuff.
But the example in this link shows setting a variable inside the then, which I am doing, and then consuming it in the service, which I would imagine is the same as the Stop.
I’m not trying to pass anything from inside an if, choose, or repeat to outside, I’m still consuming it from within.
Yeah, seems like an issue with response_variable as this example works.
alias: Example
sequence:
- variables:
people: 1
- if:
- condition: state
entity_id: device_tracker.wobani
state: home
then:
- variables:
persons: "{{ people + 1 }}"
bob: fred
- service: notify.notify
data:
message: There are {{ persons }} people home named {{ bob }}
- service: notify.notify
data:
message: There are {{ people }} people home
description: ""
I’ll see about opening a feature request. Thanks for your help.
Similar questions were asked in the past and the answer is the same, it’s a scoping restriction. response_variable cannot transport the local variable’s value out of the sequence where the local variable was defined.
The script example in the documentation is different because the local variable’s value is consumed within the sequence where it’s defined. You’re trying to export that value (via response_variable) out of the scope where it’s defined.