rogro82 / hass-variables

Home Assistant variables component

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Test variable in automation action condition

MartinAKu opened this issue · comments

Hi,

I try to do a conditional action within an automation based on the (integer) value of a variable. This is the code:
- alias: sensor2 initial_state: 'on' trigger: - platform: state entity_id: binary_sensor.stair2 action: - service: automation.turn_off entity_id: automation.sensor2 - service: automation.turn_on entity_id: automation.sensorTimeout - service: variable.set_variable data: variable: stair_state value_template: '{{ 3 if (variable.state | int) == 1 else 2 }}' - condition: template value_template: '{{ (states.variable.stair_state.state | int) == 3 }}' - service: script.turn_on entity_id: script.stair_up

I'm sure the variable stair_state = 3 but the script 'stair_up' isn't called. What can be wrong?

Sorry for the non-formatted code. Below the formatted code.

- alias: sensor2
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: binary_sensor.stair2
  action:
    - service: automation.turn_off
      entity_id: automation.sensor2
    - service: automation.turn_on
      entity_id: automation.sensorTimeout
    - service: variable.set_variable
      data:
        variable: stair_state
        value_template: '{{ 3 if (variable.state | int) == 1 else 2 }}'
    - condition: template
        value_template: '{{ (states.variable.stair_state.state | int) == 3 }}'
    - service: script.turn_on
      entity_id: script.stair_up

Does {{ 3 if (states.variable.stair_state.state | int) == 1 else 2 }} work?

Yes, this line works.