TypeError: msg.payload.slice is not a function

I am trying to use the Twitter node for the first time.
As an initial trial I am feeding the output from an 18B20 temperature sensor (using "rpi-ds18b20") node to the "Twitter out" node. All on a Raspberry Pi 3B+ running Buster.
The temperature gives me a floating point number and the Twitter node expects a string. I can see the value in a debug node and I also publish it to MQTT - so that part is working.
I have been through setting up the Twitter developer account and gave it the various strings and secrets.

So I get the error in the title "TypeError: msg.payload.slice is not a function", I assume because the 18B20 is pulishing a floating point number and the Twitter node expects a string.

I tried to convert the number to a string using ....

return msg;```


But that didn't work!
My Javascript is pretty (very) poor - can someone suggest how to fix it?

Thanks
Bruce

Sorry messed up the code part!

The actual code is ...

msg.payload = (msg.payload).toString();
return msg;

Just used an inject node to pass a string to the Twitter node and that publishes OK, so that bit is working too.

Add a debug node showing what is going to the twitter node. Set the debug node to Show Complete Message.
Also tell us which twitter node you are using, there are several. Is it node-red-node-twitter?

OK So here is the output of the debug node ...

{"_msgid":"d35b1959.686418","topic":"05EF9C040000","payload":26.25,"file":"28-0000049cef05","dir":"w1_bus_master1","family":"28"}

And yes it is "node-red-node-twitter"

So I assume the 26.25 is the temperature value which is the payload.

Can you send us a screenshot of your flow ?

msg.payload = (msg.payload).toString();
return msg;

the above code looks correct but i dont undestand where you use msg.payload.slice which is giving you the error

OK here you are, only the centre one uses the Twitter node ...

.. i havent used twitter node .. but according to your previous posts .. if tweeter node expects a string in payload .. then you should have a function node between Study temp and Tweet node
that has your float to string convertion :

msg.payload = (msg.payload).toString();
return msg;

It seems unlikely that that is coming from a function node containing

msg.payload = (msg.payload).toString();
return msg;

OK, so I have now put the ".toString" conversion in a separate function node, here is the new layout....

But I now get an error returned of ...
"Status is a duplicate."

Well at least it seems to be progress ...

A different error message is always progress :smiley:

I see from msg debug node that we are sending things to Tweet node that it doesnt care about.
Like topic, file, dir etc

Lets eliminate those extra object properties from the message in Float to string function node.

so function node can be :

let newMsg = {};   // start new
newMsg.payload = (msg.payload).toString();
return newMsg;

needless to say .. you checked your Twitter account web site to see if the message is there and we are not sending a dublicate to fast

That is coming from the Twitter API. You cannot tweet the same text twice in a row.

There was a recent thread on this same issue - the Twitter node doesn't handle number types... It assumes you are giving it a string message as tweeting bare numbers isn't really what twitter is for (and could fall foul of their terms of service)

So based on Nick's suggestion above we once again need to modify that function node :

let newMsg = {};   // start new
newMsg.payload = `Message from Node-red with my number ${msg.payload}`;   // no need for toString any more
return newMsg;

Right, but if you try to send the same message twice, the Twitter API will reject it.

How about you add a date and time
"The temperature is 27.58 at 13.35 on the 17/08/2020"
That would make the message unique.

Or just wait for the temperature to change. Not allowing duplicates is there for a reason.

Add an RBE node before the twitter node so that it does not try to send the same message twice.

1 Like

OK so I added a time field to make the Tweets unique as suggested by E1cid. By the way Twitter won't let you repeat a Tweet for 24 hours.

Code is
'''let newMsg = {}; // start new
let today = new Date();
newMsg.payload = (msg.payload).toString() + " C is the study temperature at "+today.getHours()+":"+today.getMinutes();
return newMsg;'''

That works but I need to fix having a leading zero in the minutes field.

Thanks for all your help.
Bruce

PS Colin - what is an RBE node?

It is one of the standard nodes. Report By Exception.