Sending MQTT messages within notification limit

notification trigger (SMS and email) are currently limited to 90 per day. the trigger activates every time the device publish a message above the set threshold. To avoid publish message flood and causing the trigger to run continuously and hit the 90 limit quickly use the following code for sending only once notification trigger when the threshold is crossed. For actuator trigger, there is no limit and there is no need to use the code but it is good practice to use as it.

Using Cayenne-MQTT-Arduino library.
https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino/blob/master/examples/Basics/SendDataOnTrigger/SendDataOnTrigger.ino

Code explanation:
the main point here is to only publish data once when the trigger threshold is crossed.

#define DATA_CHANNEL 0 
#define TRIGGER_CHANNEL 1 
#define THRESHOLD 200
bool sendBelowThreshold = true; ,

where

  1. DATA_CHANNEL is the MQTT channel used to publish sensor data at a 15-second interval.

  2. TRIGGER_CHANNEL is the MQTT channel which will publish value 1 when the sensor value has crossed the threshold and publish 0 when the sensor value is below the threshold.

  3. THRESHOLD is the threshold value you need to set for the trigger to activate.

  4. sendBelowThreshold is the boolean value which when set to true the trigger is set for value below the threshold and if false the trigger is set for values above or equal the threshold.

you only need to set this values correctly for your trigger to send notification only once. rest the code handles everything.

CAYENNE_OUT_DEFAULT()
{
  int sensor_value = analogRead(A0); 
  sendTriggerValue(TRIGGER_CHANNEL, sensor_value, THRESHOLD, sendBelowThreshold);
  Cayenne.virtualWrite(DATA_CHANNEL, sensor_value , "analog_sensor", "null"); //widget to display sensor data.
}

the line sendTriggerValue(TRIGGER_CHANNEL, sensor_value, THRESHOLD, sendBelowThreshold); calls the function sendTriggerValue() with all the arguments.

void sendTriggerValue(int channel, int value, int threshold, bool sendBelowThreshold) {
  if (((value >= threshold) && !sendBelowThreshold) || ((value < threshold) && sendBelowThreshold)) {
    if (!crossedThreshold) {
      Cayenne.virtualWrite(channel, 1, "digital_sensor", "d"); //set trigger two-state widget to 1 
      crossedThreshold = true;
    }
  }
  else
  {
    Cayenne.virtualWrite(channel, 0, "digital_sensor", "d"); //set trigger two-state widget to 0
    crossedThreshold = false;
  }
}

this function handles publishing data only once.

when you upload the code with correct values set, there will two temporary green widgets populated on your dashboard i.e. one with DATA_CHANNEL as a generic analog sensor value widget and other with TRIGGER_CHANNEL as a two-state digital sensor widget. Add them by clicking on + top right of each widget. Now add a trigger to TRIGGER_CHANNEL widget when it becomes 1 as the code only publish data 1 once the trigger threshold is crossed.

the same principle applies when using Cayenne MQTT Python Library
https://github.com/myDevicesIoT/Cayenne-MQTT-Python/blob/master/examples/Example-04-SendDataOnTrigger.py or any other library you are using.

2 Likes
Multiple trigger notifications every minute
Problem with the triggers
Mail trigger
Trigger does not work with temperature
My Analogue sensor trigger is not working in Cayenne
Sending a lot of SMS alert and email
Trigger not received in email
Alarma
DHT plug-in on RaspberryPi
Triggers and Limits
Trigger doesn't work
At first sms and email didn't work afterwards
Trigger problem
Monitoring shore power connected on a yacht
Controlling a VFD using a potentiometer
Notification
Trigger emails not arriving
Notification message formats
Trigger Status
ESP8266 and LED
New to Cayenne-triggers-iPhone
SMS Trigger Troubleshooting
Not receiving notifications from triggers
Not receiving any notifications via a trigger
Trigger not send email
Triggers in led status
Problema con los trigger
Temperature False Triggers
SMS messages and emails through Triggers and Alerts don't work
Stopping of Alerts / Notifications during device or trigger OFF mode
Button widget as push/release button
Cayenne + ESP8266 ( Average temeprature)
Button Not updating (Toggle Switch Icon)
Triggers only work partially
Text Notifications Not Working
SMS Alerts problem
Trigger notify problem
Raspberry Pi Generic Digial IO Notifications
BH1750/GY-30 Luminosity Sensor with Cayenne using Raspberry Pi 3 Model B
No trigger notifications
Notifications are not working
Triggers and Limits
Triggers and Limits
Trigger flood DS18B20 Temp sensor on raspberry pi
Esp8266 and luminocity sensor
Cayenne sending too many emails
Trigger events limitation, web not firmware solution
Triggers and Limits
My Analogue sensor trigger is not working in Cayenne
One Shot Trigger
My Analogue sensor trigger is not working in Cayenne
Cayenne disabling my Lora gateway
Question about cayenne triggers
Trigger is not working
Question about cayenne triggers

Fantastic… Let me try that…

Hi, thanks for the reply,
but i have three temperature sensors in my project, how should i change my code for three trigger and which values i need to set?

thanks

you just need it set this for all three sensor.

How do we know the trigger channel

Trigger Channel is the channel number of the digital two state widget which you want to set the trigger on the cayenne dashboard,

how about the Threshold value

Am working on aproject to send alert when humidity exceeds the 50
how should i prevent the triger from floading too many times

CAYENNE_OUT(2)
{
float hum=dht.readHumidity();
Serial.println(“Humi is”);
Serial.println(hum);
Cayenne.virtualWrite(2, hum );
}

so the threshold value will be 50

triger value?

It is clearly explained above all the things and how to do it. Please go through it again.

What is the result expected from running the above code ?

i guess the topic says it all.

If i have so many triggers how do i know which trigger is activated

First of all, have you understood the above code? and are you familiar with a little bit of coding?

Yes am familiar with coding in arduino
but this one is abit challenging
maybe if you can tell me how to deal with it may help

can you create a new topic, as i dont want this topic to be loaded with coding lesson.