Rainbird control from node-red

Hello

I have successfully installed node-rainbird - npm and using it in node-red.

The example on how to use the package states:

const RainBirdClass = require('node-rainbird');

let rainbird = new RainBirdClass("your_ip_address", "your_password");

rainbird.setDebug(); // Only set this for verbose logging information

rainbird
.stopIrrigation()
.then(console.log)
.catch(console.error);

I can run this on my machine using node.js and return the message successfully to console. But how do I return the console.log into a msg.payload instead in a node-red function?

In my function I tried:

const RainBirdClass = require('node-rainbird');

let rainbird = new RainBirdClass("MY_IP", "MY_PASSWORD");

let temp = rainbird.getSerialNumber();
msg.payload = temp;
return msg;

but I get an empty payload :\

The function node does not support require - use the setup tab

This article should help: Use any npm module in Node-RED • FlowFuse

Sorry I pasted my function wrong

I have

var rainbird = new RainBirdClass("MY_IP", "MY_PASSWORD");

let temp = rainbird.getSerialNumber();

msg.payload = temp;
return msg;

Add node.warn({ RainBirdClass }) to the start of the function, fire a msg into it then inspect the debug sidebar - what do you see?

I see this:

image

Then your solution is

const rainbird = new RainBirdClass("MY_IP", "MY_PASSWORD");
msg.payload = await rainbird.getSerialNumber();
return msg

The functions in the README dont say they return a promise but a quick glance at the package reveals they return a request promise. Therefore you eaither need to await or do the then().catch

If you want to do promise chain then its like this...

const rainbird = new RainBirdClass.RainBirdClass("MY_IP", "MY_PASSWORD");
rainbird.getSerialNumber().then(result => {
    msg.payload = result
    node.send(msg)
}).catch(err => {
    node.error(err, msg)
})

Thank you very much!

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