How to set cover position via variable

So I have this automation, when I open the window, save current cover position to variable. When I close the window set current cover position to the value of the variable. But it’s not working. Can somebody hint me in the right direction? I really don’t get how variables in automations are working.

alias: Lüftungsfunktions
trigger:
  - platform: state
    entity_id:
      - sensor.remoteegterrassentur
    to: open
    id: terrasse
  - platform: state
    entity_id:
      - sensor.remoteegterrassentur
    to: closed
    id: terrasse_close
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - terrasse
        sequence:
            - variables:
              terrasse_position: >-
                {{ state_attr('cover.terrassentur_rollladen',
                'current_position') }}
          - service: cover.open_cover
            target:
              entity_id: cover.terrassentur_rollladen
      - conditions:
          - condition: trigger
            id:
              - terrasse_close
        sequence:
          - service: cover.set_cover_position
            data:
              position: "{{ states('terrasse_position') | int }}"
            target:
              entity_id: cover.terrassentur_rollladen
mode: single

Look into snapshot_entities. I was trying to do the same thing and got it working using that.
One of my automations:

- id: 'my-automation'
  trigger:
  - platform: tag
    tag_id: tktktk
  mode: single 
  action:
  - service: scene.create 
    data: 
      scene_id: state_revert #the name of my scene to save my original positions
      snapshot_entities: 
        - cover.my_damper_1

then call a service to set whatever cover positions you want

and then to revert back:

  - service: scene.turn_on  
    target: 
      entity_id: scene.state_revert

op post was months ago… but in case it helps for others who run across it…

the core issue is that when you create the variable, the scope of the variable is just the current context… and for that specific run of the automation.

so terrasse_position that you defined held its value for only the scope of that sequence (not the conditional and sequence below where you used it), and also it was essentially nuked when the open trigger finished running that sequence.

when the automation got called the next time for close, the value of that variable was gone.
here a link that may help you understand variable scoping better:

the solution is either as @jorel said, to use a scene and snapshot. or in this case since it’s just a single number… and if you want to see it and be able to change it yourself, create a helper number, and save the position to that helper number.

that helper number is effectively a global variable… that you can read and write from anywhere (which certianly has it’s pros and cons!)

also, becareful about your indentation. it looks off to me… not sure if that was just a copying issue, or if your actual automation code is indented as you have it posted… but as posted, it’s invalid…

1 Like