# Stop the flow, if there's no more mqtt-topics coming in

**URL:** <https://discourse.nodered.org/t/stop-the-flow-if-theres-no-more-mqtt-topics-coming-in/76013>\
**Category:** General\
**Tags:** mqtt\
**Created:** [2 March 2023 18:08 UTC](https://discourse.nodered.org/t/stop-the-flow-if-theres-no-more-mqtt-topics-coming-in/76013 "2023-03-02T18:08:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![binderth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/binderth/32/35124_2.png) [@binderth](https://discourse.nodered.org/u/binderth)\
**Post date:** [2 March 2023 18:08 UTC](https://discourse.nodered.org/t/stop-the-flow-if-theres-no-more-mqtt-topics-coming-in/76013/1 "2023-03-02T18:08:48Z")

</div>

So, I've got a cyclic "keep-alive" flow, which keeps sending flow-variables to a specific endpoint.  
That's ok, but I'd like the flow to stop, if there's no more mqtt-topic coming in.

Presently I'm subscribing to a bunch/of/topics/# and keep sending their payloads to an API endpoint. If there's no more topics coming in, this should stop. How do I keep track, if there's no more "new" topics coming in? I've set up an "cronplus" node, which starts every 2mins, it should stop, if there's no mqtt topic within that 2 mins. I could add an "timestamp"-variable to the flow-variables and ask for that, but is there a more elegant way?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [3 March 2023 06:06 UTC](https://discourse.nodered.org/t/stop-the-flow-if-theres-no-more-mqtt-topics-coming-in/76013/2 "2023-03-03T06:06:10Z")

</div>

Not knowing what your flow loos like how about this.

1)When the flow starts save a flow context variable called 'last\_mqtt\_topic' with the value 'start'.  
2) when a mqtt msg arrives have a switch compare flow.last\_msg\_topic with msg.topic.  
3) If they are different, store msg.topic in flow.last\_mqtt\_topic and continue on with the flow.  
4) if they are the same don't do anything

all done

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [3 March 2023 07:38 UTC](https://discourse.nodered.org/t/stop-the-flow-if-theres-no-more-mqtt-topics-coming-in/76013/3 "2023-03-03T07:38:05Z")

</div>

I was wondering why your flow is cyclic. Would it be better if the mqtt messages triggered the flow, then if no msg received the flow would not proceed.

---

<div class="post-metadata">

**Author:** ![binderth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/binderth/32/35124_2.png) [@binderth](https://discourse.nodered.org/u/binderth)\
**Post date:** [3 March 2023 08:49 UTC](https://discourse.nodered.org/t/stop-the-flow-if-theres-no-more-mqtt-topics-coming-in/76013/4 "2023-03-03T08:49:01Z")

</div>

> [@E1cid](#):
>
> Would it be better if the mqtt messages triggered the flow, then if no msg received the flow would not proceed.

Thing is, the goal is to control my PV inverter. In theory the inverter will fall back to automatic battery control via inverter logic if there's no command for two minutes. My openHAB EMS sends commands at least every two minutes. If everything works, the MQTT-commands will set the inverter to automatic and it will obey.  
but I'd like a fallback, if there's an issue with either mqtt or openHAB, so my node-red will then automatically set the inverter back to automatic battery control for itself.

So, what I'd like to do is

- if there's a constant flow of commands via MQTT: send them to the inverter
- if the flow stops: make sure, the inverter is set to automatic for sure

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [3 March 2023 10:36 UTC](https://discourse.nodered.org/t/stop-the-flow-if-theres-no-more-mqtt-topics-coming-in/76013/5 "2023-03-03T10:36:07Z")

</div>

> [@binderth](#):
>
> So, what I'd like to do is
> 
> - if there's a constant flow of commands via MQTT: send them to the inverter
> - if the flow stops: make sure, the inverter is set to automatic for sure

If I got your intension right: In such scenarios, I tend to use a trigger node:

- send nothing on incoming messages
- send the `set to automatic` message after 2 minutes
- extend the delay on (new) incoming messages

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/2/42d6bf4aa5b6165cb555594b7063d107ff0004e6.png)

This node will be quiet as long as a constant flow of messages is incoming ... but wakes up 2 mins after the inflow stopped.

---

<div class="post-metadata">

**Author:** ![binderth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/binderth/32/35124_2.png) [@binderth](https://discourse.nodered.org/u/binderth)\
**Post date:** [3 March 2023 10:47 UTC](https://discourse.nodered.org/t/stop-the-flow-if-theres-no-more-mqtt-topics-coming-in/76013/6 "2023-03-03T10:47:26Z")

</div>

> [@ralphwetzel](#):
>
> This node will be quiet as long as a constant flow of messages is incoming ... but wakes up 2 mins after the inflow stopped.

that's great and elegant!  
thanks!

---

<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:** [17 March 2023 10:47 UTC](https://discourse.nodered.org/t/stop-the-flow-if-theres-no-more-mqtt-topics-coming-in/76013/7 "2023-03-17T10:47:55Z")

</div>

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