After some digging, I figured out that the device receiving the MQTT data needs a data in a JSON format.
So I tried linking an inject node set up as follows:
That worked! The device received the value as expected:
My question is how to I send a value from HA in the {“xx”:xx} format?
Is there a way to reformat a value in Node-RED prior to the MQTT publish?
Am I a complete idiot and going about this completely wrong??
FYI I’m a complete newb when it comes to JSON and Node-RED. I’ve been programming commercial building automation systems for 20 years, but this is a new realm for me.
First let’s take a step back. Node red is a system based on messages that are sent between nodes. Understanding what you are sending and how to manipulate it is key. The easiest way to know what is being sent is to use a debug. Set that to complete message obj.
The default setting of payload returns only the information contained in the payload position. If we look at the bottom of a home assistant node you will see output options. Different nodes have different default output settings. Here is the event state output options.
I’d suggest leaving it as is for the moment connecting a debug to look at the complete output. You can add/delete them as needed. One of the defined outputs is msg.topic. Messages in certain positions can override settings in nodes. This also varies by node, you can look in the nodes help panel.
In the mqtt node you can see it has an input override for the topic. Anything in msg.topic position will replace the topic path set in the node. So deleting topic and data, then setting payload to state. (state is the primary value of an entity)
This will send a number in the payload position. The node converts the output to a JSON obj. There result will be in the payload position.
Jsonata is the primary way to manipulate the values sent/received by nodes. This is an introduction to it’s use. This is also the documentation for all the home assistant nodes. There are also examples to help get you started.
First of all, thank you so much. What an awesome response.
Great advice; enabling the debug node’s ‘complete msg object’ output should be the default! What a game-changer.
So, I’ve been able to determine the difference between the two different methods of sending data, but I’m struggling to figure out how to recreate what the inject node is producing.
In this screenshot, the output from Office Temp (debug 28) gives me the error: sedona.c:181: Failed to parse config data(error: line 1: ‘[’ or ‘{’ expected near ‘73.9’) on the subscribed MQTT device.
You have an object inside an object. To recreate that structure instead of payload use payload.in This is referred to as dot notation. If you go to the line in the debug little icons appear to the right. There you can copy the path or the value.