Remember a state/value and recall it later

Hello dear community,

I am planning an automation for my window blinds. Each of my windows have blinds controlled by HA as well as a sensor to report if the window is closed or open.

New I like to do the following:
If I open the window (because I need some fresh air) and the blinds are lower than a defined level, the blinds shall roll up until this level is reached (maybe with hysteresis).
When I now close the window again, the blinds shall roll back down to their previous level.

In pseudocode, it would look like this:

If(window_1 gets opened)
{
    if(actual position is lower than x)
    {
        remember actual position x
        roll blinds up to x
    }
}

And the other way:

if(window_1 gets closed)
{
    recall previous position

    if(window had been lower before)
    {
        roll blinds back down until previous position
    }
}

The logic is clear, but how could this be solved in HA? How can I remember a state/value and recall it later?

Thanks a lot in advance!

Cheers

Hi,
We have ā€˜literallyā€™ just covered this in :- ; - )))

Storing and retrieving variables for use with lights, you should easily be able to adapt this to your needs
Cheers
Mutt

1 Like

Great, thank you! I would have wondered, Iā€™d been the first one who wanted to do such a thing :slight_smile: .
I will read through it and report back my results.

Thanks again!

EDIT:
ok, if I understand it correctly, you just take an ā€œinput_numberā€ component to store a value? Well, this isā€¦ a really smart idea! Maybe this is a just too obvious, so that I didnā€™t think about this by myself! In Germany we say ā€œYou donā€™t see the forest because of all the trees.ā€ :sweat_smile:

Ok, now I have implemented two automations: one for opening the cover to let in some fresh air and one to close it again after refreshment.
For this I added an ā€œinput_numberā€ for each window to store the value of the actual position before opening it.
I also added an ā€œinput_numberā€ component to adjust the position to where the covers should go when the window gets opened.
Now, Iā€™d highly appreciate, if someone could have a look at my ā€œcodeā€ if it is formatted correctly. Maybe I need conversions or something like that?

- id: '834611532639'
  alias: Open blinds when window gets opened
  trigger:
  - entity_id: binary_sensor.living_room
    from: open
    platform: state
    to: closed
  - entity_id: binary_sensor.dinning_room
    from: open
    platform: state
    to: closed
  - (many more window sensors here)
  condition:
  - condition: numeric state
    entity_id: cover.{{trigger.entity_id.split('.')[1]}}.position
    below: input_number.fresh_air.value
  action:
  - service: input_number.set_value
    data:
      entity_id: input_number.{{trigger.entity_id.split('.')[1]}}
      value: cover.{{trigger.entity_id.split('.')[1]}}.position
  - service: cover.set_cover_position
    data:
      entity_id: cover.{{trigger.entity_id.split('.')[1]}}
      position: input_number.fresh_air.value

- id: '834611531604'
  alias: Close Blinds when window gets closed
  trigger:
  - entity_id: binary_sensor.living_room
    from: open
    platform: state
    to: closed
  - entity_id: binary_sensor.dinning_room
    from: open
    platform: state
    to: closed
  - (many more window sensors here)
  condition:
  - condition: numeric state
    entity_id: input_number.{{trigger.entity_id.split('.')[1]}}
    below: cover.{{trigger.entity_id.split('.')[1]}}.position
  action:
  - service: cover.set_cover_position
    data:
      entity_id: cover.{{trigger.entity_id.split('.')[1]}}
      position: input_number.{{trigger.entity_id.split('.')[1]}}.value 
  - service: input_number.set_value
    data:
      entity_id: input_number.{{trigger.entity_id.split('.')[1]}}
      value: 0

Especially the parts where I copy the actual position into the input_number and back are where I am not that sure about the correct formattingā€¦

Thanks a lot!

Jojo,
Itā€™s a bit difficult to see how this works as Iā€™m not sure of the values and what represents your (preferred) up position and what a typical down position is (in relation to your typical input_number.fresh_air.value is).
Also your both your triggers go from open to closed, I would have thought one would be the other way
Can you give us complete entity names (typical of each window) ?
Finally, your first code :-

  - service: cover.set_cover_position
    data:
      entity_id: cover.{{trigger.entity_id.split('.')[1]}}
      position: input_number.fresh_air.value

Is using data: when I think it needs to use data_template:

I donā€™t have any covers to play with, so this is just my ā€˜uneducatedā€™ guess
Hope that helps
Mutt

@Mutt
thanks a ton, that you took some time to take a look at my config. You are completely right about the wrong parts in my config. But in the meantime, I was changing and actually fixing a lot of things and now it kind of works.

At the moment, I try to figure out, how to have my automations ā€œdynamicā€, so that there are multiple triggers (different window contacts) that trigger all the same automation, but only for that specific window, that was opened/closed. But this is another story (Dynamic Automation for multiple components?).

Thanks to you, I know now how to use input_number components as ā€œvariablesā€ to store values (which is essential for my automation system) :slight_smile: :+1: .

Jojo,
Iā€™m pleased that you now have something that works even if you want to improve upon it further.
Could you post the code as it now stands, that will help anyone else with a similar issue find your code and have a good starting point from which to work for them.
If you do manage to polish it up to a dynamic automation, then I would request that you again return to this thread and post the updated code for the benefit of the community
Oh ! I also noted the - id: ā€˜834611532639ā€™ so this ā€˜wasā€™ generated by the automation editor (nothing wrong with that, but you can tidy up afterwards AND note that this will purge your file of ALL comments - dangerous when you are troubleshooting later or worse still adapt ā€˜thisā€™ to ā€˜thatā€™ but you donā€™t understand/remember this ā€˜widgetā€™), you can, if it helps you (t certainly does for me), change that to - id: auto_blind2lvngrm_openwthwndw or something more memorable so that you will know what it refers to in the future when you come across a reference to it elsewhere in the system (all clues help) especially when your system grows ; - )
Finally, you can ā€˜likeā€™ a response that has helped you, this will make the thread more attractive and it helps your helpers too.
Cheers
Mutt

1 Like

@Mutt
alright, thanks for the hints!
It is correct that the automation editor does not generate ā€œbeautifulā€ code. I am thinking about to just not use it at all, because of its limitations and the fact, that it deletes comments, etc.

About the code: well, there is totally no problem to post it. I already did in the thread a refered to above. I just have not done it here, because the code has nothing to do with the original topic of this thread (ā€œhow to store values in a variableā€).
The initial problem has been solved here, thats why I marked you post as solution :slight_smile: .

Greetings

Jojo,
I almost agree but think about what you were searching for and when you didnā€™t find it, you asked a question with a title that summarised your issue.
The next person to the site with a similar question may well find this thread and not the other one. Cross linking and quoting is good for everyone ; - )
Have a great Christmas
Mutt

Thanks dude, you too!