# Help with node-red-contrib-dsm and timeout

**URL:** https://discourse.nodered.org/t/help-with-node-red-contrib-dsm-and-timeout/22609
**Category:** General
**Created:** [4 March 2020 10:31 UTC](https://discourse.nodered.org/t/help-with-node-red-contrib-dsm-and-timeout/22609 "2020-03-04T10:31:28Z")
**Posts on this page:** 12
**Page:** 1

<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: [4 March 2020 10:31 UTC](https://discourse.nodered.org/t/help-with-node-red-contrib-dsm-and-timeout/22609/1 "2020-03-04T10:31:29Z")

</div>

I am trying to get my head round node-red-contrib-dsm and in particular a case where I have a simple situation that the machine cycles through states based on incoming signals, but on one of them I want a timeout such that if the next transition does not occur within 15 seconds that it goes to another state. I have looked at the Blink example but am having difficulty seeing the wood for the trees and hope that a simple example will help me. The machine I have so far is

```auto
{
  "currentState": "Waiting",
  "states": {
      "Waiting": {
          "StartShutdown": "WaitingShutdown"
      },
      "WaitingShutdown": {
          "Offline": "Offline"
      },
      "Offline": {
          "Online": "Waiting"
    }
  }
}

```

and I want to add the condition that if it stays in the WaitingShutdown state for 15 seconds without the Offline signal then it goes to the Waiting state.

---

<div class="post-metadata">

### Author: ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)
#### Post date: [4 March 2020 16:39 UTC](https://discourse.nodered.org/t/help-with-node-red-contrib-dsm-and-timeout/22609/2 "2020-03-04T16:39:27Z")

</div>

what does the input (or inputs) look like?

---

<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: [4 March 2020 17:16 UTC](https://discourse.nodered.org/t/help-with-node-red-contrib-dsm-and-timeout/22609/3 "2020-03-04T17:16:12Z")

</div>

Just the usual topic based signals for the dsm, so the topic is " StartShutdown", "Offline", or "Online" to switch between the states, but if the "Offline" message does not appear while in state WaitingShutdown (which can happen due to other circumstances) then after 15 seconds I want it to transition to the Waiting state.

---

<div class="post-metadata">

### Author: ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)
#### Post date: [4 March 2020 17:32 UTC](https://discourse.nodered.org/t/help-with-node-red-contrib-dsm-and-timeout/22609/4 "2020-03-04T17:32:28Z")

</div>

Is this an academic question in understanding that node or are you trying to accomplish some real world solution? In other words if it was accomplished using a function node does that suffice?

---

<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: [4 March 2020 17:37 UTC](https://discourse.nodered.org/t/help-with-node-red-contrib-dsm-and-timeout/22609/5 "2020-03-04T17:37:33Z")

</div>

I know how to do it with a function node, this is a simplified real world example which is ideally suited to the finite state machine technique. The Blink example in the dsm wiki shows that the timeout can be done fairly easily as the example has much more complex timer features than I need. I just can't quite grasp the required incantations to get it to work. I am sure it is just a few lines of code in the dsm spec.

---

<div class="post-metadata">

### Author: ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)
#### Post date: [4 March 2020 19:43 UTC](https://discourse.nodered.org/t/help-with-node-red-contrib-dsm-and-timeout/22609/6 "2020-03-04T19:43:29Z")

</div>

Hi, this should do the job:

```auto
{
    "currentState": "Waiting",
    "states": {
        "Waiting": {
            "StartShutdown": "WaitingShutdown"
        },
        "WaitingShutdown": {
            "Offline": "Offline",
            "Timeout": "Waiting"
        },
        "Offline": {
            "Online": "Waiting"
        }
    },
    "methods": {
        "StartShutdown": [
            "timeout.id = setTimeout(function() {",
                "resume('Timeout', msg)",
            "},15000);"
        ],
        "status": {
            "fill": "green",
            "shape": "dot",
            "text": {
                "get": "sm.currentState;"
            }
        }
    }
}

```

---

<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: [4 March 2020 20:40 UTC](https://discourse.nodered.org/t/help-with-node-red-contrib-dsm-and-timeout/22609/7 "2020-03-04T20:40:46Z")

</div>

Ah right, I see, of course. You just need to start a timer with the same event that switches to the state and get it do the state transition when the timer runs down. If there were any danger of it getting back round to the WaitingShutdown state (via the normal route) before the timer had run down then presumably I should reset the timer on the Offline event.  
Many thanks indeed.

---

<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: [17 March 2020 16:38 UTC](https://discourse.nodered.org/t/help-with-node-red-contrib-dsm-and-timeout/22609/8 "2020-03-17T16:38:18Z")

</div>

Trying to generalise this a bit, as I want to run the timer on every state change, I have tried

```auto
 "methods": {
        "onTransition": [
            "if (timeout.id) clearTimeout(timeout.id);",
            "timeout.id = setTimeout(function() {",
                "resume('Timeout', msg)",
                "timeout.id = 0",
            "},5000);"
        ]
}

```

which works perfectly until I add the line `timeout.id = 0` and then I get  
`[error] [dsm:Presence/Armed] SyntaxError: Unexpected identifier`  
It seemed a good idea to do that, along with the test at the start when clearing the timeout, in order to make sure timeout.id is a valid timer, but I must be missing something.

---

<div class="post-metadata">

### Author: ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)
#### Post date: [17 March 2020 17:34 UTC](https://discourse.nodered.org/t/help-with-node-red-contrib-dsm-and-timeout/22609/9 "2020-03-17T17:34:18Z")

</div>

missing `;`

```auto
"resume('Timeout', msg);",

```

Note: the function is one string! so you have to separate every statement.

---

<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: [17 March 2020 18:04 UTC](https://discourse.nodered.org/t/help-with-node-red-contrib-dsm-and-timeout/22609/10 "2020-03-17T18:04:21Z")

</div>

Doh, of course, that's what comes from getting used to taking shortcuts.  
Thanks.

---

<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: [21 March 2020 10:03 UTC](https://discourse.nodered.org/t/help-with-node-red-contrib-dsm-and-timeout/22609/11 "2020-03-21T10:03:57Z")

</div>

Is there a way to implement a timeout on the initial state, so for instance something like

```auto
{
    "currentState": "Waiting",
    "states": {
        "Waiting": {
            "StartShutdown": "WaitingShutdown",
            "Timeout": "Offline"
        },
...
}

```

I can't use `onTransition` or a specific event method to start the timer as there is no transition or event to get to the initial state.  
I have a work around using an extra state `Initialising` which defines a transition to `Waiting` and use an Inject node firing on startup to kick it into life, but it isn't pretty.

---

<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: [4 April 2020 10:04 UTC](https://discourse.nodered.org/t/help-with-node-red-contrib-dsm-and-timeout/22609/12 "2020-04-04T10:04:05Z")

</div>

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