¿ªÔÆÌåÓý

Add custom items to mqtt topic #mqtt


 

When a point/turnout is changed in JMRI connected to the layout using MQTT it publishes a message whose topic is of the form ../turnouts/ T101 ?I have a number of arduino nodes controlling points and would like something like ../turnouts/node2/T101. so the node can subscribe only to topics that are relevant. I have worked out how to add and persist custom properties to the turnout object but how do I change the published MQTT topic to reflect these.?


 

On Apr 29, 2025, at 12:23?PM, p.lavers via groups.io <p.lavers@...> wrote:

When a point/turnout is changed in JMRI connected to the layout using MQTT it publishes a message whose topic is of the form ../turnouts/ T101 I have a number of arduino nodes controlling points and would like something like ../turnouts/node2/T101. so the node can subscribe only to topics that are relevant. I have worked out how to add and persist custom properties to the turnout object but how do I change the published MQTT topic to reflect these.
There¡¯s a script in jython/MQTT that might be a starting point for doing this: jython/MQTT/SetMqttPrefix.py



That lets you specify a specific prefix for all turnouts created after it¡¯s run. So you could make a version of this that provides a specific prefix for an Arduino, then creates that Arduino¡¯s turnouts. After that, you then run a version of the script that changes to a 2nd Arduino¡¯s prefix and creates more turnouts. Etc.



Bob

¡ª
Bob Jacobsen
rgj1927@...


 

Hi - you could just explicitly subscribe to the topics you want to subscribe to anyway - probably the quickest way to achieve it.
?
Something like
?
? ? ? client.subscribe("track/turnout/T101");
? ? ? client.subscribe("track/turnout/T102");

etc.
?
Or a loop that will insert the relevant ID on the end for you.
?
Chris


 

P -
?
I do exactly what you suggest. ?My turnouts are named to include the id of the microcontroller that is controlling them (in my case, ESP32s).? For example, I have ESP32 named 3Way01 controlling the two turnouts associated with a 3 way (numbered 01? and 02).? I created them with the following system names:
?
E32-3Way01/01
E32-3Way01/02
?
Note that the MC node is part of the turnout system name (as is the /).? JMRI will then send on topic as proscribed by my config, which follows the example in the JMRI MQTT documentation:
?
trains/jSend/to/E32-3Way01/01
trains/jSend/to/E32-3Way01/02
?
My ESP32 sketch subscribes to:
?
trains/jSend/to/E32-3Way01/#
?
Other ESP32s similarly subscribe to TOs attached to their id.
?
BTW, you might also check the MQTT group:? MQTT4MR.
?
Jerry
___________________________________
jerryg2003@...


 

Hi
?
That is a cool hack. I like the idea of including the / in the name! However it is still a work around. I guess there is no way of telling jmri to use a specific set of properties (including custom ones) to derive the MQTT topic. This of course does not just apply to turnouts but other 'named beans'!?