Sending MQTT messages within notification limit


notification trigger (SMS and email) are currently limited to 300 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 300 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.

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

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


  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.

  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;
    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 or any other library you are using.

Mail trigger
Trigger doesn't work
Problem with the triggers
Monitoring shore power connected on a yacht