About edit function node before MQTT OUT

I have a question about selecting one payload_fields signal in function node, so the MQTT OUT node can only output one signal such as humidity. But in fact, in my TTN console, the downlink massage is always 00000000000, I am confused maybe the first line of the code is wrong, here is my code:

$: payload = msg.payload.payload_fields.humidity;
return {
topic: 'molly-pycom-device/devices/orangefipy/down',
payload: {
port: 2,
confirmed: false,
payload_raw: new Buffer(payload).toString('base64'),
schedule: 'replace'

Wishing someone can solve my problem. Thanks very much!

Hi @molly

The issue is this line:

payload_raw: new Buffer(payload).toString('base64'),

At this point payload contains the number 39.4597.... When you pass that to the Buffer constructor, it thinks you want a buffer object of size 39.4597. If you want a create a string representation of that number you could do:

payload_raw: ""+payload

If base64 encoding is a particular requirement you have, then you can do:

payload_raw: Buffer.from(""+payload).toString('base64'),

It works! Thanks very much!!!:smiley: