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

Hi,

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

1

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.

{"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:

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

The 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)

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

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 or 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.

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