One Shot Trigger

So I am monitoring temperature with an ESP8266… When temp is above a certain value (Say 50F) I want one trigger sent… Not multiple… Is this possible within the trigger setup? Or do I have add some background code…

have a look at this topic Sending MQTT messages within notification limit

So the one shot trigger worked… I also had to create another trigger to turn this trigger back off… Otherwise I would still receive triggers.

no, you dont, what kind of trigger have you added?

Well I thought it worked… However I still received false triggers overnight…
So my threshold is 100… Rising trigger… The value was at 160 all night… When I watch the code run I can see the Trigger Channel send a value of 1 when the threshold is crossed… Then it stops send anything on that channel… (until the threshold is crossed)

can you share the code you have used in this case.

// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling. 

#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include <HCSR04.h>
#include <OneWire.h>
#include <DallasTemperature.h>

float tempf;
float h;
int rssi;
int value;

#define ONE_WIRE_BUS D4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

HCSR04 hc(D1,D2);//initialisation class HCSR04 (trig pin , echo pin)

// WiFi network info.
char ssid[] = "1189";
char wifiPassword[] = "";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "33f4f060-e9cc-11e7-91c0-e55799240ffd";
char password[] = "3cb86b6d7d8fedaf55a2e9145c123926516c9276";
char clientID[] = "f4fffce0-46a7-11e8-a653-55835b4089d1";

#define DATA_CHANNEL 6 //Virtual channel for publishing sensor data.
#define TRIGGER_CHANNEL 7 //Virtual channel for publishing the trigger value.
#define THRESHOLD 100 //Threshold for the trigger.
bool sendBelowThreshold = false; //Set to true if the trigger should happen when the data value is below the threshold, 
                                //false if it should happen when the data value is above or equal to the threshold.
bool crossedThreshold = false;

void setup() {
  pinMode(D5, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  digitalWrite(D5, LOW);
	Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  digitalWrite(LED_BUILTIN, HIGH);

void sendTriggerValue(int channel, int value, int threshold, bool sendBelowThreshold) {
  Serial.print("check"); Serial.print("\t"); Serial.println(value);
  if (((value >= threshold) && !sendBelowThreshold) || ((value < threshold) && sendBelowThreshold)) {
    if (!crossedThreshold) {
      Cayenne.virtualWrite(channel, 1, "door_open", "x"); //set trigger two-state widget to 1 
      crossedThreshold = true;
    Cayenne.virtualWrite(channel, 0, "door_closed", "y"); //set trigger two-state widget to 0
    crossedThreshold = false;

void loop() {
  sensors.requestTemperatures(); // Send the command to get temperatures
  tempf = sensors.getTempFByIndex(0);
  h = hc.dist();
  rssi = WiFi.RSSI();
  Serial.print(millis()/1000); Serial.print("\t"); Serial.print(h); Serial.print("\t"); Serial.print(tempf); Serial.print("\t"); Serial.print(rssi); Serial.print("\t"); Serial.println(value);

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
	// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
	Cayenne.virtualWrite(0, millis()/1000);
  Cayenne.virtualWrite(1, rssi);
	Cayenne.virtualWrite(2, tempf);
	Cayenne.virtualWrite(3, h);
  Cayenne.virtualWrite(4, value); 

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

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
    value = getValue.asInt();
    CAYENNE_LOG("Channel %d, pin %d, value %d", 5, LED_BUILTIN, value);
    digitalWrite(LED_BUILTIN, !value);

i see you have added a digital actuator on channel 7 and then you are using another trigger to turn it off.
where did you get this data type from door_open and door_closed Cayenne.virtualWrite(channel, 1, "door_open", "x"); //set trigger two-state widget to 1

can you just use the code from the link I shared above and only change:

 #define    DATA_CHANNEL 0 
#define THRESHOLD 200

sensor_value to your sensor data.

sendTriggerValue(TRIGGER_CHANNEL, sensor_value, THRESHOLD, sendBelowThreshold);`

Remove all the widgets and upload the code. Add a generic digital input sensor to the trigger channel you have selected and set trigger to this widget.

ok … I will try again… I made the change to “door_open” because I was hoping to get the SMS message to say something else… Other than “on”…

changing won’t change the SMS content as the SMS text which is fixed and cannot be changed. For different data types, have a look at this Data types for Cayenne MQTT API

ok re did everything… After a closer look I saw this… 160 set value… 100 Threshold… Random jump down to 62 which set off trigger… I think I found my issue… Thanks.

20:45:09.737 → 4155 161
20:45:10.739 → 4156 160
20:45:11.742 → 4157 160
20:45:12.791 → 4158 160
20:45:13.794 → 4159 161
20:45:14.797 → 4160 160
20:45:15.799 → 4161 161
20:45:16.802 → 4162 160
20:45:17.858 → 4163 160
20:45:18.860 → 4164 161
20:45:19.847 → [4165293] Connection ok
20:45:19.847 → 4165 62
20:45:20.866 → [4166299] Publish: topic 1, channel 1, value 0, subkey d, key digital_sensor
20:45:20.913 → [4166300] Publish: topic 1, channel 0, value 62, subkey null, key analog_sensor
20:45:21.013 → 4166 133
20:45:21.915 → 4167 161
20:45:22.918 → 4168 161
20:45:23.921 → 4169 160

1 Like