# Detect if device is online / offline

**URL:** <https://discourse.nodered.org/t/detect-if-device-is-online-offline/14086>\
**Category:** General\
**Created:** [7 August 2019 09:46 UTC](https://discourse.nodered.org/t/detect-if-device-is-online-offline/14086 "2019-08-07T09:46:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ch3f](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@ch3f](https://discourse.nodered.org/u/ch3f)\
**Post date:** [7 August 2019 09:46 UTC](https://discourse.nodered.org/t/detect-if-device-is-online-offline/14086/1 "2019-08-07T09:46:52Z")

</div>

I'm using the node-red-node-serialport to read and send data to serial and USB ports. How can I react on disconnection of such a device? I'm looking for something like: If device A is currently connected (online), make something, if not (offline) make something else.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 August 2019 11:20 UTC](https://discourse.nodered.org/t/detect-if-device-is-online-offline/14086/2 "2019-08-07T11:20:20Z")

</div>

Which device? Serial or usb?  
Are you asking it for data? If so what happens if it is offline?

---

<div class="post-metadata">

**Author:** ![ch3f](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@ch3f](https://discourse.nodered.org/u/ch3f)\
**Post date:** [7 August 2019 11:30 UTC](https://discourse.nodered.org/t/detect-if-device-is-online-offline/14086/3 "2019-08-07T11:30:18Z")

</div>

These are serial sensors connected via USB-Converters. Some of them I have to ask constantly to send me data. Others are satisfied with a one-time request to send me constantly data. If the device gets offline I have to send this one-time request again as soon as the device gets online. So I want to check if the device is currrently online or offline.

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [7 August 2019 13:36 UTC](https://discourse.nodered.org/t/detect-if-device-is-online-offline/14086/4 "2019-08-07T13:36:33Z")

</div>

save a timestamp with every msg received. Create another flow that checks the time difference between msg arrived and now. if difference is to big send new request.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 August 2019 15:04 UTC](https://discourse.nodered.org/t/detect-if-device-is-online-offline/14086/5 "2019-08-07T15:04:36Z")

</div>

Feed the messages from each sensor through a Trigger node (one for each sensor), set to initially send nothing, and send a message after the specified interval. The result will be that provided the messages keep coming in at the expected rate then the Trigger node will be continually reset and will never send its message. If the sensor stops then the trigger will timeout and send its message, which you can use to take whatever action is required. You may need an inject node set to inject once on startup to hit the trigger initially on startup in case the sensor never responds to its first request.

**[Edit]** This flow pattern is described in one of the examples in the cookbook:  
[https://cookbook.nodered.org/basic/trigger-timeout](https://cookbook.nodered.org/basic/trigger-timeout)

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [7 August 2019 16:35 UTC](https://discourse.nodered.org/t/detect-if-device-is-online-offline/14086/6 "2019-08-07T16:35:26Z")

</div>

Trigger is the best solution. Thanks for the tip!

---

<div class="post-metadata">

**Author:** ![ch3f](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@ch3f](https://discourse.nodered.org/u/ch3f)\
**Post date:** [9 August 2019 07:56 UTC](https://discourse.nodered.org/t/detect-if-device-is-online-offline/14086/7 "2019-08-09T07:56:08Z")

</div>

Thanks that almost solved my problem.  
Is it possible to set the intervall by a message? I can set the intervalls of the sensors dynamically and want therefore to set the trigger seconds dynamically.
