Save state and return to it unless

Hi, I am trying to achieve the following with a “group.water_group” entity. That group is a combination in hass of a pump and a valve. I further have 3 sprinklers (valves) (which have noting to do with that group, that group is another valve and pump). When I toggle a sprinkler I want node red to check if the group is on, if not turn it on. And then if the sprinkler is done (turned off) I want the group to return to the previous state as it was before (on or off).

To be sure: the group.water_group entity has nothing to do with the sprinkler entities. The sprinkler(s) need to “control” and check the state of the group.water_group.

I read:
https://zachowj.github.io/node-red-contrib-home-assistant-websocket/cookbook/saving-and-restoring-states.html#using-the-get-entities-node

And I created:

The flow:

[{"id":"cc9c5380.dc22a8","type":"server-state-changed","z":"3732525b.226766","name":"Sprinkler Links aan","server":"c6b350c5.b8b3c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"switch.tuin_achter_sprinklers_links","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":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":150,"y":2040,"wires":[["bdd3eefd.467d1","19e48427.ded77c"],["af88277b.b5973"]]},{"id":"4492c513.5d2c3c","type":"server-state-changed","z":"3732525b.226766","name":"Sprinkler Rechts aan","server":"c6b350c5.b8b3c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"switch.tuin_achter_sprinklers_rechts","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":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":160,"y":2100,"wires":[["bdd3eefd.467d1","19e48427.ded77c"],["af88277b.b5973"]]},{"id":"cdb974a0.43cc","type":"server-state-changed","z":"3732525b.226766","name":"Sprinkler Voor aan","server":"c6b350c5.b8b3c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"switch.tuin_voor_sprinklers","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":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":150,"y":2160,"wires":[["bdd3eefd.467d1","19e48427.ded77c"],["af88277b.b5973"]]},{"id":"bdd3eefd.467d1","type":"api-current-state","z":"3732525b.226766","name":"Water uit?","server":"c6b350c5.b8b3c","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"group.water_group","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":570,"y":2040,"wires":[["3c10728c.226a9e"],[]]},{"id":"2deaec4f.e5e30c","type":"api-call-service","z":"3732525b.226766","name":"Zet Water Group aan","server":"c6b350c5.b8b3c","version":1,"debugenabled":false,"service_domain":"homeassistant","service":"turn_on","entityId":"group.water_group","data":"","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":940,"y":2040,"wires":[[]]},{"id":"19e48427.ded77c","type":"ha-get-entities","z":"3732525b.226766","server":"c6b350c5.b8b3c","name":"Water Group","rules":[{"property":"entity_id","logic":"is","value":"group.water_group","valueType":"str"},{"property":"state","logic":"includes","value":"on,off","valueType":"str"}],"output_type":"split","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":570,"y":2100,"wires":[["ffbeb9a7.745ae8"]]},{"id":"ffbeb9a7.745ae8","type":"function","z":"3732525b.226766","name":"transform data","func":"const payload = {};\npayload.domain = msg.payload.entity_id.split('.')[0];\npayload.service = `turn_${msg.payload.state}`;\npayload.data = { entity_id: msg.payload.entity_id };\n\n// create a blank message object with out new payload\nmsg = { payload: payload };\nreturn msg;","outputs":1,"noerr":0,"x":748,"y":2100,"wires":[["d31e4eb4.89c96"]]},{"id":"d31e4eb4.89c96","type":"join","z":"3732525b.226766","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":926,"y":2100,"wires":[["78a462fb.f74724"]]},{"id":"e1fb09f1.c7c218","type":"debug","z":"3732525b.226766","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1298,"y":2100,"wires":[]},{"id":"78a462fb.f74724","type":"change","z":"3732525b.226766","name":"","rules":[{"t":"set","p":"savedStates","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1104,"y":2100,"wires":[["e1fb09f1.c7c218"]]},{"id":"3c10728c.226a9e","type":"delay","z":"3732525b.226766","name":"","pauseType":"delay","timeout":"50","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":743,"y":2040,"wires":[["2deaec4f.e5e30c"]]},{"id":"af88277b.b5973","type":"change","z":"3732525b.226766","name":"Get Saved States","rules":[{"t":"set","p":"payload","pt":"msg","to":"savedStates","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":2160,"wires":[["dc9034bb.9986e"]]},{"id":"dc9034bb.9986e","type":"split","z":"3732525b.226766","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":758,"y":2160,"wires":[["766225df.a06afc"]]},{"id":"766225df.a06afc","type":"api-call-service","z":"3732525b.226766","name":"Restore State","server":"c6b350c5.b8b3c","version":1,"debugenabled":false,"service_domain":"homeassistant","service":"turn_{{payload}}","entityId":"group.water_group","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":916,"y":2160,"wires":[[]]},{"id":"c6b350c5.b8b3c","type":"server","name":"Home Assistant"}]

I tried multiple things to change etc. (not in the flow pasted) but I cannot find a way to get it work the way I want. What would be wrong and what do I need to change?

Thank you!

does the group consist of sprinkler valves or some other valve? if so, it looks too me as feedback loop.

Also what does ‘previous state’ mean for returning valve? Previous state is stored at time of turning a sprinkler on? so you need to turn off the group if no sprinkler is requested to be on?

Think of the group as an entity (group.water_group).

If the group entity is on, all is fine, the sprinkler entity can be turned on of off.

If the group entity is off it needs to be turned on when the sprinkler entity is turned on and must be turned off when turned off.

but ha groups do that automatically. At least one entity turned on in a group makes the group on. Otherwise group is off.

How NR fits this scenario?

Thank you for your reply, but this has nothing to do with my qeustion/problem.

I just cannot understand from OO what you are going to achieve. So trying to gather more info.

There is no more info. Please read carefully. All is really in there. If you read my flow you can see what I want. I also edited the 1st post to be more clear than it already was.

[{"id":"d3ab1c89.7298a","type":"server-state-changed","z":"3596e818.12bb08","name":"Sprinklers","server":"9405c3fe.d0a6c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"switch.tuin_achter_sprinklers_links, switch.tuin_achter_sprinklers_rechts, switch.tuin_voor_sprinklers","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":700,"y":440,"wires":[["72eac422.418bcc"],["62d035bd.f595cc"]]},{"id":"72eac422.418bcc","type":"api-current-state","z":"3596e818.12bb08","name":"Group state","server":"9405c3fe.d0a6c","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"group.water_group","state_type":"str","state_location":"currentState","override_payload":"msg","entity_location":"","override_data":"none","blockInputOverrides":true,"x":870,"y":400,"wires":[["27719e22.59fec2"]]},{"id":"27719e22.59fec2","type":"change","z":"3596e818.12bb08","name":"save state","rules":[{"t":"set","p":"previousState","pt":"flow","to":"currentState","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1040,"y":400,"wires":[["7a2bea2.37e4c14"]]},{"id":"7a2bea2.37e4c14","type":"switch","z":"3596e818.12bb08","name":"is group off?","property":"currentState","propertyType":"msg","rules":[{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1210,"y":400,"wires":[["bdbecc04.0a88e"]]},{"id":"bdbecc04.0a88e","type":"delay","z":"3596e818.12bb08","name":"","pauseType":"delay","timeout":"50","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1380,"y":400,"wires":[["f58ece74.7a49f"]]},{"id":"f58ece74.7a49f","type":"api-call-service","z":"3596e818.12bb08","name":"Zet Water Group aan","server":"c6b350c5.b8b3c","version":1,"debugenabled":false,"service_domain":"homeassistant","service":"turn_on","entityId":"group.water_group","data":"","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1577,"y":400,"wires":[[]]},{"id":"62d035bd.f595cc","type":"api-call-service","z":"3596e818.12bb08","name":"Restore State","server":"c6b350c5.b8b3c","version":1,"debugenabled":false,"service_domain":"homeassistant","service":"turn_{{flow.previousState}}","entityId":"group.water_group","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":880,"y":460,"wires":[[]]},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","addon":true},{"id":"c6b350c5.b8b3c","type":"server","name":"Home Assistant"}]
1 Like

Thanks! Will try and let know

no problem

btw, next time you need to post a flow, put three straight accent marks like this one ` at the top and bottom of your code. This way you won’t post a wall :slight_smile:

It works! Thank you so much! also very valuable information to use for other flows. Thanks!

ps. I change the code wall :-).

I cannot mark the solution, it wont let me in the forum.

1 Like