# Format value for Mqtt

**URL:** <https://discourse.nodered.org/t/format-value-for-mqtt/3759>\
**Category:** General\
**Created:** [7 October 2018 16:37 UTC](https://discourse.nodered.org/t/format-value-for-mqtt/3759 "2018-10-07T16:37:45Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![MupeTTz](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MupeTTz](https://discourse.nodered.org/u/MupeTTz)\
**Post date:** [7 October 2018 16:37 UTC](https://discourse.nodered.org/t/format-value-for-mqtt/3759/1 "2018-10-07T16:37:45Z")

</div>

Hi there, I'm new here as you can see 🙂 I need a little help with my project I'm getting this:

{"payload":[15,32,1,1,0,9,8,183,87,91,2,183,87,255,1,195,130],"port":"/dev/ttyUSB0","\_msgid":"efd1e1ef.29c8e","checksum":0,"run":"ac","type":"Inverter AC","values":{"bat\_volt":55.44,"bat\_current\_used":0,"bat\_current\_charge":0.6,"bat\_inverter\_period":49.902515436095584,"bat\_watts":0,"bat\_charge\_watts":33.263999999999996,"Mains":1,"Absorpion":10,"Bulk":10,"Float":1,"Inverter":10,"Overload":10,"Low Battery":10,"Temperature":10,"bf\_factor":1,"inverter\_factor":1,"mains\_volt":224.55,"mains\_current":6.03,"inverter\_voltage":224.55,"inverter\_current":5.11,"mains\_period":50.1025641025641,"mains\_watts":1354.0365000000002,"inverter\_watts":1147.4505000000001,"InverterWattsOut":1147.4505000000001},"node":1,"packet\_type":2}

If I try to send it over mqtt I get only the:  
[15,32,1,1,0,9,8,183,87,91,2,183,87,255,1,195,130]

Another help would be if you can tell me how can I parse it or make a function to read the values.

Thank you.

---

<div class="post-metadata">

**Author:** ![moebius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moebius/32/2872_2.png) [@moebius](https://discourse.nodered.org/u/moebius)\
**Post date:** [7 October 2018 17:12 UTC](https://discourse.nodered.org/t/format-value-for-mqtt/3759/2 "2018-10-07T17:12:17Z")

</div>

Take a look at the info panel for the mqtt out node, first sentence " `msg.payload` is used as the payload of the published message.".  
That means all other elements of your object will not be sent and you have to prepare the payload according to your needs.

Also you can already access the properties of your object like: var port = msg.port  
The old school parsing is not required.  
Maybe you can tell, what you want to achieve finally with the "parsed" data?

---

<div class="post-metadata">

**Author:** ![MupeTTz](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MupeTTz](https://discourse.nodered.org/u/MupeTTz)\
**Post date:** [7 October 2018 17:24 UTC](https://discourse.nodered.org/t/format-value-for-mqtt/3759/3 "2018-10-07T17:24:50Z")

</div>

I want to send it to my central node-red, and after that to display it's values, simple but it's been some time from when I was doing dev 🙂 a little too much.  
So my project is to monitor the entire house and make decision based on rules/statistics,weather and the rest.

Right now I've implemented,  
Thermostat per room, set value per valve.  
Make decision for the power from the solar.  
Start generator in case of power loss over 5H and battery voltage or current draw.

Different thing's for automation in house 🙂

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [7 October 2018 17:33 UTC](https://discourse.nodered.org/t/format-value-for-mqtt/3759/4 "2018-10-07T17:33:26Z")

</div>

Split the message where you currently have it and send them on separate MQTT topics to your central Node-RED.

---

<div class="post-metadata">

**Author:** ![MupeTTz](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MupeTTz](https://discourse.nodered.org/u/MupeTTz)\
**Post date:** [7 October 2018 18:24 UTC](https://discourse.nodered.org/t/format-value-for-mqtt/3759/5 "2018-10-07T18:24:36Z")

</div>

Well i tried to do something like this:

function decode\_dc(packet)  
{  
var dc = {

```
	bat_volt: packet.readInt16LE(2+5)/100 //cket[2+5])+packet[2+6]*255)/100

}
return dc;

```

}  
msg.values = decode\_dc(msg.payload)  
return msg;

{"payload":[15,32,159,147,200,3,12,161,21,0,0,0,2,0,0,137,135],"port":"/dev/ttyUSB0","\_msgid":"e4321b57.7ac418","checksum":0,"type":1,"run":"DC","values":{"bat\_volt":55.37}}

The ideea is the victron need a pack of bytes to initialize the conversation. after I query it I can do this. Any ideea ?

Protocol for conversation is VE.Can.

---

<div class="post-metadata">

**Author:** ![moebius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moebius/32/2872_2.png) [@moebius](https://discourse.nodered.org/u/moebius)\
**Post date:** [7 October 2018 18:35 UTC](https://discourse.nodered.org/t/format-value-for-mqtt/3759/6 "2018-10-07T18:35:34Z")

</div>

Any idea for what? You didnt express a problem...

---

<div class="post-metadata">

**Author:** ![MupeTTz](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MupeTTz](https://discourse.nodered.org/u/MupeTTz)\
**Post date:** [7 October 2018 21:25 UTC](https://discourse.nodered.org/t/format-value-for-mqtt/3759/7 "2018-10-07T21:25:20Z")

</div>

To get only the values, I don't need the payload from the begin, like I said this type of communication needs data hex for initialization and talk, after I get that, the values. How can I split and get only the values, or reorder the data.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [8 October 2018 06:02 UTC](https://discourse.nodered.org/t/format-value-for-mqtt/3759/8 "2018-10-08T06:02:26Z")

</div>

As example, to get the battery voltage level, try this in a function node (feed it with the complete message you showed in your first post)

```auto
var bat_volt = msg.payload.values.bat_volt;
node.warn(bat_volt);

```

If you need better understanding of objects, arrays and how to access object properties in javascript, I recommend  
[https://www.w3schools.com/js/js\_arrays.asp](https://www.w3schools.com/js/js_arrays.asp)  
[https://www.w3schools.com/js/js\_objects.asp](https://www.w3schools.com/js/js_objects.asp)

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [8 October 2018 06:50 UTC](https://discourse.nodered.org/t/format-value-for-mqtt/3759/9 "2018-10-08T06:50:29Z")

</div>

Also worth reading the docs [https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

and look at the `[CHANGE]` `[SWITCH]` `[SPLIT]` nodes

---

<div class="post-metadata">

**Author:** ![MupeTTz](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MupeTTz](https://discourse.nodered.org/u/MupeTTz)\
**Post date:** [8 October 2018 09:46 UTC](https://discourse.nodered.org/t/format-value-for-mqtt/3759/10 "2018-10-08T09:46:23Z")

</div>

Thank you all for the reply's I will do so, and will come back with an result, good one I hope. Thank you again.

---

<div class="post-metadata">

**Author:** ![MupeTTz](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MupeTTz](https://discourse.nodered.org/u/MupeTTz)\
**Post date:** [8 October 2018 10:08 UTC](https://discourse.nodered.org/t/format-value-for-mqtt/3759/11 "2018-10-08T10:08:30Z")

</div>

TypeError: Cannot read property 'values' of undefined

I think is correct because that message does not come in payload directly...

The correct function it will be:

var bat\_volt = msg.values.bat\_volt;  
node.warn(bat\_volt);

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [8 October 2018 15:14 UTC](https://discourse.nodered.org/t/format-value-for-mqtt/3759/12 "2018-10-08T15:14:53Z")

</div>

Well, whatever, you get the idea now 🕶  
Fine, just do the same way for the other values you need

---

<div class="post-metadata">

**Author:** ![MupeTTz](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MupeTTz](https://discourse.nodered.org/u/MupeTTz)\
**Post date:** [8 October 2018 16:45 UTC](https://discourse.nodered.org/t/format-value-for-mqtt/3759/13 "2018-10-08T16:45:53Z")

</div>

Like I said thank you for the help, and the links.

Thank you all.
