Probleme mit Zeitbasiertem Inject (Problems with time-based injection)

Hallo,

ich habe in meinem Projekt mehrere Timer über Inject, die zu verschiedenen Zeiten eine Aktion auslösen.
Dabei gibt es 10-Sekunden-, 30-Sekunden- und 5-Minuten-Timer.
Ich prüfe darüber auch, ob verschiedene Geräte im Netzwerk online sind.

Ob der Timer auch alle X Sekunden auslöst, lasse ich mir mittels eines Triggers und eines LED-Nodes auf dem Dashboard anzeigen.

Z. B. löst ein 10-Sekunden-Inject einen Trigger aus, der 10 Sekunden lang einen LED-Node ansteuert.
Nun passiert es aber, dass der Inject sehr häufig nicht alle 10 Sekunden auslöst und mir der Status anzeigt, dass der Timer nicht gekommen ist.
Daraufhin habe ich den Trigger hochgesetzt, sodass dieser mir den Status nicht 10 Sekunden lang als „Aktiv“ anzeigt, sondern 20 Sekunden lang.

Trotzdem passiert es, dass der Inject nicht genau nach 10 Sekunden auslöst.

Dieses Problem habe ich mit so gut wie allen Timern.
Habe ich einen Denkfehler oder wo liegt hierbei das Problem ?

Node Red läuft auf Proxmox in einem LXC Container, benötigt im schnitt etwa 0,1% CPU Last und 17% von 1GB zugewiesenem Speicher.
Somit würde ich die Auslastung komplett ausschliessen.
Node.js version 22.23.2
Node-Red v 5.0.4

[
    {
        "id": "d33aa40a0de714f9",
        "type": "inject",
        "z": "15a3cde9224b823f",
        "g": "1e82dcd0a9a719b4",
        "name": "Initialisierung (10 sec.)",
        "props": [],
        "repeat": "10",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "x": 210,
        "y": 240,
        "wires": [
            [
                "34e98ca2bf555973",
                "ff688fe32314a8e9"
            ]
        ]
    },
    {
        "id": "34e98ca2bf555973",
        "type": "link out",
        "z": "15a3cde9224b823f",
        "g": "1e82dcd0a9a719b4",
        "name": "Timer (10 sec.)",
        "mode": "link",
        "links": [
            "685175d673321716",
            "7dbe99004fab5ad4"
        ],
        "x": 375,
        "y": 220,
        "wires": []
    },
    {
        "id": "ff688fe32314a8e9",
        "type": "trigger",
        "z": "15a3cde9224b823f",
        "g": "1e82dcd0a9a719b4",
        "name": "20s Delay",
        "op1": "true",
        "op2": "false",
        "op1type": "bool",
        "op2type": "bool",
        "duration": "20",
        "extend": false,
        "overrideDelay": false,
        "units": "s",
        "reset": "",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 1,
        "x": 500,
        "y": 240,
        "wires": [
            [
                "de9716f13948a8e1"
            ]
        ]
    },
    {
        "id": "de9716f13948a8e1",
        "type": "ui_led2",
        "z": "15a3cde9224b823f",
        "g": "1e82dcd0a9a719b4",
        "order": 4,
        "group": "98e58b62b797c9a2",
        "width": 4,
        "height": 1,
        "label": "Timer 10 Sekunden",
        "property": "payload",
        "labelPlacement": "right",
        "labelAlignment": "left",
        "colorForValue": [
            {
                "color": "#ff0000",
                "value": "false",
                "valueType": "bool"
            },
            {
                "color": "#008000",
                "value": "true",
                "valueType": "bool"
            }
        ],
        "allowColorForValueInMessage": false,
        "shape": "circle",
        "showGlow": true,
        "name": "Timer 10 Sekunden",
        "x": 670,
        "y": 240,
        "wires": []
    },
    {
        "id": "98e58b62b797c9a2",
        "type": "ui_group",
        "name": "Dienste & Timer",
        "tab": "e31e7bc2a2fd5981",
        "order": 2,
        "disp": true,
        "width": "8",
        "collapse": false,
        "className": ""
    },
    {
        "id": "e31e7bc2a2fd5981",
        "type": "ui_tab",
        "name": "Statistik",
        "icon": "power",
        "order": 2,
        "disabled": false,
        "hidden": false
    },
    {
        "id": "d7e5ecd7df0312bf",
        "type": "global-config",
        "env": [],
        "modules": {
            "node-red-contrib-ui-led2": "0.4.22",
            "node-red-dashboard": "3.6.6"
        }
    }
]

Translation


Hello,

In my project, I use several "inject" nodes as timers to trigger actions at various intervals—specifically 10-second, 30-second, and 5-minute timers. I also use these to check whether various devices on the network are online.

To verify that these timers are firing as expected, I use a "trigger" node connected to an "LED" node on the dashboard.

For example, a 10-second "inject" node activates a "trigger" node, which then sets an "LED" node to an "active" state for 10 seconds.
However, the "inject" node frequently fails to fire every 10 seconds, and the status indicator shows that the timer did not trigger.
In response, I extended the duration of the "trigger" node so that it displays an "active" status for 20 seconds instead of 10.

Even so, the "inject" node still fails to fire exactly every 10 seconds.

I am experiencing this issue with almost all of my timers.
Am I misunderstanding how this works, or where might the problem lie?

Node-RED is running in an LXC container on Proxmox, with an average CPU load of about 0.1% and memory usage at 17% of the 1GB allocated.
Therefore, I think I can safely rule out system resource constraints as the cause.
Node.js version: 22.23.2
Node-RED version: 5.0.4

Welcome to the forums @DBProgger,

I have translated your post – most of the forum members are English speakers, so where possible, it would be great if you could translate.

Are you expecting "exactly" 10s intervals, with high accuracy?

I'm no JavaScript engine expert, but I don't think you'll get a constant 10s. It will naturally swing by a few milliseconds, potentially more.

JavaScript is single-threaded, so if your Node-RED flow is doing work, the interval may swing depending on how busy the event loop is.


I did load your flow, and have 1 suggestion.
Set the wait for 15s + extend on each message.

This will allow the LED to remain on, and only go out if the inject hasn't fired within 15s of the last message (unsure of any reason why this will happen) - It's A kind of "Alive" LED if you will.

here I am using a 5s tolerance (based on your 10s requirement)

Also Note: setting the trigger to send a 2nd message at the same interval my cause a race condition, I would set some off-set between the 2 if you really need to.

Hi,

thanks for translating! I wasn't quite sure about the language, as I've already read a lot of German posts here.

Initially, I also set the Trigger to 10 seconds, just like the Inject node.
As you mentioned, it's supposed to act as an "alive" function.

I intentionally left "Extend delay if new messages arrive" unchecked because I didn't want an input buffer that might eventually keep the LED turned on permanently.

The Inject node is set to 10 seconds anyway, and the Trigger to 20, so that the LED stays on for 10 seconds longer.
However, it still turns off at some point and stays off for at least one full interval.
This doesn't seem to be a matter of just a few milliseconds, but rather entire intervals.

@DBProgger Place a debug on the inject node to confirm it fires every 10 seconds. Setting the trigger to 20 seconds will mean every other inject at 10 secs is ignored.
@marcus-j-davies clicked your translation thinking it was the OG post, sorry.

@DBProgger :slight_smile:
You can try this:

const P = 10000;   // Periode in ms

// erstes Ziel: die übernächste volle 10-s-Marke (genug Vorlauf)
let ziel = Math.ceil(Date.now() / P) * P + P;

function tick() {
    const t = Date.now();
    node.send({
        payload: t,
        zeit: new Date(t).toLocaleTimeString('de-DE'),
        abweichung_ms: t - ziel          // wie spät dieser Tick kam
    });

    ziel += P;
    // nach einem längeren Hänger nicht nachfeuern, sondern neu einrasten
    if (ziel <= Date.now()) ziel = Math.ceil(Date.now() / P) * P + P;

    context.set('timer', setTimeout(tick, ziel - Date.now()));
}

context.set('timer', setTimeout(tick, ziel - Date.now()));

In tab STOP insert this:
clearTimeout(context.get('timer'));

Thanks, that's a really good idea, and I tried it right away.
In doing so, I saw that the inject actually starts properly,
but probably due to the delay in the trigger and minimal deviations, it keeps happening
that the trigger, which is still running, "misses" the inject by a few milliseconds.

I have now simply made the trigger 1 second shorter than the inject.
As a result, the "Alive" LED briefly flashes red, but it works better.

@mssib28
Thanks for the script, I'll keep it in mind in case there are still problems or I don't like the flashing.

You should use @marcus-j-davies suggestion of extend delay if new message arrives.
As what you think, will not happen.

The trigger will reset to 20 seconds on each new inject and as soon as a message gap of 20 seconds happens the led will turn off.