# Add topic to each array values, output JSON for MQTT?

**URL:** <https://discourse.nodered.org/t/add-topic-to-each-array-values-output-json-for-mqtt/39478>\
**Category:** General\
**Created:** [21 January 2021 08:49 UTC](https://discourse.nodered.org/t/add-topic-to-each-array-values-output-json-for-mqtt/39478 "2021-01-21T08:49:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SaiQmon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/saiqmon/32/6431_2.png) [@SaiQmon](https://discourse.nodered.org/u/SaiQmon)\
**Post date:** [21 January 2021 08:49 UTC](https://discourse.nodered.org/t/add-topic-to-each-array-values-output-json-for-mqtt/39478/1 "2021-01-21T08:49:49Z")

</div>

Hi,

I read 92 x values from heating controller via modbus TCP. Response payload is just array of values

![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cd076e6623b5ca2c33dfba57d6e40904df09193d.jpeg)

I have a list, what each value means. e.g. 3rd value name is "403". **I don't use any database** in Node Red, so the list should be built in the nodes.

Also, there are always empty values. For example 5th value is never there, so it should be skipped. So it doesn't have a matching pair in the list. About 50 of those 92 received values actually mean something.

What I'd like to do, is to publish the values via MQTT in JSON message to be used in Home Assistant sensors.

My thoughts were to add a topic (or other identifier) for each value so it can be picked in Home Assistant and build a JSON message.

```auto
{"403":123,"404":456}

```

I really suck at function node and couldn't find suitable example from forum.

Home Assistant can easily divide JSON message for different sensor values:

```auto
sensor:
  - platform: mqtt
    name: "403"
    state_topic: "heating/HC1"
    unit_of_measurement: 'C'
    value_template: "{{ value_json.403}}"

```

---

<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:** [21 January 2021 09:55 UTC](https://discourse.nodered.org/t/add-topic-to-each-array-values-output-json-for-mqtt/39478/2 "2021-01-21T09:55:28Z")

</div>

> [@SaiQmon](#):
>
> What I'd like to do, is to publish the values via MQTT in

The [buffer-parser](https://flows.nodered.org/node/node-red-contrib-buffer-parser) nodes are designed for this. It can name your items and spit them out as topic+payload directly to MQTT. It will also convert and values as required (e.g. to float or string or bits)

---

<div class="post-metadata">

**Author:** ![SaiQmon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/saiqmon/32/6431_2.png) [@SaiQmon](https://discourse.nodered.org/u/SaiQmon)\
**Post date:** [21 January 2021 11:22 UTC](https://discourse.nodered.org/t/add-topic-to-each-array-values-output-json-for-mqtt/39478/3 "2021-01-21T11:22:14Z")

</div>

Thank you, I got it running. Some additional questions related to buffer-parser:

- Can you round the values? Even my original value (temperature) is e.g. "223" and I scale it with "0.1" sometime the final value is 22.3001234 for example. I used Int16 (be), no idea what it should be.

I tried rounding for final JSON with function node, but once again my knowledge failed me 😃

---

<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:** [21 January 2021 11:27 UTC](https://discourse.nodered.org/t/add-topic-to-each-array-values-output-json-for-mqtt/39478/4 "2021-01-21T11:27:33Z")

</div>

rounding should really only be done at the final step (for presentation e.g. for display on a dashboard) - is there a reason you need to round the values?

If you must: in a function node use [toFixed](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) or [round](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round)

toFixed turns your number into a formatted string, round keeps it as a number. Both will lose precision.

Last note: if it is just for display on dashboard, you should really leave number as it is and use the angular formatting syntax.

---

<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:** [4 February 2021 11:27 UTC](https://discourse.nodered.org/t/add-topic-to-each-array-values-output-json-for-mqtt/39478/5 "2021-02-04T11:27:38Z")

</div>

Closed on 4 February 2021 11:27 UTC

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