Struggling with Variables

Hi everyone,

I could use some help here. Depending on the weather, my front light bulb will blink different colors when I open the front door. I take a scene snapshot of the bulb so after I run the automation it returns to the previous color. It works well as long as the light is already one. When it is off, scene.create seems to capture it’s off state, but not the color. So when I use scene.turn_on it resets the bulb to off, but doesn’t return it to the default color. Next time I turn on the light it is the wrong color.

I tried adding a section that if the bulb is off, I first turn it on and set a variable light_is_off to ‘true’. This way scene can capture the color. When I end the automation, if light_is_off == ‘true’ it should turn off the bulb. Unfortunately, I can’t get the variable / condition correct. Any help would be appreciated.

In addition, in release 9.3 (2023.9: New climate entity dialogs, lots of tile features, and template sensors from the UI! - Home Assistant) there is a new weather forecast service. I can’t figure out if I need to change anything.

Here is my automation. All help is appreciated!!!

alias: LR Flash Bulbs based on Upcoming Rain when Door Opens
description: >-
  If it is raining blink 6 times; if it will rain in the next 4 hours blink 3
  times.  Only works if that light is already on.  - Fix that
trigger:
  - type: opened
    platform: device
    device_id: 52f08ff90a1eb74c1c027c88f2bd4590
    entity_id: binary_sensor.front_door_sensor
    domain: binary_sensor
condition:
  - condition: template
    value_template: >-
      {% set warning_condition = ['rainy', 'lightning-rainy', 'pouring',
      'snowy-rainy', 'lightning'] %}

      {% set primary_warning = states('weather.openweathermap') in
      warning_condition %}

      {% set ns = namespace(mid_forecast_warning = False, last_forecast_warning
      = False) %}

      {% set mid_forecast_to_check = 1 %}

      {% set last_forecast_to_check = 4 %}

      {% for forecast in range (0, mid_forecast_to_check + 1) %}
        {% set ns.mid_forecast_warning = ns.mid_forecast_warning or (state_attr('weather.openweathermap', 'forecast')[forecast].condition in warning_condition) %}
      {% endfor %}


      {% for forecast in range (mid_forecast_to_check + 1,
      last_forecast_to_check + 1) %}
        {% set ns.last_forecast_warning = ns.last_forecast_warning or (state_attr('weather.openweathermap', 'forecast')[forecast].condition in warning_condition) %}
      {% endfor %}


      {{ primary_warning or ns.mid_forecast_warning or ns.last_forecast_warning
      }}
action:
  - variables:
      light_is_off: "false"
  - if:
      - condition: device
        type: is_off
        device_id: 7334082729f4c7d2ee2fefbae5d4e675
        entity_id: a71611f4172d3c704068803e345856f7
        domain: light
    then:
      - variables:
          light_is_off: "true"
      - type: turn_on
        device_id: 7334082729f4c7d2ee2fefbae5d4e675
        entity_id: a71611f4172d3c704068803e345856f7
        domain: light
  - service: scene.create
    data:
      scene_id: before_alerting_weather
      snapshot_entities: light.living_room_foyer_bulb_right
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {% set warning_condition = ['rainy', 'lightning-rainy', 'pouring',
              'snowy-rainy', 'lightning'] %}

              {% set primary_warning = states('weather.openweathermap') in
              warning_condition %}

              {% set ns = namespace(mid_forecast_warning = False,
              last_forecast_warning = False) %}

              {% set mid_forecast_to_check = 1 %}

              {% set last_forecast_to_check = 4 %}

              {% for forecast in range (0, mid_forecast_to_check + 1) %}
                {% set ns.mid_forecast_warning = ns.mid_forecast_warning or (state_attr('weather.openweathermap', 'forecast')[forecast].condition in warning_condition) %}
              {% endfor %}


              {% for forecast in range (mid_forecast_to_check + 1,
              last_forecast_to_check + 1) %}
                {% set ns.last_forecast_warning = ns.last_forecast_warning or (state_attr('weather.openweathermap', 'forecast')[forecast].condition in warning_condition) %}
              {% endfor %}


              {{ primary_warning }}
        sequence:
          - repeat:
              count: "6"
              sequence:
                - service: light.turn_on
                  target:
                    entity_id: light.living_room_foyer_bulb_right
                  data:
                    color_name: blue
                    brightness_pct: 100
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 2
                    milliseconds: 0
                - service: light.turn_off
                  target:
                    entity_id: light.living_room_foyer_bulb_right
                  data:
                    transition: 1
      - conditions:
          - condition: template
            value_template: >-
              {% set warning_condition = ['rainy', 'lightning-rainy', 'pouring',
              'snowy-rainy', 'lightning'] %}

              {% set primary_warning = states('weather.openweathermap') in
              warning_condition %}

              {% set ns = namespace(mid_forecast_warning = False,
              last_forecast_warning = False) %}

              {% set mid_forecast_to_check = 1 %}

              {% set last_forecast_to_check = 4 %}

              {% for forecast in range (0, mid_forecast_to_check + 1) %}
                {% set ns.mid_forecast_warning = ns.mid_forecast_warning or (state_attr('weather.openweathermap', 'forecast')[forecast].condition in warning_condition) %}
              {% endfor %}


              {% for forecast in range (mid_forecast_to_check + 1,
              last_forecast_to_check + 1) %}
                {% set ns.last_forecast_warning = ns.last_forecast_warning or (state_attr('weather.openweathermap', 'forecast')[forecast].condition in warning_condition) %}
              {% endfor %}


              {{ ns.mid_forecast_warning or ns.last_forecast_warning }}
        sequence:
          - repeat:
              count: "3"
              sequence:
                - service: light.turn_on
                  target:
                    entity_id: light.living_room_foyer_bulb_right
                  data:
                    color_name: turquoise
                    brightness_pct: 100
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 2
                    milliseconds: 0
                - service: light.turn_off
                  target:
                    entity_id: light.living_room_foyer_bulb_right
                  data:
                    transition: 1
    default: []
  - service: scene.turn_on
    target:
      entity_id: scene.before_alerting_weather
    data: {}
  - if:
      - condition: template
        value_template: "{{ light_is_off == 'true' }}"
    then:
      - type: turn_off
        device_id: 7334082729f4c7d2ee2fefbae5d4e675
        entity_id: a71611f4172d3c704068803e345856f7
        domain: light
mode: single

Thank you!