How to add payload to a posted MQTT topic?

I have a float OPCDA signal from PLC

I want add payload to a posted MQTT topic

Regards

May be you should post an example. What is the payload which comes out of your mqtt node and what should be added?

Preformatted text

[{"id":"b261f6b8f06331ab","type":"inject","z":"d2279fc8909d5ffb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Channel1.Device1.PMMTrkPyroStat1Temp","payload":"85","payloadType":"str","x":110,"y":60,"wires":[["8cbef9d81b8fdded","9b8b99ee95277bcc"]]}]

Preformatted text

I want add payload to a posted MQTT topic

This is your topic: "Channel1.Device1.PMMTrkPyroStat1Temp"
And this is your payload: "85"

If you add them together you get "Channel1.Device1.PMMTrkPyroStat1Temp85"

If the above is not what you are trying to achieve you will have to explain better.

This is what you posted:

Is it correct that you got a string as output (or is it a number ? ) and what you want to add? A number, a string, an object???

I see value client machine.

But i want get from opcclient like this address

[{"id":"Channel1.Device1.PMMTrkPyroStat1Temp","v":"Floating Value From OPCDA"}]

OK i am not sure if I understand correctly - but may be you meant this:

So use a change Node like this:

If you want that as JSON String put a JSON Node in as well:

1 Like

1 Like

What is the difference - using a function node - instead of change node???? - More complex is always possible.

I am not very familiar with Jsonata so I naturally reach for a function node.

There is no way I could convert the string to an object, convert the number to floating point and add it as a message property in Jsonata.

I expect smarter people could, but if they did, how easy would the code be to understand in 6 month's time?

There are ways to convert in JSONATA, but that is not the topic. The payload is assigned without any modification to a property of an object.

But even if you do not like to use JSONATA - it is still much easier to use a change node which does the same as your function node. Function nodes needs coding. In my point of view function nodes should be avoided when core nodes can be used instead. Otherwise I do not need NodeRed. Then I have a trigger and an output and between instead of designing a flow with all the transparence of the logical flow - I do all programming in a function node in Javascript. Then I am back to text based coding.

Here a solution - using the topic as you did in your function node:

and this without JSONATA - just using the capabilities of a change node.

Sorry for using a localized version of NodeRed - so translated: MOVE is used in the Change Node.

There are always lots of ways to solve any programming problem.
I think your change mode version is better than mine. :grinning:

As soon as you start fiddeling with JSONATA, even inside a standard node, that is in itself coding. From that perspective, no difference, just a matter of taste & preference. In addition, it has been mentioned several times that a function node with code has better performance than an equivalent JSONATA expression. Regardless, the last example you provided seems to be the best & straight forward!

1 Like

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