Help with JSON formatting/conversion

Hello smart peoples!!

I’m trying to send a value from Home Assistant to an MQTT device, but there seems to be a formatting issue going on.

When I link the events: state node directly to the MQTT publish node:


I receive the following error:

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:
image

That worked! The device received the value as expected:
image

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.

Thanks in advance!!
Dylan

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.

image

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.

image

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.

image

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)

image

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.

image

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.

1 Like

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.


However, when manually firing the temp/sensor inject node, the data is successfully sent without error.

Here’s the configuration for the Office Temp node

Here’s the configuration for the inject node

So it seems like the Office Temp node somehow excludes the brackets (according to the error I get.)

Also, in the debug, I noticed the payload for the inject node is object

Do you have any idea how I can get the Office Temp node to match the configuration of the inject node?

Again, thank you for taking the time to work with me. I have a lot of reading to do!

Sorry I over looked that. { } = obj = object If you look at the debug you will see this written out.

image

The first looks like this

{
    "payload": 73.9,
    "in": 73.9,
    "topic": "OfficeTemp"
}

The structure of the second

{
    "payload": {
        "in": 73.9
    },
    "topic": "OfficeTemp"
}

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.

image