#Variables in action IF condition

1 messages · Page 1 of 1 (latest)

willow pivot
#

I'm lost.. why this action always returns "test" instead of correct number?

actions:
  - variables:
      pump_delay: test
  - if:
      - condition: device
        type: is_off
        device_id: 64c63d02fdda4418cb94930b6800d54b
        entity_id: 8662ba0a9d438f1e5d26a0d216b7045a
        domain: switch
        for:
          hours: 1
          minutes: 0
          seconds: 0
    then:
      - variables:
          pump_delay: 3
    else:
      - variables:
          pump_delay: 1
  - action: persistent_notification.create
    metadata: {}
    data:
      message: "{{ pump_delay }}"

Is it not possible to pass variable out of if condition?

normal crypt
#

I think there are scope issues

#

I think maybe it can't be done.

marsh parrot
#

Right, you're declaring a variable in each branch and then not using it. It won't propagate to the higher scope

#

There isn't a good way to define or modify variables that you can use in later steps without doing it in the initial definition with a template

#

Which you could in this case

runic torrent
#

Am alternative solution (that's a bit jank) is just creating a helper to store the number
But yes, you need to use the variable within the scope it's defined in, so either template your if condition in the variable block, or move the notification within the if blocks

marsh parrot
#

The last bit would be the most straightforward

willow pivot