Hold status for x seconds

Hi everyone,

I'm begineer....:slight_smile:

I need an info....

I've two node (iot sensors) with boolean output (updated every seconds) that pilot raspberry GPIO.
I need that when one of this sensor goes true, the true state stay true for x seconds.

The problem is that if one sensor goes true, the other one send false and reset the output.
Any ideas?

Thanks for your help.

Hi,
Maybe the following, add a function node between your two nodes and the GPIO node

const output = context.get('output') || false;

if (msg.payload === true && !output) {
  context.set('output', true);
  node.send({ payload: true });
  // Inject false after 10s
  setTimeout(() => {
    context.set('output', false);
    node.send({ payload: false });
  }, 10000); // <- adjust time here in ms
}

When one of the two nodes sends true it sends a true message and triggers a timeout of 10s which will send false.

Thanks a lot!!!!!!!

As a beginner, i would recommend you get to know the nodes a bit better before reaching for a function node.

Here is another approach:

chrome_UXEFui7byf

Demo flow
Use CTRL+I to import

[{"id":"c1d40a844369be8f","type":"inject","z":"e0236af69e824001","name":"Fake Sensor 1 true","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":330,"y":160,"wires":[["a45425b2a93f587f"]]},{"id":"3af7aefde8bc24b7","type":"inject","z":"e0236af69e824001","name":"Fake Sensor 1 false","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":330,"y":200,"wires":[["a45425b2a93f587f"]]},{"id":"94cd9a8bf54d4059","type":"inject","z":"e0236af69e824001","name":"Fake Sensor 2 true","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":330,"y":300,"wires":[["a45425b2a93f587f"]]},{"id":"026ebcf939ce4106","type":"inject","z":"e0236af69e824001","name":"Fake Sensor 2 false","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":330,"y":340,"wires":[["a45425b2a93f587f"]]},{"id":"a45425b2a93f587f","type":"switch","z":"e0236af69e824001","name":"is true?","property":"payload","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":560,"y":240,"wires":[["1373dc1ab9abfef5"]]},{"id":"3efc628fc449d21c","type":"debug","z":"e0236af69e824001","name":"to next node ->","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":740,"y":360,"wires":[]},{"id":"1373dc1ab9abfef5","type":"trigger","z":"e0236af69e824001","name":"send false after 10s (extend if more arrive)","op1":"","op2":"false","op1type":"pay","op2type":"bool","duration":"10","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":660,"y":300,"wires":[["3efc628fc449d21c"]]}]
3 Likes

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

thanks!!!