Get state from HA within while loop

Hi, I'm trying to utilize the long-press button feature on my Ikea remote. I created a flow with a while-loop. The while condition is the state of the button: brightness_up_hold. It enters the while loop correctly, but when I release the button, the while-loop keeps running and never breaks. even though the output of the get state in the loop != brightness_up_hold.

Can anyone explain how this works? I saw somewhere that node-red is single threaded? That might be a problem when in a while loop?

Don't create loops.

How do you know that the button was released ?

The current_state: sensor.ikea_remote_action is what detects the button release. The debug shape at the bottom shows the new button state in the debug window, but loop continues...

You advise not to use loops, but how would I then utilise the long-press feature to dim a light until I release the button?

If you get a message that tells you that the button was released you can use a trigger node.

@bakman2 I don't know how a trigger node would help here, but I figured it out. I am setting the state of the switch outside the loop to a flow variable. That works as intended.
Thanks for your suggestions!

A trigger node triggers.
Reset the trigger once you receive a message that you are no longer holding the button.

Example flow:

[{"id":"e8abaa49.62c76","type":"inject","z":"4c118a8e.23250c","name":"","topic":"","payload":"button down","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":286,"y":312,"wires":[["d92b830f.9b8df"]]},{"id":"54f6603d.996b9","type":"inject","z":"4c118a8e.23250c","name":"","topic":"","payload":"button release","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":286,"y":360,"wires":[["d92b830f.9b8df"]]},{"id":"d92b830f.9b8df","type":"trigger","z":"4c118a8e.23250c","op1":"","op2":"0","op1type":"pay","op2type":"str","duration":"-1","extend":false,"units":"s","reset":"button release","bytopic":"all","name":"","x":488,"y":336,"wires":[["2f45441.d96c13c"]]},{"id":"2f45441.d96c13c","type":"debug","z":"4c118a8e.23250c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":694,"y":336,"wires":[]}]

I had to create a function to extract the payload correctly from the remote, but your example works great.
No loop needed indeed :slight_smile: Thank you for your time. Just posting result for you and others who might have a similar question.

[{"id":"3c13f15c.b29646","type":"server-state-changed","z":"749b5fa6.356358","name":"Long press","server":"7f685d33.d72984","version":1,"entityidfilter":"sensor.ikea_remote_action","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":100,"y":420,"wires":[["ada2d3e.9019d3"]]},{"id":"ada2d3e.9019d3","type":"function","z":"749b5fa6.356358","name":"set flow state","func":"if(msg.payload !== \"\")\n    flow.set(\"state\",msg.payload);\n    \nmsg.payload = flow.get(\"state\");\nreturn msg;","outputs":1,"noerr":0,"x":150,"y":540,"wires":[["6f86aa71.5d916c"]]},{"id":"4e5f8212.0dbba4","type":"api-call-service","z":"749b5fa6.356358","name":"feller","server":"7f685d33.d72984","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.woonkamer","data":"{\"brightness\": msg.data.attributes.brightness + 25}","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":990,"y":500,"wires":[[]]},{"id":"61cdba6.c09e044","type":"api-current-state","z":"749b5fa6.356358","name":"brightness","server":"7f685d33.d72984","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.woonkamer","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":830,"y":500,"wires":[["4e5f8212.0dbba4"]]},{"id":"8729e772.e2a088","type":"api-current-state","z":"749b5fa6.356358","name":"brightness","server":"7f685d33.d72984","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.woonkamer","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":830,"y":580,"wires":[["dedb3a7c.cc0eb8"]]},{"id":"dedb3a7c.cc0eb8","type":"api-call-service","z":"749b5fa6.356358","name":"zwakker","server":"7f685d33.d72984","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.woonkamer","data":"{\"brightness\": msg.data.attributes.brightness - 25}","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1000,"y":580,"wires":[[]]},{"id":"8710bd20.e488e8","type":"trigger","z":"749b5fa6.356358","op1":"state","op2":"0","op1type":"flow","op2type":"str","duration":"-500","extend":false,"units":"ms","reset":"brightness_up_release","bytopic":"all","name":"","x":600,"y":500,"wires":[["61cdba6.c09e044"]]},{"id":"6f86aa71.5d916c","type":"switch","z":"749b5fa6.356358","name":"Hold commands","property":"state","propertyType":"flow","rules":[{"t":"regex","v":"brightness_up_(hold|release)","vt":"str","case":false},{"t":"regex","v":"brightness_down_(hold|release)","vt":"str","case":false}],"checkall":"true","repair":false,"outputs":2,"x":350,"y":540,"wires":[["8710bd20.e488e8"],["714ad213.e4ccbc"]]},{"id":"714ad213.e4ccbc","type":"trigger","z":"749b5fa6.356358","op1":"state","op2":"0","op1type":"flow","op2type":"str","duration":"-500","extend":false,"units":"ms","reset":"brightness_down_release","bytopic":"all","name":"","x":600,"y":580,"wires":[["8729e772.e2a088"]]},{"id":"7f685d33.d72984","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
1 Like

Goed om te horen dat het werkt :wink: