# Send twilio sms when temperature sensor output \<xx?

**URL:** <https://discourse.nodered.org/t/send-twilio-sms-when-temperature-sensor-output-xx/44990>\
**Category:** General\
**Created:** [30 April 2021 13:52 UTC](https://discourse.nodered.org/t/send-twilio-sms-when-temperature-sensor-output-xx/44990 "2021-04-30T13:52:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Advocat1978](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/advocat1978/32/39469_2.png) [@Advocat1978](https://discourse.nodered.org/u/Advocat1978)\
**Post date:** [30 April 2021 13:52 UTC](https://discourse.nodered.org/t/send-twilio-sms-when-temperature-sensor-output-xx/44990/1 "2021-04-30T13:52:00Z")

</div>

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:

```auto
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [30 April 2021 14:32 UTC](https://discourse.nodered.org/t/send-twilio-sms-when-temperature-sensor-output-xx/44990/2 "2021-04-30T14:32:33Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/f/5faed439467c51d415570427b7abd7a894386e23.png)

```auto
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

---

<div class="post-metadata">

**Author:** ![Advocat1978](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/advocat1978/32/39469_2.png) [@Advocat1978](https://discourse.nodered.org/u/Advocat1978)\
**Post date:** [30 April 2021 17:16 UTC](https://discourse.nodered.org/t/send-twilio-sms-when-temperature-sensor-output-xx/44990/3 "2021-04-30T17:16:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [30 April 2021 21:10 UTC](https://discourse.nodered.org/t/send-twilio-sms-when-temperature-sensor-output-xx/44990/4 "2021-04-30T21:10:32Z")

</div>

> [@Advocat1978](#):
>
> 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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/9/e9eaad5220ce6ee5f396851ee619c18ab5888143.png)

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.

```auto
// 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
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 May 2021 21:10 UTC](https://discourse.nodered.org/t/send-twilio-sms-when-temperature-sensor-output-xx/44990/5 "2021-05-14T21:10:59Z")

</div>

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