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

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?

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

1 Like

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.

1 Like

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

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

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

2 Likes

that's great and elegant!
thanks!

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