# Repeat loop not working properly. Controlling a 240v Petrol Generator

**URL:** <https://discourse.nodered.org/t/repeat-loop-not-working-properly-controlling-a-240v-petrol-generator/23097>\
**Category:** General\
**Created:** [14 March 2020 12:50 UTC](https://discourse.nodered.org/t/repeat-loop-not-working-properly-controlling-a-240v-petrol-generator/23097 "2020-03-14T12:50:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [14 March 2020 12:50 UTC](https://discourse.nodered.org/t/repeat-loop-not-working-properly-controlling-a-240v-petrol-generator/23097/1 "2020-03-14T12:50:35Z")

</div>

Hello!

Scenario.  
I have a petrol power generator triggered with two switches (for security).

Flow:  
Inject 'on' \> Switch 1 turn on Switch 2 turn on \> delay \> Switch 1 and 2 turn off \> check if genny is running via sensor \> Then repeat three times till Generator sensor present on or abort.

Flow works great till I add the function node with repeat (The generic repeat node isnt working either, so uninstalled).

The genny start process is flawless.

If genny doesnt start, the loop starts. But goes nuts with multiple retry attempts and gets stuck requiring a re-deploy of Node red to stop it.

It seems like multiple messages are cannoning around the flow and multiplying faster than the delay times in the flow, making me think the delay nodes arent delaying all messages and the repeat function isnt successfully stopping too.

Heres the flow:

`[{"id":"567b337a.8d2014","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"25f690e2.cb1fc","type":"api-call-service","z":"567b337a.8d2014","name":"Start key 2","server":"8ab15128.bb899","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.genny_on_engine_wifinode","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1230,"y":180,"wires":[["dc52a12e.b010d"]]},{"id":"12b563f4.61b884","type":"api-call-service","z":"567b337a.8d2014","name":"Turn key 2 off","server":"8ab15128.bb899","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.genny_on_engine_wifinode","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1800,"y":180,"wires":[["592244c2.95a22c"]]},{"id":"5b15a2b0.cfb16c","type":"api-call-service","z":"567b337a.8d2014","name":"Turn key 1 off ","server":"8ab15128.bb899","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.genny_on","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1620,"y":180,"wires":[["12b563f4.61b884"]]},{"id":"3f098d38.785a22","type":"api-current-state","z":"567b337a.8d2014","name":"Genny state","server":"8ab15128.bb899","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"binary_sensor.generator_state_engine_wifinode","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":2170,"y":180,"wires":[["897b3ac9.8428f8"]]},{"id":"897b3ac9.8428f8","type":"switch","z":"567b337a.8d2014","name":"on or off","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":2320,"y":180,"wires":[["1c8969ec.9cb09e"],["a6295300.b82b28"]]},{"id":"b539da7e.a6b6a","type":"api-current-state","z":"567b337a.8d2014","name":"Wireless node state","server":"8ab15128.bb899","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"binary_sensor.engine_node_status","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":670,"y":180,"wires":[["8f0b5716.39e288","875b4087.07d1e"]]},{"id":"875b4087.07d1e","type":"ha-wait-until","z":"567b337a.8d2014","name":"","server":"8ab15128.bb899","outputs":1,"entityId":"binary_sensor.engine_node_status","property":"state","comparator":"is","value":"on","valueType":"str","timeout":0,"timeoutUnits":"seconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":880,"y":180,"wires":[["b5aad4bb.3783e"]]},{"id":"8f0b5716.39e288","type":"switch","z":"567b337a.8d2014","name":"on or off","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":880,"y":140,"wires":[[],[]]},{"id":"8e410831.3f1f6","type":"do","z":"567b337a.8d2014","name":"Genny start","tasks":["Start Genny","Transfer power"],"outputs":3,"x":450,"y":200,"wires":[["b539da7e.a6b6a"],[],[]]},{"id":"1c8969ec.9cb09e","type":"do-return","z":"567b337a.8d2014","name":"","mode":"done","x":2510,"y":180,"wires":[]},{"id":"b5aad4bb.3783e","type":"api-call-service","z":"567b337a.8d2014","name":"Start key 1","server":"8ab15128.bb899","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.genny_on","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1070,"y":180,"wires":[["25f690e2.cb1fc"]]},{"id":"d4d402d8.d4d4c8","type":"do-return","z":"567b337a.8d2014","name":"Repeat","mode":"continue","x":2680,"y":200,"wires":[]},{"id":"dc52a12e.b010d","type":"delay","z":"567b337a.8d2014","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1420,"y":180,"wires":[["5b15a2b0.cfb16c"]]},{"id":"592244c2.95a22c","type":"delay","z":"567b337a.8d2014","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":2000,"y":180,"wires":[["3f098d38.785a22"]]},{"id":"22d549e2.e19b6e","type":"debug","z":"567b337a.8d2014","name":"Repeat new","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":2790,"y":480,"wires":[]},{"id":"a6295300.b82b28","type":"function","z":"567b337a.8d2014","name":"Repeat 3","func":"var count = context.get('count') || 0;\nvar msg2 = {};\n\nif (count < 3) {\n count++;\n msg2 = null;\n} else {\n count = 0;\n msg2 = msg;\n msg = null;\n}\n\ncontext.set('count', count);\n\nreturn [msg, msg2];","outputs":2,"noerr":0,"x":2480,"y":220,"wires":[["d4d402d8.d4d4c8"],["d3532676.4bcd28"]]},{"id":"d3532676.4bcd28","type":"do-return","z":"567b337a.8d2014","name":"Aborted","mode":"abort","x":2680,"y":240,"wires":[]},{"id":"22c0acfc.03476c","type":"inject","z":"567b337a.8d2014","name":"","topic":"","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":200,"wires":[["8e410831.3f1f6"]]},{"id":"8ab15128.bb899","type":"server","z":"","name":"Home Assistant"}]`

Any tips?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 March 2020 13:49 UTC](https://discourse.nodered.org/t/repeat-loop-not-working-properly-controlling-a-240v-petrol-generator/23097/2 "2020-03-14T13:49:56Z")

</div>

I can't try it as I don't have any of the non-standard nodes installed, but is it possible that any of the nodes along the way generate more than one message?

Other than that my approach would be to add some named debug nodes at appropriate points and configure them to output to syslog. Then change the delays to a large value such as 30 seconds to make it easier to see what is happening. Then run it and after it goes nuts analyse the log to work out what went wrong.

---

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [14 March 2020 14:57 UTC](https://discourse.nodered.org/t/repeat-loop-not-working-properly-controlling-a-240v-petrol-generator/23097/3 "2020-03-14T14:57:00Z")

</div>

Found the issue. Plonker @ my end,

One of the two genny start switches was used to start the flow! (not shown on the sample flow on this post).

I made a virtual switch on a spare pin on a wemos D1 and referenced that in node red to start the flow process... No more double messages.

Anyone know if there is a way to create another type of virtual flow trigger switch in node red? Triggerable from home assistant?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 March 2020 15:07 UTC](https://discourse.nodered.org/t/repeat-loop-not-working-properly-controlling-a-240v-petrol-generator/23097/4 "2020-03-14T15:07:58Z")

</div>

> [@Mobile\_NR\_Env](#):
>
> Anyone know if there is a way to create another type of virtual flow trigger switch in node red?

Can you explain in more detail what you mean by that?

---

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [14 March 2020 15:19 UTC](https://discourse.nodered.org/t/repeat-loop-not-working-properly-controlling-a-240v-petrol-generator/23097/5 "2020-03-14T15:19:54Z")

</div>

So, to trigger a flow in node red, I check the event state of a HA switch. When the state changes, it starts the flow.

This means each flow of the type above use up a switch.

Is there a trigger inside node red flow (to start it) ie: a virtual switch which can be seen and triggered from inside home assistant.  
Rather than using a HA switch...

Hope that makes a bit more sense?!

---

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [14 March 2020 15:24 UTC](https://discourse.nodered.org/t/repeat-loop-not-working-properly-controlling-a-240v-petrol-generator/23097/6 "2020-03-14T15:24:41Z")

</div>

Suppose I could create a MQTT Switch and topic in HA ....

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 March 2020 15:33 UTC](https://discourse.nodered.org/t/repeat-loop-not-working-properly-controlling-a-240v-petrol-generator/23097/7 "2020-03-14T15:33:19Z")

</div>

I don't use Home Assistant, but if you already use MQTT then yes, publish to a topic and subscribe to that in node-red.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [28 March 2020 15:33 UTC](https://discourse.nodered.org/t/repeat-loop-not-working-properly-controlling-a-240v-petrol-generator/23097/8 "2020-03-28T15:33:26Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
