WeSee
WeSee

Reputation: 3762

NodeRED: Configuration lost after modifying flows.json

I have a working flow using MQTT publisher and subscribe nodes and one broker configuration. Everything works.

Before starting up NodeRED, I have to change the MQTT broker ip and port with environment variables MQTT_BROKER_IP and MQTT_BROKER_PORT.

To do that I use

sed -i -e "s/^\([[:blank:]]*\"broker\":[[:blank:]]*\)\"[^\"]*\",/\1\"$MQTT_BROKER_IP\",/g" /data/flows.json
sed -i -e "s/^\([[:blank:]]*\"port\":[[:blank:]]*\)\"[^\"]*\",/\1\"$MQTT_BROKER_PORT\",/g" /data/flows.json

The substitutions works.

Now, the problem is, that when NodeRED starts up there is an error in the logfile:

[info] Starting flows
[error] [mqtt out:2b1fba1a.efa24e] missing broker configuration
[error] [mqtt in:58c7bd6b.0e8664] missing broker configuration

When I go to the NodeRED Editor, open the MQTT nodes, save them without modifying anything and deploy the flow, everything works and the MQTT nodes get a green "connected" light.

Why does this happen? How do I have to modify flows.json outside NodeRED correctly?

Upvotes: 1

Views: 2051

Answers (1)

hardillb
hardillb

Reputation: 59668

The problem is that the json field broker is used by multiple entries in the flow for different things. e.g.

The mqtt-broker config node that you are trying to edit:

{
    "id": "c2ba2a60.80ae38",
    "type": "mqtt-broker",
    "z": "",
    "name": "",
    "broker": "192.168.1.116",
    "port": "1883",
    "clientid": "",
    "usetls": false,
    "compatmode": false,
    "keepalive": "60",
    "cleansession": true,
    "birthTopic": "",
    "birthQos": "0",
    "birthPayload": "",
    "closeTopic": "",
    "closeQos": "0",
    "closePayload": "",
    "willTopic": "",
    "willQos": "0",
    "willPayload": ""
  }

And the mqtt-in node:

{
  "id": "98d0e95b.c28c2",
  "type": "mqtt in",
  "z": "a4de5120.cd3f58",
  "name": "",
  "topic": "#",
  "qos": "0",
  "datatype": "auto",
  "broker": "c2ba2a60.80ae38",
  "x": 190,
  "y": 80,
  "wires": [
    []
  ]
}

As you can see the when you run your sed script it will also change broker the entry in the mqtt-in node that points to the mqtt-broker.

The open/save probably works because you only have one mqtt-broker defined so it will default to be selected in the list so fixes that entry when you deploy.

If you want to do this sort of thing you will need something more complex that is aware of the type entry in each JSON entry it is updating.

Upvotes: 1

Related Questions