# Dynamically set clientID for MQTT output

**URL:** <https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226>\
**Category:** General\
**Created:** [26 August 2021 18:57 UTC](https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226 "2021-08-26T18:57:03Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlesmodrich-tc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/charlesmodrich-tc/32/47036_2.png) [@charlesmodrich-tc](https://discourse.nodered.org/u/charlesmodrich-tc)\
**Post date:** [26 August 2021 18:57 UTC](https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226/1 "2021-08-26T18:57:03Z")

</div>

Hello,

I'm trying to work with a LoRaWAN gateway running Node-RED, however am running into a bit of an issue.

What I need to do is for each payload received by the gateway, strip out the serial number from the data it sends, and use this serial number to send a 1-off MQTT message, using the serial number as a clientID for the MQTT connection, then close the connection. This process would then be repeated programmatically for each LoRaWAN node that calls in.

Currently, I can't find a good way to set clientIDs on the fly. Anyone know of a way to do this?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [26 August 2021 19:27 UTC](https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226/2 "2021-08-26T19:27:42Z")

</div>

There is a PR for [dynamic mqtt control](https://github.com/node-red/node-red/pull/2989) waiting review. It can do this (and more).

If you are desperate, I could tell you how to modify your installation SRC to make it work otherwise hang tight and keep fingers crossed for node-red 2.1.

---

<div class="post-metadata">

**Author:** ![charlesmodrich-tc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/charlesmodrich-tc/32/47036_2.png) [@charlesmodrich-tc](https://discourse.nodered.org/u/charlesmodrich-tc)\
**Post date:** [26 August 2021 19:44 UTC](https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226/3 "2021-08-26T19:44:47Z")

</div>

Approaching desperation, definitely. If you have a workaround that would be fantastic!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [26 August 2021 19:46 UTC](https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226/4 "2021-08-26T19:46:03Z")

</div>

Ok. But first. How is your node-red installed, what version, what hardware and what OS? Do you have access to filesystem?

---

<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:** [26 August 2021 19:50 UTC](https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226/5 "2021-08-26T19:50:39Z")

</div>

Can you explain why you want to do this? It seems very unusual.

---

<div class="post-metadata">

**Author:** ![charlesmodrich-tc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/charlesmodrich-tc/32/47036_2.png) [@charlesmodrich-tc](https://discourse.nodered.org/u/charlesmodrich-tc)\
**Post date:** [26 August 2021 20:57 UTC](https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226/6 "2021-08-26T20:57:40Z")

</div>

@Colin I need to do this because this is how my company's IoT architecture is setup on the backend, it's out of my control I'm afraid.

@Steve-Mcl Working with v0.15.3, it came pre-installed on the gateway (MultiTech Conduit AP MTCAP-LNA3), OS appears to be arm-mlinux, and I have ssh and file system access.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [26 August 2021 21:39 UTC](https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226/7 "2021-08-26T21:39:26Z")

</div>

> [@charlesmodrich-tc](#):
>
> Working with v0.15.3

Unfortunately I _think_ that is too old. The underlying MQTT library has had several full major updates since then.

Does that box have an updated firmware with a newer node js & node-red or is it possible to upgrade it?

Failing that, you could install node-red on a server or raspberry pi?

---

<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:** [27 August 2021 06:12 UTC](https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226/8 "2021-08-27T06:12:57Z")

</div>

> [@charlesmodrich-tc](#):
>
> I need to do this because this is how my company's IoT architecture is setup

Out of interest, do you understand why? At the moment I have difficulty seeing a use case. Unless it is for some security related issue I suppose.

---

<div class="post-metadata">

**Author:** ![charlesmodrich-tc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/charlesmodrich-tc/32/47036_2.png) [@charlesmodrich-tc](https://discourse.nodered.org/u/charlesmodrich-tc)\
**Post date:** [27 August 2021 17:43 UTC](https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226/9 "2021-08-27T17:43:37Z")

</div>

@Steve-Mcl Unsure if I can upgrade it, I'll look into it, but would like to at least attempt with the current version before opening that can of worms

@Colin Security is definitely one reason, in any case, it's outside of my hands to change it, just have to roll with the punches for now.

---

<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:** [28 August 2021 15:26 UTC](https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226/10 "2021-08-28T15:26:41Z")

</div>

> [@charlesmodrich-tc](#):
>
> Security is definitely one reason

Ok, in that case don't tell them that you are connecting using the Id of a different device.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [29 August 2021 04:28 UTC](https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226/11 "2021-08-29T04:28:21Z")

</div>

While waiting for the new version, a simple work-around would be to use a python script. Call it from NR using the exec node with necessary params where one is the serial number that you then use in the script as clientID

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 August 2021 07:17 UTC](https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226/12 "2021-08-29T07:17:12Z")

</div>

> [@krambriw](#):
>
> While waiting for the new version

Unfortunately, the node version on that box is `0.x` (I looked at the firmware info) so it is doubtful it will ever work.

> [@krambriw](#):
>
> a simple work-around would be to use a python script

Assuming python is installed on that box & the OP can install an MQTT lib.

  

An alternative (also assuming box already has mosquitto\_pub or OP can install mosquitto client tools) is to `exec` the `mosquitto_pub` command specifying the client id

Demo...  
 ![JWJjKe5Yok](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/4/a420e9d469d22ecbcf848137851eba73fa30bc49.gif)

Check on broker ...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/b/3b5a2a3cd55fbbc4904a17f79df15ff6afb6484a.png)

---

<div class="post-metadata">

**Author:** ![charlesmodrich-tc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/charlesmodrich-tc/32/47036_2.png) [@charlesmodrich-tc](https://discourse.nodered.org/u/charlesmodrich-tc)\
**Post date:** [30 August 2021 13:27 UTC](https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226/13 "2021-08-30T13:27:26Z")

</div>

Very interesting, thank you both for the recommendations and workarounds, they're much appreciated!

I'll see about working this way soon as the project is starting up tomorrow, I'll let you guys know how it goes. Thanks again!

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [30 August 2021 17:13 UTC](https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226/14 "2021-08-30T17:13:00Z")

</div>

This is a simple script I think should work (you have to change the ip to the broker) as a starting point. Maybe you also have to change the sensor value to a string before you send it

Create a file named "lorawan\_send.py" or similar and call it with the params as described below

```auto
#!/usr/bin/python
# USAGE
# python lorawan_send.py -cid mqtt_client_id -v value
# or
# python3 lorawan_send.py -cid mqtt_client_id -v value

import argparse
import paho.mqtt.client as mqtt

def on_publish(mosq, userdata, mid):
    mosq.disconnect()
    del mosq
    exit(0)

ap = argparse.ArgumentParser()
ap.add_argument(
    "-cid",
    "--cid", 
    required=True, 
    help="mqtt client id"
)
ap.add_argument(
    "-v",
    "--value", 
    required=True, 
    help="value from sensor"
)
args = vars(ap.parse_args())

client = mqtt.Mosquitto(client_id=args['cid'])
client.connect("127.0.0.1", 1883, 60)
client.on_publish = on_publish
client.publish("lorawan", args['value'], 1, False)
client.loop_forever()

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [30 August 2021 18:00 UTC](https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226/15 "2021-08-30T18:00:19Z")

</div>

If on Linux - then as @Steve-Mcl suggested - calling the command line program mosquitto\_pub may be simpler.

---

<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:** [29 October 2021 18:01 UTC](https://discourse.nodered.org/t/dynamically-set-clientid-for-mqtt-output/50226/16 "2021-10-29T18:01:19Z")

</div>

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