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.
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
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…