Node-red-contrib-dsm


#1

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:

Configuration

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

Flow

[{"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
You'll find more use cases in the Wiki : https://github.com/cflurin/node-red-contrib-dsm/wiki


How can i build a random delayed trigger loop?
#2

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


#3

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

(
$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.


#4

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


#8

moved from How can i build a random delayed trigger loop?

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

One way to learn using the dsm node is to import the examples, there are more then 25 examples in the 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.