Setting a variable in conditions, referenced in actions?

I’m creating an automation, but I want to create a variable in conditions (like, if today is Thursday, set x to 0), and reference it in the action (like, if x = 0, else …). Is this possible, or is it only possible to use variables in the action section?

You can use variables in template conditions but it will be limited to the scope of only that condition.

Logically speaking, I don’t think it makes sense to do this: Conditions (in the HA sense) should be just tests (basically, if statements that control the flow). Secondly, you might get very, very complicated code and weird error cases. If you have e.g. some conditions in an and block you have no guarantee each condition will be evaluated (the first false condition will make the whole wing false without further evaluation). That means, whatever state you set may not have executed when you get to the actions and you’ll need to put a lot of the same code there anyway.

1 Like

You also have variables that can be used by the whole automation/script. E.g.

variables:
  weekday: "{{ now().weekday() }}"
trigger:
....
condition:
  - "{{ weekday < 5 }}"
action:
....

But you’d rather use the variable in the action section directly together with choose.

Please show your current code and then we can help you.

Ok, here’s some sample code:

alias: test with vars
description: ''
mode: single
trigger:
  - platform: state
    entity_id: light.leg_lamp
    to: 'on'
condition: []
action:
  - service: light.turn_on
    data: {}
    entity_id: light.patio

This would turn on the patio light if the leg lamp comes on. What I’m trying to do is something like, if the leg lamp comes on, turn on the patio light green, else turn on the patio light white. I know there’s a way to do this without variables, but I’m specifically trying to figure out if I can change variables in the condition section, and leave them the same in the action section.

How should this work? The automation never gets triggered when the leg lamp is not turned on, so the else case will never fire.

You’re right. I’ve added a time trigger.

alias: test with vars
description: ''
mode: single
trigger:
  - platform: state
    entity_id: light.leg_lamp
    to: 'on'
  - platform: time
    at: '09:00'
condition: []
action:
  - service: light.turn_on
    data: {}
    entity_id: light.patio

And now how should this logically work?

The condition section determines whether the action should be run or not, it’s not meant to decide which action should be run, only if any action should be run.
In the action section you can then have differwnt actions based on conditions:

E.g.

action:
  - choose:
      - conditions:
          - "{{ is_state('light.leg_lamp', 'on') }}"
        sequence:
          - service call to turn on patio light green
    default: 
      - service call to turn on patio light white
1 Like

Looks like the answer is no.

I’m used to coding in PHP, where you could do something like,

if ( $x == 0 ) { $y = 1; } else { $y = 2; }

echo $y;

This makes variables editable conditionally, and the result line stays the same. I’m looking for a way to do this in HA.

Nvm, I see what you were doing there. That works, just not where I wanted the condition. Thanks.