# Node-red-contrib-dsm

**URL:** <https://discourse.nodered.org/t/node-red-contrib-dsm/1388>\
**Category:** Share Your Nodes\
**Created:** [6 July 2018 04:26 UTC](https://discourse.nodered.org/t/node-red-contrib-dsm/1388 "2018-07-06T04:26:25Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [6 July 2018 04:26 UTC](https://discourse.nodered.org/t/node-red-contrib-dsm/1388/1 "2018-07-06T04:26:25Z")

</div>

### New version 0.4.1

The new version supports data and methods. The new feature can be used to set|get local variables (context). This example shows a practical use case:

 ![dsm5b](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c0b077889a4ed9b393f3c679de3d1480ba0b657c.jpeg)

### Configuration

```auto
{
    "data": {
        "delay": 2000
    },
    "methods": {
        "set": "output=false;",
        "setDelay": "sm.data.delay = msg.payload;output=false;",
        "getDelay": "msg.delay = sm.data.delay;"
    }
}

```

### Flow

```auto
[{"id":"aad010ae.0a331","type":"comment","z":"57ef434a.058a6c","name":"Set|get delay to|from context (local variable) _______________________________________","info":"","x":320,"y":2520,"wires":[]},{"id":"2103589f.f4ca68","type":"dsm","z":"57ef434a.058a6c","name":"","x":370,"y":2640,"wires":[["6d6468dd.45fd88"]]},{"id":"b515039c.868a4","type":"inject","z":"57ef434a.058a6c","name":"","topic":"setDelay","payload":"3000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":2620,"wires":[["2103589f.f4ca68"]]},{"id":"a9267621.f4dfa8","type":"inject","z":"57ef434a.058a6c","name":"","topic":"getDelay","payload":"delayed msg","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":2700,"wires":[["2103589f.f4ca68"]]},{"id":"1c7b4650.41cb3a","type":"inject","z":"57ef434a.058a6c","name":"","topic":"setDelay","payload":"4000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":111,"y":2658,"wires":[["2103589f.f4ca68"]]},{"id":"6d6468dd.45fd88","type":"delay","z":"57ef434a.058a6c","name":"","pauseType":"delayv","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":520,"y":2640,"wires":[["4b822dd8.f6c324"]]},{"id":"4b822dd8.f6c324","type":"debug","z":"57ef434a.058a6c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":690,"y":2640,"wires":[]},{"id":"7bf8cf38.8cfe3","type":"inject","z":"57ef434a.058a6c","name":"set dsm","topic":"set","payload":"{\"data\":{\"delay\":2000},\"methods\":{\"set\":\"output=false;\",\"setDelay\":\"sm.data.delay = msg.payload;output=false;\",\"getDelay\":\"msg.delay = sm.data.delay;\"}}","payloadType":"json","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":120,"y":2580,"wires":[["2103589f.f4ca68"]]}]

```

Consult GitHub for more details: [https://github.com/cflurin/node-red-contrib-dsm](https://github.com/cflurin/node-red-contrib-dsm)  
You'll find more use cases in the Wiki : [https://github.com/cflurin/node-red-contrib-dsm/wiki](https://github.com/cflurin/node-red-contrib-dsm/wiki)

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [6 July 2018 14:17 UTC](https://discourse.nodered.org/t/node-red-contrib-dsm/1388/2 "2018-07-06T14:17:59Z")

</div>

Hi @cflurin, awesome node indeed. Just a remark about the CO2 example in the wiki page. A warning error is displayed when we inject the value 900. I believe this is due to topic not being correctly set in the change node. I think it is necessary to revise the jsonata expression to cover the range between 800 and 1000. Here below what I see when adding a debug node after the change node. Once again thank you for the dsm node.

![dsm-error](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/9c9bd60fb8178942bc5638e064b49d8940e90653.PNG)

---

<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:** [6 July 2018 14:54 UTC](https://discourse.nodered.org/t/node-red-contrib-dsm/1388/3 "2018-07-06T14:54:54Z")

</div>

Thanks for your feedback. Correct there is an issue in the change node. Please change the JSONata expression to:

```auto
(
$v := $number(payload.value);
($v < 800) ? "cross_low" :
($v > 1000) ? "cross_high" : "inter" 
)

```

I'll update the flow on GitHub and add a check to the dsm node.

Edit: I'm testing the CO2 example, it doesn't work correctly with the new version 0.4.1.

---

<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:** [6 July 2018 17:21 UTC](https://discourse.nodered.org/t/node-red-contrib-dsm/1388/4 "2018-07-06T17:21:17Z")

</div>

I've published a new version 0.4.2. This should fix the bug.

---

<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:** [5 November 2018 09:24 UTC](https://discourse.nodered.org/t/node-red-contrib-dsm/1388/8 "2018-11-05T09:24:13Z")

</div>

moved from [How can i build a random delayed trigger loop?](https://discourse.nodered.org/t/how-can-i-build-a-random-delayed-trigger-loop/3912/10)

I know, generally the state machine approach isn't easy to understand. That's the reason the node is in the `Advanced Category`. 🙂

One way to learn using the dsm node is to import the examples, there are more then 25 examples in the [Wiki](https://github.com/cflurin/node-red-contrib-dsm/wiki)

I've no plan at the moment to add a config UI, but let's see.

* * *

New version 0.13.0 available.

**Enhancements:**

- preState: the previous state (sm.preState) before the transition is executed.

- preStateOutput: here you can define the msg property e.g. `previous` =\> msg.previous

- examples: Menu \> Import \> Examples \> dsm

To start you can import the `skeleton` example.

Feedback and PR's are welcome.
