Format value for Mqtt

#1

Hi there, I'm new here as you can see :slight_smile: 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.

#2

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?

#3

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

#4

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

#5

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.

#6

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

#7

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.

#8

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)

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_objects.asp

1 Like
#9

Also worth reading the docs https://nodered.org/docs/user-guide/messages

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

1 Like
#10

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.

#11

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

#12

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

#13

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

Thank you all.