# node-red-contrib-error-on-timeout

Node red contribution node that triggers an error if a second message has not been received within a configurable timeframe.

![Example usage](https://git.maxorganic.vn/token/node-red-contrib-error-on-timeout/-/raw/82213f993a96e9080290b079e4f0f73896bb39c1/example.png "Example usage")

## Usage

This node receives at first a message with the `eot_trigger` property set and then starts the delay until **a message** with the `eot_input` flag arrives.

---

## Warning

This node is **not suitable for high frequency message processing**. It is meant to help control the flow by checking a `command / acknowledgement` pattern such as switching equipment on and off. [See my use case](#my-use-case). 

### Dead lock

Once the node receives a message with the `eot_trigger` flag set, it'll be **waiting for a message with the `eot_input` flag** and won't accept any more trigger messages untile the configured timeout has been triggered.

---

## Outputs

1) Success output: All messages that have arrived **within** the configured timeframe
2) Error output: When no message arrives within the configured timeframe
3) Late output: All messages that arrived **after** the configured timeframe

---

## Programmatic configuration of the timeout interval

The message with the `eot_trigger` property can have a `eot_timeout` property set to the desired interval in milliseconds.

---

## Latency stats

The following properties/fields are added to the messages coming out of each individual output of the node:

| Output   	| Fields |
|---	|---	|
| Success | <ul><li>`eot_timeout` Configured timeout in milliseconds</li><li>`eot_time` Time in milliseconds it took until the expected message arrived</li></ul> |
| Error | <ul><li>`topic` is set to `error-on-timeout`</li><li>`payload` Formatted error message reading `Input message not received within ${timeout} ms`</li><li>`eot_trigger` A copy of the message that triggered the timer that resulted in the timeout</ul> |
| Late | <ul><li>`eot_timeout` Configured timeout in milliseconds</li><li>`eot_time` Time in milliseconds it took until the late message arrived</li><li>`eot_late` Late time in milliseconds</li></ul> |

---

## My use case

I'm working on an IoT project running on MQTT where successfull execution of commands is crucial. After sending a command to a device I needed to ensure that the command has been executed successfully by having the device send out an `ack` message within a certain timeframe before turning on other heavy duty equipment (and potentially blow up the entire infrastructure).

```mermaid
sequenceDiagram
    Actor Node Red
    Actor Selenoid
    Actor Pump
    Node Red->>Selenoid: Open
    Selenoid->>Node Red: ACK
    Node Red->>Pump: Run
    Pump->>Node Red: ACK
```

The default nodes that come with Node Red seemed to be too... redundant for my needs and I preferred to develop something simple and concise and wrap it into a node.

I sincerely do hope that you make good use of it.

---

Please see the examples by importing them through the **Import > Examples** dialog.
