Periodically Adjust Roller Blind Positions

Hi! I’m a recent convert to Home Assistant (I’ve been struggling with various other home automation platforms for several years and have always been frustrated at the lack of customizability or insane work-arounds needed to accomplish seemingly simple things). I finally took the plunge to HASS and I have to say it’s a huge breath of fresh air!

I’m attempting to write my first automation that doesn’t just use built in functionality and I’m having trouble wrapping my head around a few things. I’m a software engineer by day (mostly c++, Swift, and Java) but I’m just starting with YAML, Jinja, and HASS in general.

I’m running Home Assistant 2023.12.1 on a Debian vm.

The problem: I have a bunch of fabric roller blinds that are automatically opened in the morning and closed at night. Each blind has a different preferred position based on several different factors (sun glare, privacy, etc). Most of them end up being opened between 40-60%. However, since they aren’t open all the way, the fabric tends to stretch over the roller and create lumps that are then visible when closed. I’m attempting to create an automation that adjusts the position by 5-10% every half hour.

It would be easy enough to create a couple automations for each blind that trigger on the hour/half hour and set the position to a hardcoded value. But my engineer brain can’t stand the idea of duplicating that many automations, so I’m attempting to create one automation that triggers every half hour, and loops through a group of blinds to adjust the position up or down a small amount. This should only apply to blinds that are already open and be an offset from the current position.

So far I have this automation:

alias: Blinds - Adjust Periodically
description: Adjust the blind positions every 30 minutes to prevent stretching.
trigger:
  - platform: time_pattern
    hours: "*"
    minutes: "0"
    seconds: "0"
  - platform: time_pattern
    hours: "*"
    minutes: "30"
    seconds: "0"
condition:
  - condition: state
    entity_id: cover.all_blinds
    state: open
action:
  - service: script.blinds_adjust
    data:
      amount: 5 #Figure out amount here
mode: single

And my script:

blinds_adjust:
    alias: "Adjusts Blinds"
    description: "Adjusts the position of all currently open blinds by the amount specified."
    fields:
        amount:
            name: "Amount"
            description: "The amount to adjust the blinds."
            example: "5"
            required: true
            default: 0
    sequence:
        repeat:
            for_each: >-
                {{ expand('cover.all_blinds') | selectattr('state', 'eq', 'open') | map(attribute='entity_id') | list }}
            sequence:
                service: cover.set_position
                #What to do here?

I’ve attempted to use a for_each loop in the script, but I’m run into a point where I don’t know how to get the current position of each blind so I can calculate the new adjusted position.

Am I on the right track, or is there a better approach to take?

Thanks!

I’ve made some progress on this. My script had a few syntax errors, and I also figured out how to use repeat.item to get the entity_id. Updated script:

    alias: "Adjusts Blinds"
    description: "Adjusts the position of all currently open blinds by the amount specified."
    fields:
        amount:
            name: "Amount"
            description: "The amount to adjust the blinds."
            example: "5"
            required: true
            default: 0
    sequence:
        repeat:
            for_each: >-
                {{ expand('cover.all_blinds') | selectattr('state', 'eq', 'open') | map(attribute='entity_id') | list }}
            sequence:
              - service: cover.set_cover_position
                data:
                    entity_id: "{{ repeat.item }}"
                    position: "{{ state_attr(repeat.item, 'current_position') + amount }}"

So now my question is, is there a way to alternate the blind adjustment value every other time the automation runs? Ideally, the first time the automation runs, it would move the blinds up 5%, then every run afterwards the position adjustment would alternate between -10% and 10% (ie, if the blind starts the day opened to 50%, it would alternate between 45-55%). Is there a way to determine how many times the automation has been run today, or a way to save state that can be reset each day?