Help - Restore a previous climate state

hi, I have a small problem, I have a couple of automations that make me deactivate and reactivate my Netatmo when opening/closing windows. Now the point I’m banging my head on is this: I would like the Netatmo state to go back to its previous state when closing the windows (at the moment it goes back to preset_mode). E.g. now if its in manual mode and i close windows it goes back to schedule mode, while I would like it to go back to manual

[{"id":"66eab432.7ea53c","type":"server-state-changed","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"Gruppo finestre aperta","server":"706d0d82.9d7084","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.sensori_finestre","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"2","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":140,"y":680,"wires":[["a05acc5.9eeda3"],[]]},{"id":"a05acc5.9eeda3","type":"api-call-service","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"Imposta Away","server":"706d0d82.9d7084","version":5,"debugenabled":false,"domain":"climate","service":"set_preset_mode","areaId":[],"deviceId":[],"entityId":["climate.sala"],"data":"{\"preset_mode\":\"away\"}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":480,"y":660,"wires":[[]]},{"id":"a84138fc.8ba168","type":"api-call-service","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"Imposta Schedule","server":"706d0d82.9d7084","version":5,"debugenabled":false,"domain":"climate","service":"set_preset_mode","areaId":[],"deviceId":[],"entityId":["climate.sala"],"data":"{\"preset_mode\":\"Schedule\"}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":570,"y":740,"wires":[[]]},{"id":"4e818fd6.9d077","type":"server-state-changed","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"Gruppo finestre chiuse","server":"706d0d82.9d7084","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.sensori_finestre","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"off","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"1","forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":140,"y":780,"wires":[["a84138fc.8ba168"],[]]},{"id":"706d0d82.9d7084","type":"server","name":"Home Assistant Riccardo","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

Ok thanks to the help outside the forum I managed to find a solution, still not satisfactory, but it’s a starting point:

[{"id":"1d3465ab.5b7b3a","type":"group","z":"6488ba9d.59b954","name":"","style":{"fill":"#c8e7a7","label":true},"nodes":["66eab432.7ea53c","a05acc5.9eeda3","4e818fd6.9d077","a84138fc.8ba168","454fa934.d89368","10bd6fe2ab4107f5","3790fc30.12b354","36705576.17a42a","2b7e0299.1ce7ae","b3db19bf.61d8e8","a5a0930.ed3ae7","6247c7cf.5cb858","719b7b39.cbf0e4","6082d5e25c2db228","0ab4aa14f366a61c"],"x":14,"y":579,"w":1052,"h":242},{"id":"66eab432.7ea53c","type":"server-state-changed","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"Gruppo finestre aperta","server":"706d0d82.9d7084","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.sensori_finestre","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"2","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":140,"y":680,"wires":[["3790fc30.12b354"],[]]},{"id":"a05acc5.9eeda3","type":"api-call-service","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"Imposta Away","server":"706d0d82.9d7084","version":5,"debugenabled":false,"domain":"climate","service":"set_preset_mode","areaId":[],"deviceId":[],"entityId":["climate.sala"],"data":"{\"preset_mode\":\"away\"}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":960,"y":660,"wires":[[]]},{"id":"4e818fd6.9d077","type":"server-state-changed","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"Gruppo finestre chiuse","server":"706d0d82.9d7084","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.sensori_finestre","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"off","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"1","forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":140,"y":780,"wires":[["a5a0930.ed3ae7"],[]]},{"id":"a84138fc.8ba168","type":"api-call-service","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"Imposta Schedule","server":"706d0d82.9d7084","version":5,"debugenabled":false,"domain":"climate","service":"set_preset_mode","areaId":[],"deviceId":[],"entityId":["climate.sala"],"data":"{\"preset_mode\":\"Schedule\"}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":950,"y":620,"wires":[[]]},{"id":"454fa934.d89368","type":"comment","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"Automazione Finestre/Termostato","info":"","x":170,"y":620,"wires":[]},{"id":"10bd6fe2ab4107f5","type":"delay","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":800,"y":660,"wires":[["a05acc5.9eeda3"]]},{"id":"3790fc30.12b354","type":"ha-get-entities","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"Get Climate","server":"706d0d82.9d7084","version":0,"rules":[{"property":"entity_id","logic":"is","value":"climate.sala","valueType":"str"}],"output_type":"split","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":330,"y":720,"wires":[["36705576.17a42a"]]},{"id":"36705576.17a42a","type":"function","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"transform data","func":"const payloads = [];\nconst payload = {};\npayload.domain = 'climate';\npayload.service = `set_temperature`;\npayload.data = {\n    entity_id: msg.payload.entity_id,\n    temperature: msg.payload.attributes.temperature,\n    target_temp_low: msg.payload.attributes.min_temp,\n    target_temp_high: msg.payload.attributes.max_temp\n};\n\n// create a blank message object with out new payload\nmsg = { payload: payload };\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":500,"y":720,"wires":[["2b7e0299.1ce7ae"]]},{"id":"2b7e0299.1ce7ae","type":"join","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"5","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":450,"y":660,"wires":[["b3db19bf.61d8e8"]]},{"id":"b3db19bf.61d8e8","type":"change","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"","rules":[{"t":"set","p":"savedStates","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":660,"wires":[["10bd6fe2ab4107f5"]]},{"id":"a5a0930.ed3ae7","type":"change","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"Get Saved States","rules":[{"t":"set","p":"payload","pt":"msg","to":"savedStates","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":780,"wires":[["6247c7cf.5cb858"]]},{"id":"6247c7cf.5cb858","type":"split","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":530,"y":780,"wires":[["0ab4aa14f366a61c"]]},{"id":"719b7b39.cbf0e4","type":"api-call-service","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"Restore State","server":"706d0d82.9d7084","version":5,"debugenabled":false,"domain":"climate","service":"set_temperature","areaId":[],"deviceId":[],"entityId":["climate.sala"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"data"}],"queue":"none","output_location":"payload","output_location_type":"msg","x":940,"y":740,"wires":[[]]},{"id":"6082d5e25c2db228","type":"api-call-service","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"Restore State","server":"706d0d82.9d7084","version":5,"debugenabled":false,"domain":"climate","service":"set_preset_mode","areaId":[],"deviceId":[],"entityId":["climate.sala"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"data"}],"queue":"none","output_location":"payload","output_location_type":"msg","x":780,"y":780,"wires":[[]]},{"id":"0ab4aa14f366a61c","type":"delay","z":"6488ba9d.59b954","g":"1d3465ab.5b7b3a","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":760,"y":740,"wires":[["719b7b39.cbf0e4"]]},{"id":"706d0d82.9d7084","type":"server","name":"Home Assistant Riccardo","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]