Send email every interval

Hello.

Today i am fighting with an email that should be send every "interval" time.I thought this will be a easy task, but here i am. I cant see my error. The error is, when i change the interval, i get multilple emails at different intervals. it seems that there are more intervals. I store the interval in a global. But for a better understanding, here is my current code:

[
    {
        "id": "7fd668330b39dee9",
        "type": "switch",
        "z": "746eb8d0d4ff58af",
        "name": "Check if email was sent (emailSent)",
        "property": "emailSent",
        "propertyType": "global",
        "rules": [
            {
                "t": "false"
            },
            {
                "t": "true"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 140,
        "y": 560,
        "wires": [
            [
                "6856e0132700fbc5"
            ],
            []
        ]
    },
    {
        "id": "6856e0132700fbc5",
        "type": "function",
        "z": "746eb8d0d4ff58af",
        "name": "prepare Email",
        "func": "var alarm = msg.payload[\"CIM500/ALARM_WARNING\"][0].AlarmCode\nvar warning = msg.payload[\"CIM500/ALARM_WARNING\"][0].WarningCode\n//-------------------\nvar alarm_present = msg.payload[\"CIM500/ADR_204\"][0].Fault\nvar warning_present = msg.payload[\"CIM500/ADR_204\"][0].Warning\n//-------------------\nlet email = flow.get(\"saveformEmail\")\nlet emailSent = global.get(\"emailSent\")\n\nif ((alarm_present == \"ON\" || warning_present == \"ON\") && email.EmailOnOff == true )\n{\n\n    var msg1 = { to: email.Email1, payload:{alarm,warning}, topic: \"Alarm or warning triggered\" };\n    return [msg1];\n} \n\nelse {\n    return;\n}\n\n\n\n\n\n\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 380,
        "y": 560,
        "wires": [
            [
                "8ac50f5f7dcf8f64",
                "e3af556f6a5808ef"
            ]
        ]
    },
    {
        "id": "8ac50f5f7dcf8f64",
        "type": "change",
        "z": "746eb8d0d4ff58af",
        "name": "set emailSent to true (was send)",
        "rules": [
            {
                "t": "set",
                "p": "emailSent",
                "pt": "global",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 610,
        "y": 560,
        "wires": [
            [
                "d95b014856b0098c",
                "62e2a3ebfa21cc84"
            ]
        ]
    },
    {
        "id": "e3af556f6a5808ef",
        "type": "debug",
        "z": "746eb8d0d4ff58af",
        "name": "debug 276",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 650,
        "y": 500,
        "wires": []
    },
    {
        "id": "62e2a3ebfa21cc84",
        "type": "e-mail",
        "z": "746eb8d0d4ff58af",
        "server": "smtp.office365.com",
        "port": "587",
        "secure": false,
        "tls": false,
        "name": "",
        "dname": "send email",
        "credentials": {
            "userid": "",
            "password": ""
        },
        "x": 770,
        "y": 440,
        "wires": []
    },
    {
        "id": "d95b014856b0098c",
        "type": "function",
        "z": "746eb8d0d4ff58af",
        "name": "set timer for interval",
        "func": "let email = flow.get(\"saveformEmail\")\n\nmsg.payload = email.Interval * 60000\nreturn msg;\n\n\n\n\n\n\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 440,
        "y": 680,
        "wires": [
            [
                "ec7e80f7b94cfe14"
            ]
        ]
    },
    {
        "id": "ec7e80f7b94cfe14",
        "type": "change",
        "z": "746eb8d0d4ff58af",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "delay",
                "pt": "global",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 700,
        "y": 640,
        "wires": [
            [
                "64583836920d9e86"
            ]
        ]
    },
    {
        "id": "64583836920d9e86",
        "type": "change",
        "z": "746eb8d0d4ff58af",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "delay",
                "pt": "msg",
                "to": "delay",
                "tot": "global"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 720,
        "y": 680,
        "wires": [
            [
                "d0279fe2017eba01"
            ]
        ]
    },
    {
        "id": "d0279fe2017eba01",
        "type": "delay",
        "z": "746eb8d0d4ff58af",
        "name": "delay intervall for reset",
        "pauseType": "delayv",
        "timeout": "1",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 550,
        "y": 760,
        "wires": [
            [
                "6a2c3f26726e6a72",
                "d740752d31d6ebdd"
            ]
        ]
    },
    {
        "id": "d740752d31d6ebdd",
        "type": "debug",
        "z": "746eb8d0d4ff58af",
        "name": "debug 275",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 830,
        "y": 760,
        "wires": []
    },
    {
        "id": "6a2c3f26726e6a72",
        "type": "change",
        "z": "746eb8d0d4ff58af",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "emailSent",
                "pt": "global",
                "to": "false",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 660,
        "y": 860,
        "wires": [
            []
        ]
    },
    {
        "id": "1197ad5e0f88aca4",
        "type": "group",
        "z": "746eb8d0d4ff58af",
        "name": "EMAIL CONTACT FORM",
        "style": {
            "label": true
        },
        "nodes": [
            "4938c21e100e2f6d",
            "56509d4818c32a40",
            "5718d1032588488b",
            "394149370030dd08",
            "9c18edc8d4bda1d3",
            "f6689311da10032c",
            "5051c0ea40a38dc8"
        ],
        "x": 14,
        "y": 979,
        "w": 972,
        "h": 142
    },
    {
        "id": "4938c21e100e2f6d",
        "type": "ui_form",
        "z": "746eb8d0d4ff58af",
        "g": "1197ad5e0f88aca4",
        "name": "Monitoring Email",
        "label": "",
        "group": "b81c1409f706d23d",
        "order": 3,
        "width": 6,
        "height": 1,
        "options": [
            {
                "label": "Contact-Email I",
                "value": "Email1",
                "type": "email",
                "required": true,
                "rows": null
            },
            {
                "label": "Contact-Email II",
                "value": "Email2",
                "type": "email",
                "required": false,
                "rows": null
            },
            {
                "label": "Contact-Email III",
                "value": "Email3",
                "type": "email",
                "required": false,
                "rows": null
            },
            {
                "label": "Contact-Email IV",
                "value": "Email4",
                "type": "email",
                "required": false,
                "rows": null
            },
            {
                "label": "Interval [min]",
                "value": "Interval",
                "type": "number",
                "required": true,
                "rows": null
            },
            {
                "label": "OFF/ON",
                "value": "EmailOnOff",
                "type": "switch",
                "required": true,
                "rows": null
            }
        ],
        "formValue": {
            "Email1": "",
            "Email2": "",
            "Email3": "",
            "Email4": "",
            "Interval": "",
            "EmailOnOff": false
        },
        "payload": "",
        "submit": "submit",
        "cancel": "cancel",
        "topic": "topic",
        "topicType": "msg",
        "splitLayout": false,
        "className": "",
        "x": 590,
        "y": 1020,
        "wires": [
            [
                "56509d4818c32a40",
                "5718d1032588488b",
                "5051c0ea40a38dc8"
            ]
        ]
    },
    {
        "id": "56509d4818c32a40",
        "type": "debug",
        "z": "746eb8d0d4ff58af",
        "g": "1197ad5e0f88aca4",
        "name": "debug 277",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 810,
        "y": 1020,
        "wires": []
    },
    {
        "id": "5718d1032588488b",
        "type": "change",
        "z": "746eb8d0d4ff58af",
        "g": "1197ad5e0f88aca4",
        "name": "save saveformEmail",
        "rules": [
            {
                "t": "set",
                "p": "saveformEmail",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 580,
        "y": 1080,
        "wires": [
            [
                "4938c21e100e2f6d",
                "d95b014856b0098c"
            ]
        ]
    },
    {
        "id": "394149370030dd08",
        "type": "ui_ui_control",
        "z": "746eb8d0d4ff58af",
        "g": "1197ad5e0f88aca4",
        "name": "",
        "events": "all",
        "x": 100,
        "y": 1020,
        "wires": [
            [
                "9c18edc8d4bda1d3"
            ]
        ]
    },
    {
        "id": "9c18edc8d4bda1d3",
        "type": "switch",
        "z": "746eb8d0d4ff58af",
        "g": "1197ad5e0f88aca4",
        "name": "",
        "property": "name",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "PUMP A",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 230,
        "y": 1020,
        "wires": [
            [
                "f6689311da10032c"
            ]
        ]
    },
    {
        "id": "f6689311da10032c",
        "type": "change",
        "z": "746eb8d0d4ff58af",
        "g": "1197ad5e0f88aca4",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "saveformEmail",
                "tot": "flow"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 380,
        "y": 1020,
        "wires": [
            [
                "4938c21e100e2f6d"
            ]
        ]
    },
    {
        "id": "5051c0ea40a38dc8",
        "type": "change",
        "z": "746eb8d0d4ff58af",
        "g": "1197ad5e0f88aca4",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "emailSent",
                "pt": "global",
                "to": "false",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 860,
        "y": 1080,
        "wires": [
            []
        ]
    },
    {
        "id": "b81c1409f706d23d",
        "type": "ui_group",
        "name": "E_CONTACT",
        "tab": "4ad3c40739cbf998",
        "order": 3,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "4ad3c40739cbf998",
        "type": "ui_tab",
        "name": "PUMP A",
        "icon": "dashboard",
        "order": 3,
        "disabled": false,
        "hidden": false
    }
]

That all seems rather complicated with flow and context storage.
On the face of it I'd say something like this should suffice.

But who's getting the email?
I'd be pretty annoyed if my computer sent me an email "The sky is falling" every few minutes.
If it's another computer process, is MQTT a viable alternative to email?

thank you for the answer.

the email is for me, if my pump has an error. i thought an interval where i can decide of often an email will be send, when an error occurs would be a nice idea

i send the data of my pump to an MQTT server

Apologies, not an answer to your actual question.

However, a lot of us use Telegram bots for this rather than email.

Have a look at the trigger node. That has a mode where it can repeat a message at a rate that can be passed in the message, and can then be reset when you want it to stop.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.