Variables in if statements

HI All,

Can someone tell me if this will work?
I use variables to make the script more readable.

  variables:
    door_sensor: binary_sensor.neo_diningroomdoor_custom_sensor
    kay: switch.kay_learning
    adaptive: "{{ states('sensor.adaptive_lighting_color_temperature')| round(0) }}"
    media_player: media_player.woonkamer

Below in script use the variables in if statements, here a example part:

          {% if states('door_sensor') == 'on' and states('kay') == 'on' %} 100
          {% elif states('door_sensor') == 'on' and states('kay') == 'off' %} 0
          {% elif states('door_sensor') == 'off' and states('kay') == 'on' %} 100
          {% else %} 50
          {% endif %}

But im not sure if this is possble.
Here whole script (1 of 6 in total)

###########################################################################################
# SCENE FOR APPLE TV WHEN SUN IS BELOW HORIZON
###########################################################################################

- alias: "Scene - AppleTV"
  initial_state: "false"
  trigger:

    - platform: state
      entity_id: sun.sun

    - platform: state
      entity_id: sensor.harmony
      to: "AppleTV"

  variables:
    door_sensor: binary_sensor.neo_diningroomdoor_custom_sensor
    kay: switch.kay_learning
    adaptive: "{{ states('sensor.adaptive_lighting_color_temperature')| round(0) }}"
    media_player: media_player.woonkamer

  condition:
    - condition: state
      entity_id: sun.sun
      state: "below_horizon"

    - condition: state
      entity_id:
        - switch.scene_appletv
        - switch.scene_goodnight
      state: "off"

    - condition: state
      entity_id: binary_sensor.sensor_manual_family_home
      state: "on"

    - condition: state
      entity_id: sensor.harmony
      state: "AppleTV"

  action:
    - service: script.engine_say
      data:
        media_player: media_player.this_device
        call_scene_appletv: 1

    - service: input_number.set_value
      data:
        entity_id: input_number.fullykiosk_brightness_slider
        value: 50

    - service: switch.turn_on
      entity_id: switch.scene_appletv

    - service: switch.turn_off
      data:
        entity_id:
          - switch.scene_cooking
          - switch.scene_daytime
          - switch.scene_disco
          - switch.scene_evening
          - switch.scene_goodnight
          - switch.scene_kodi
          - switch.scene_sexy
          - switch.scene_sleeping
          - switch.scene_tv
          - switch.lux_low

    - service: light.turn_on
      data:
        entity_id: light.nanoleaf
        brightness_pct: 20
        effect: >-
          {% if states('sensor.holiday') == 'Christmas' %} Christmas
          {% elif states('sensor.holiday') == 'Halloween' %} Halloween
          {% elif states('sensor.holiday') == 'Kingsday' %} Kingsday
          {% elif states('sensor.holiday') == 'Birthday' %} Kingsday
          {% else %} Kay
          {% endif %}

    - service: homeassistant.turn_on
      data:
        entity_id: light.newkaku_01401eb6_a
        brightness_pct: >-
          {% if states('door_sensor') == 'on' and states('kay') == 'on' %} 100
          {% elif states('door_sensor') == 'on' and states('kay') == 'off' %} 0
          {% elif states('door_sensor') == 'off' and states('kay') == 'on' %} 100
          {% else %} 50
          {% endif %}

    - choose:
# CHRISTMAS
        - conditions: "{{ is_state('sensor.holiday', 'Christmas') }}"

          sequence:
            - service: switch.turn_on
              entity_id:
                - switch.zwave2mqtt_nodeid_79_switch # Bank
                - switch.zwave2mqtt_nodeid_73_switch # iMac

            - service: light.turn_on
              data:
                entity_id: light.livingroom_all
                color_name: "red"
                brightness_pct: "{{ 50 if is_state('media_player', 'playing') else 100 }}"

            - service: light.turn_on
              data:
                entity_id:
                  - light.livingroom_2
                  - light.livingroom_3
                color_name: "green"
                brightness_pct: "{{ 20 if is_state('media_player', 'playing') else 100 }}"

            - service: light.turn_on
              data:
                entity_id: light.dressoir_all
                color_name: "blue"
                brightness_pct: "{{ 0 if is_state('door_sensor', 'on') else 75 }}"

            - service: light.turn_on
              data:
                entity_id: light.dressoir_2
                color_temp: 153
                brightness_pct: "{{ 0 if is_state('door_sensor', 'on') else 75 }}"

            - service: light.turn_on
              data:
                entity_id: light.balcony
                color_name: "blue"
                brightness_pct: "{{ 20 if is_state('media_player', 'playing') else 100 }}"

# HALLOWEEN
        - conditions: "{{ is_state('sensor.holiday', 'Halloween') }}"

          sequence:
            - service: light.turn_on
              data:
                entity_id: light.livingroom_all
                color_name: "orange"
                brightness_pct: "{{ 50 if is_state('media_player', 'playing') else 100 }}"

            - service: light.turn_on
              data:
                entity_id: light.livingroom_window
                color_name: "orange"
                brightness_pct: "{{ 20 if is_state('media_player', 'playing') else 100 }}"

            - service: light.turn_on
              data:
                entity_id: light.dressoir_all
                color_name: "blue"
                brightness_pct: "{{ 0 if is_state('door_sensor', 'on') else 100 }}"

            - service: light.turn_on
              data:
                entity_id: light.dressoir_2
                color_name: "purple"
                brightness_pct: "{{ 0 if is_state('door_sensor', 'on') else 100 }}"

            - service: light.turn_on
              data:
                entity_id: light.balcony
                color_name: "blue"
                brightness_pct: "{{ 20 if is_state('media_player', 'playing') else 100 }}"

# KINGSDAY
        - conditions: "{{ is_state('sensor.holiday', 'Kingsday') }}"

          sequence:
            - service: light.turn_on
              data:
                entity_id: light.livingroom_all
                brightness_pct: "{{ 50 if is_state('media_player', 'playing') else 100 }}"
                kelvin: "{{ adaptive }}"

            - service: light.turn_on
              data:
                entity_id: light.livingroom_window
                brightness_pct: "{{ 20 if is_state('media_player', 'playing') else 100 }}"
                kelvin: "{{ adaptive }}"

            - service: light.turn_on
              data:
                entity_id: light.dressoir_1
                color_name: "red"
                brightness_pct: "{{ 0 if is_state('door_sensor', 'on') else 100 }}"

            - service: light.turn_on
              data:
                entity_id: light.dressoir_2
                color_temp: 153
                brightness_pct: "{{ 0 if is_state('door_sensor', 'on') else 100 }}"

            - service: light.turn_on
              data:
                entity_id: light.dressoir_3
                color_name: "blue"
                brightness_pct: "{{ 0 if is_state('door_sensor', 'on') else 100 }}"

            - service: light.turn_on
              data:
                entity_id: light.balcony
                color_name: "blue"
                brightness_pct: "{{ 20 if is_state('media_player', 'playing') else 100 }}"

# BITHRDAY
        - conditions: "{{ is_state('sensor.holiday', 'Birthday') }}"

          sequence:
            - service: light.turn_on
              data:
                entity_id: light.livingroom_all
                brightness_pct: "{{ 50 if is_state('media_player', 'playing') else 100 }}"
                kelvin: "{{ states.adaptive.state | round(0)}}"

            - service: light.turn_on
              data:
                entity_id: light.livingroom_window
                brightness_pct: "{{ 20 if is_state('media_player', 'playing') else 100 }}"
                kelvin: "{{ adaptive }}"

            - service: light.turn_on
              data:
                entity_id:
                  - light.dressoir_1
                  - light.dressoir_2
                  - light.dressoir_3
                effect: 0
                brightness_pct: "{{ 0 if is_state('door_sensor', 'on') else 100 }}"

            - service: light.turn_on
              data:
                entity_id: light.balcony
                color_name: "blue"
                brightness_pct: "{{ 20 if is_state('media_player', 'playing') else 100 }}"

# DEFAULT
      default:
        - service: light.turn_on
          data:
            entity_id: light.livingroom_all
            brightness_pct: "{{ 50 if is_state('media_player', 'playing') else 100 }}"
            kelvin: "{{ adaptive }}"

        - service: light.turn_on
          data:
            entity_id: light.livingroom_window
            brightness_pct: "{{ 20 if is_state('media_player', 'playing') else 100 }}"
            kelvin: "{{ adaptive }}"

        - service: light.turn_on
          data:
            entity_id: light.dressoir_all
            brightness_pct: >-
              {% if states('media_player') == 'playing' and states('door_sensor') == 'on' %}0
              {% elif states('media_player') == 'playing' and states('door_sensor') == 'off' %} 50
              {% elif states('media_player') == 'idle' and states('door_sensor') == 'on' %} 0
              {% elif states('media_player') == 'idle' and states('door_sensor') == 'off' %} 100
              {% else %} 100
              {% endif %}
            kelvin: "{{ adaptive }}"

        - service: light.turn_on
          data:
            entity_id: light.balcony
            color_name: "blue"
            brightness_pct: "{{ 20 if is_state('media_player', 'playing') else 100 }}"

What you want to do should be totally fine, however, you need to remove the quotes around the variables, otherwise it will think it’s a string.

Thank you! So it will become like this:

          {% if states(door_sensor) == 'on' and states(kay) == 'on' %} 100
          {% elif states(door_sensor) == 'on' and states(kay) == 'off' %} 0
          {% elif states(door_sensor) == 'off' and states(kay) == 'on' %} 100
          {% else %} 50
          {% endif %}

WIll change it and make my scripts more readable and shorter :smiley:

Exactly, you can simplify this.
When states(kay) is on it will always be 100, no matter if the states(door_sensor) is on or off. So you can remove states(door_sensor) == 'on' from the first line and remove the third line completely.

1 Like

Damn, indeed… was blind to see this. Thanks also !!! Will check some more of those combinations.

1 Like