GSM Notifications for weewx

68 views
Skip to first unread message

Remy Lavabre

unread,
Feb 3, 2023, 12:22:04 PM2/3/23
to weewx-user
Hello everyone...

Like you, I use the superb WeeWX software (thanks again for its designers!) and perhaps the alarm.py extension (or even better, alarm_multi.py).

By slightly modifying alarm.py or alarm_multi.py, it is very easily possible to receive notifications on your GSM mobile phone (The application is available for Android or iPhone on the store).

For example, when it rains, when the temperature drops or rises above a certain value, when there is wind with a pre-defined speed, etc. etc you receive an alert on your mobile phone

The original extension allows you to send an email, but with ntfy, you can very easily send notifications directly to your phones. Several settings are possible, such as adding an icon, defining a degree of urgency, etc. etc

It is also possible to self-host the notification server directly on your computer, which avoids going through the third-party server (more secure).

The documentation is available here: https://ntfy.sh/

I leave you a working example in python. Good fun to those who are interested ;-)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import locale
from pip._vendor import requests


locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')


URL_NOTIFY = "https://ntfy.sh/"
ABONNEMENT_NOTIFY = "MYNOTIF_PARAMETER"
URL_SERVEUR = "https://meteomillau.pagesperso-orange.fr/"
WEBCAM_FILE = "webcam.png"
ICON_ALERT = "warning,skull"


# Fonction de notification sur téléphone Android ou Iphone via l'application ntfy
# titre correspond au titre du message, message au texte du message, icone aux icones de notication rajoutés (voir plus bas), urgence de 1 à 5,
# click_ok si vrai renvoie sur une addresse http si click sur la notification et image_ok si vrai renvoie l'image png réduite de la WebCam
# En réponse à la procédure notify, le code de retour de la requête put.
# Icones : cloud_with_snow, cloud_with_rain, fog, thermometer, sunny, cold_face, hot_face, tornado, warning
# En fin d'operation, renvoie "True" et code erreur 0 si tout s'est bien passé, sinon False avec le libellé de l'erreur.

def notify (titre,message,icone,urgence,clic_ok,image_ok) :

    if clic_ok :
        ADDRESSE_CLIC = URL_SERVEUR
    else :
        ADDRESSE_CLIC = None

    if image_ok :
        ADDRESSE_IMAGE = URL_SERVEUR + WEBCAM_FILE
    else :
        ADDRESSE_IMAGE = None

    PRIORITY_VALUE = {
        1: "min",
        2: "low",
        3: "default",
        4: "high",
        5: "urgent"
        }


    priorite = PRIORITY_VALUE.get (urgence)
   
    headers = {
        'Click' : ADDRESSE_CLIC,
        'Title' : titre.encode(encoding='utf-8'),
        'Attach' : ADDRESSE_IMAGE,
        'Priority': priorite,
        'Tags': icone,
    }
    if priorite != None :
        try :
            reponse = requests.put(URL_NOTIFY + ABONNEMENT_NOTIFY, headers=headers, data=message.encode(encoding='utf-8'))
            status_envoie = (reponse.reason=="OK")
            erreur = "Notification GSM correctement envoyée"
        except Exception as e:
            status_envoie = False
            erreur = e
    else :
        status_envoie = False
        erreur = "Erreur de notification GSM : niveau d'urgence erroné (0...-> 5)"
    return (status_envoie,erreur)





# ---------------------------------> Début du Programme <-------------------------------------



subj = "Alerte météo : TEMPERATURE EXTERIEURE TRES BASSE ( < -5°C ) - **** ! DANGER GEL SEVERE ! ****"
status,code_erreur= notify ("Message d'alerte météo",subj,"warning",None,True,True)
print (status)
print (code_erreur)
exit ()
Reply all
Reply to author
Forward
0 new messages