Multiple trigger notifications every minute

Hi, I have doing an alarm and control system with Cayenne and Raspberry.

I have some outputs to activate manually and some inputs when activated they send me a notification (trigger) through telegram…

The trouble comes when the input trigger… I receive one, two or three notifications per minute.

Is any way to get only one notification? I tried with email notification and same trouble.

Thanks in advance.

which device are you using?

Hi, Raspberry Pi 3

if you are familiar with python programming then there is a workaround for this

Hi, no, I don´t know nothing about python… starting with Cayenne…

Is another “easy way”, such as crontab or similar to change this?

Thanks!!

the python code is very easy and requires a minimal coding from the user end. what are the sensors and actuator you are using ?

Hi, the actuators are very simple, only a push button with a pullup resistor… when push button send only one notification or one every 30 minutes…

add this commands on you pi:

git clone https://github.com/myDevicesIoT/Cayenne-MQTT-Python
cd Cayenne-MQTT-Python
sudo python setup.py install

then create a new device Bring your own thing on your cayenne dashboard and get the MQTT credentials and add them in below code.

#!/usr/bin/env python
import cayenne.client
import time
import logging
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)

# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = "MQTT_USERNAME"
MQTT_PASSWORD  = "MQTT_PASSWORD"
MQTT_CLIENT_ID = "MQTT_CLIENT_ID"

TRIGGER_CHANNEL = 2  #Virtual channel for publishing the trigger value.
THRESHOLD = 0        #Threshold for the trigger.

i=0
timestamp = 0
send_below_threshold = 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.
crossed_threshold = False

# The callback for when a message is received from Cayenne.
def on_message(message):    
    print("message received: " + str(message))
    # If there is an error processing the message return an error string, otherwise return nothing.

client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, loglevel=logging.INFO)
# For a secure connection use port 8883 when calling client.begin:
# client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883, loglevel=logging.INFO)

def send_trigger_value(trigger_channel, sensor_value, threshold, send_below_threshold):
    global crossed_threshold
    if (((sensor_value >= threshold) and not send_below_threshold) or ((sensor_value < threshold) and send_below_threshold)):
        if not crossed_threshold:
            client.virtualWrite(trigger_channel, 1, "digital_sensor", "d")
            crossed_threshold = True
    else:
        client.virtualWrite(trigger_channel, 0, "digital_sensor", "d")
        crossed_threshold = False


while True:
    client.loop()
    if (time.time() > timestamp + 10):
        sensor_value =  GPIO.input(10)
        send_trigger_value(TRIGGER_CHANNEL, sensor_value, THRESHOLD, send_below_threshold)
        timestamp = time.time()

Give this code a try and let me know.

Hi, thanks for your prompt reply, I have installed python, created a input12.py with this code and MQTT username, password and client_id, changed trigger channel for the channel where I will test this… nothing more changed.

I have chmod +x and executed input12.py and cayenne.mydevices detected the “new device”… now I think I need to create a new sensor on this new device… but only I can select the Raspberry Pi… I can´t select “new device” to create the sensor…

On other side, I think I need a python code and “new device” for all inputs, right? And I need to add to Raspbian startup, Am I right?

is your sensor a push button or a switch? if it is switch try this simpler code which sends data only on state change and will send trigger only once the state is changed.

#!/usr/bin/env python
import cayenne.client
import time
import logging
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(40, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)

# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = "xxx"
MQTT_PASSWORD  = "xxx"
MQTT_CLIENT_ID = "xxx"

TRIGGER_CHANNEL = 2  #Virtual channel for publishing the trigger value.
i=0
timestamp = 0

# The callback for when a message is received from Cayenne.
def on_message(message):    
    print("message received: " + str(message))
    # If there is an error processing the message return an error string, otherwise return nothing.

client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, loglevel=logging.INFO)
# For a secure connection use port 8883 when calling client.begin:
# client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883, loglevel=logging.INFO)

previousState = -1
currentState = -1

while True:
    client.loop()
    if (time.time() > timestamp + 5):
        currentState =  GPIO.input(40)
        print(currentState)
        if (currentState != previousState):
                client.virtualWrite(TRIGGER_CHANNEL, currentState, "digital_sensor", "d")
                previousState = currentState
        timestamp = time.time()

when you change the state of the switch, the data will be published only once on the dashboard.

you can edit the same python code and all the sensors code in it.

Do you mean to start the script on startup? yes, you can do it by following the steps from here Five Ways to Run a Program On Your Raspberry Pi At Startup