Send twilio sms when temperature sensor output <xx?

Hi everyone,

This is my first post on here so go easy on me please!
My project involves monitoring temperature and humidity outputs from some sensors that are publishing and subscribing on my local MQTT broker on my raspberry pi 4B (mosquitto).
I would like to be able to send an sms message through twilio if the temperature drops below a certain value. I have signed up to twilio, installed on node red and I can send the message through twilio from the command line with:

node sms

the output is some kind of send receipt and the message appears on my phone.

So what I want is for node-red to run my sms.js file when the temperature drops below my setpoint. I am trying to implement this through a function node, however, my javascript knowledge is zero. This is what I have tried so far:

const fileUrl = '/home/pi/Documents/SmsSender.java/sms.js'; // provide file location
var payload = msg.payload;
if (msg.payload <=21);
src ('/home/pi/Documents/SmsSender.java/sms.js');
return msg;

This returns ReferenceError: src is not defined (line 4, col 1)
I also had it set up previously where the temperature value was being sent by sms which is undesirable. I need the twilio node to send my pre-defined message (sms.js) which has been configured previously.
Hopefully I have given enough information for someone to be able to help.
Thanks in advance,
John

Hi John and welcome to the forum,

Its not very clear to me why you are trying to load that external sms.js file.
If you have the Twilio node-red node installed then replicate whatever pre-defined messages you have in sms.js entirely in your node-red flow.

Based on the node's documentation .. msg.payload can either contain the text of the SMS message and if the number is left blank, it can be set using msg.topic

So in a Function just before the Twilio node

image

if (msg.payload <=21) {
msg.topic = "234324324234"  // your number
msg.payload = "ALERT. Temperature below 21 Degrees" // your pre-defined msg
return msg;
}

ps. the node hasnt been updated for the last 3 years and i havent used Twilio 's service before. hope it works

1 Like

Hi @UnborN
Thankyou for replying with the solution. It works great.
There is however one small problem which I didn't originally make clear.
Obviously this solution will keep sending messages until the temperature rises above 21° C.
Would there be a way to have only one message sent in say a 12 or 24 hour period?

You can use the Delay node after your function node, to rate limit the msgs.
With Drop intermediate messages enabled.

image

You may need some slight modification in the function node code so in the case than within the time frame the temperature returns to normal .. you send a msg.reset to the Rate node to reset it.

// if temp less than 21, prepare sms
if (msg.payload <= 21) {
    msg.topic = "234324324234"  // your number
    msg.payload = "ALERT. Temperature below 21 Degrees" // your pre-defined msg
    return msg;
}
else {
    msg.reset = true  // reset the delay
    return msg
}

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