Night time brightness, store all lights as variables, automation

I´m trying to write a few automations (might have been better to do this as a script in retrospect) and i think I have the logistics of it down but it does not seem to like the value in the first automation for some reason, probably missing something apparent that I have been staring myself blind on past hour.

Someone that has a good idea on the code below?

# Automation to save the current brightness of all lights in the Living Room area
- id: save_brightness
  alias: Save Brightness
  trigger:
    - platform: time
      at: '22:00:00'
  action:
    - service: variable.set_variable
      data_template:
        variable: saved_brightness
        value:
          {% for state in states.light %}
          {% if state.attributes.area in ["Living Room", "Bedroom"] %}
          - light: '{{ state.entity_id }}'
            brightness: '{{ state.attributes.brightness }}'
          {% endif %}
          {% endfor %}

# Automation to adjust the brightness of all lights in the Living Room area during the time range
- id: adjust_brightness
  alias: Adjust Brightness
  trigger:
    - platform: time
      at: '22:00:05'
  action:
    - service: light.turn_on
      data_template:
        entity_id: '{{ item.light }}'
        brightness_pct: '{{ (item.brightness * 0.5)|int }}'
      loop: '{{ saved_brightness }}'

# Automation to return the brightness of all lights in the Living Room area to their saved state
- id: restore_brightness
  alias: Restore Brightness
  trigger:
    - platform: time
      at: '06:00:00'
  action:
    - service: light.turn_on
      data_template:
        entity_id: '{{ item.light }}'
        brightness: '{{ item.brightness }}'
      loop: '{{ saved_brightness }}'

What generated these automations?

Their triggers and service calls use options that don’t exist in the official documentation.


The light.turn_on service call doesn’t have a loop option.

The Time Condition doesn’t have a between option.

Hi Taras,

Completely forgot, i do have a integration that adds the set_variable as below, code is mostly written by hand in visualstudio so far and trying to get it moved into HA.

rogro82/hass-variables: Home Assistant variables component (github.com)

I recognize that variable.set_variable is part of a custom integration.

What about the other things I mentioned? If you added them, what led you to believe they are supported options?

BTW, the templates in the last two automations reference undefined variables.

To be fair i thought that it would support loops after reading some jinja examples, would it be possible to use for loops or are loops inside actions simply not supported?

I suggest you consider using the Automation Editor to create automations instead of composing them directly in YAML. There are so many fundamental errors in the examples you posted that it would be best if you use the Automation Editor to guide you through the process.