Flow process help

I am fairly new to node-red, but not to coding.

I am hoping for a bit of guidance to flow layout. I am happy with how to use node-red, the different nodes etc. What I am missing is experience with algorithms for node-red.

I have two devices that communicate with an MQTT server.

The flow needs to:

  1. Listen out for a message from an MQTT server topic.
  2. check the message content (an integer [1-100]) against a list of numbers and strings ([[0,"a"], [4,"b"], [3,"re"], [6."we"]])
  3. if the message matches the current position in the array, send an MQTT message to the second device and progress the place in the array
  4. continue until the array has been fully traversed.

The part I am having issues with are holding the "state" of the process. Is using context variables and pretending this is normal code the way forward? or is there a "node-red" way of doing this?

Thanks for any suggestions

Hello @Morsey and welcome to the forum.

  1. Listen out for a message from an MQTT server topic.
    An mqtt-in node does this.

  2. check the message content (an integer [1-100]) against a list of numbers and strings ([[0,"a"], [4,"b"], [3,"re"], [6."we"]])
    The message content is an integer? So what does [6."we"] mean?

  3. if the message matches the current position in the array, send an MQTT message to the second device and progress the place in the array
    What does "message matches the current position in the array" mean?
    What does "progress the place in the array" mean?

  4. continue until the array has been fully traversed.
    Are you saying that the message, an integer, might somehow "match" both [3, "re"] and [6."we"]?

Maybe you can show us a real example of an incoming message and this occult array of arrays. What output do you expect to get?

There probably is, yes.
It's generally a mistake to think in terms of variables and procedural code when moving to Node-red.
If you can explain more clearly what the requirement is, we might be able to suggest a Node-red way to achieve it.

Thanks do the advice, It is an escape room prop.

Device one:

Essentially a sensor. It sends an MQTT message with a number. The number represents a location on a map.

Device two:

Receives an MQTT message telling it what to show. The message it shows is to tell the user where to go next on the map.

Node red’s job:

Receive message from sensor via MQTT
Check if it is the first step in a defined map “path”. If so send a message to device two with next step message. (North, South,
East, West)
Repeat for each step in the path
If the next location sent from the map is wrong, redo from start

Is that clearer?

Sorry, just noticed your inline replies.

The messages are all written by me. So message from device one is current a json containing one bit of info:

{location:1}

The array is a bit I am unsure of, it needs to be definable, and changeable. In essence is a list of location number (1,4,2,3,…) but each location has a paired “where to go next” location string “north”, “east” etc.

As long as the location sequence is followed then the location string is sent onwards. Currently just a json:

{direction: “north”}

This might be if interest:

1 Like