Skip to content

Random trigger

cflurin edited this page Nov 4, 2018 · 2 revisions

random_trigger

Configuration

{
    "currentState": "stopped",
    "states": {
        "stopped": {
            "start": "running"
        },
        "running": {
            "running": "running",
            "stop": "stopped"
        }
    },
    "data": {
        "minDelay": 1,
        "maxDelay": 5
    },
    "methods": {
        "start": [
            "clearTimeout(timeout.delay);",            
            "resume('running', msg);"
        ],
        "running": [
            "sm.delay = Math.random() * (sm.data.maxDelay - sm.data.minDelay) + sm.data.minDelay;",
            "msg.delay = 'delay '+ sm.delay.toFixed(2);",
            "timeout.delay = setTimeout(function() {",
            "   node.send(msg);",
            "   resume('running',msg);",
            "},sm.delay*1000);"
        ],
        "stop": [
            "clearTimeout(timeout.delay);",
            "msg.delay = 'stopped';"
        ],
        "onTransition": [
            "output = false;"
        ],
        "status": {
            "fill": {
                "get": "sm.currentState === 'running' ? 'green' : 'red';"
            },
            "shape": "dot",
            "text": {
                "get": "msg.delay;"
            }
        }
    }
}

Flow

[{"id":"a26cd0b7.90aef","type":"dsm","z":"66646ca8.e97654","name":"random trigger","sm_config":"{\n    \"currentState\": \"stopped\",\n    \"states\": {\n        \"stopped\": {\n            \"start\": \"running\"\n        },\n        \"running\": {\n            \"running\": \"running\",\n            \"stop\": \"stopped\"\n        }\n    },\n    \"data\": {\n        \"minDelay\": 1,\n        \"maxDelay\": 5\n    },\n    \"methods\": {\n        \"start\": [\n            \"clearTimeout(timeout.delay);\",            \n            \"resume('running', msg);\"\n        ],\n        \"running\": [\n            \"sm.delay = Math.random() * (sm.data.maxDelay - sm.data.minDelay) + sm.data.minDelay;\",\n            \"msg.delay = 'delay '+ sm.delay.toFixed(2);\",\n            \"timeout.delay = setTimeout(function() {\",\n            \"   node.send(msg);\",\n            \"   resume('running',msg);\",\n            \"},sm.delay*1000);\"\n        ],\n        \"stop\": [\n            \"clearTimeout(timeout.delay);\",\n            \"msg.delay = 'stopped';\"\n        ],\n        \"onTransition\": [\n            \"output = false;\"\n        ],\n        \"status\": {\n            \"fill\": {\n                \"get\": \"sm.currentState === 'running' ? 'green' : 'red';\"\n            },\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"msg.delay;\"\n            }\n        }\n    }\n}","x":330,"y":320,"wires":[["fdb8b530.787a38"]]},{"id":"c78a24cc.f507f8","type":"inject","z":"66646ca8.e97654","name":"start","topic":"start","payload":"random trigger","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":320,"wires":[["a26cd0b7.90aef"]]},{"id":"fdb8b530.787a38","type":"debug","z":"66646ca8.e97654","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":490,"y":320,"wires":[]},{"id":"894976da.9dfca8","type":"inject","z":"66646ca8.e97654","name":"stop","topic":"stop","payload":"random trigger","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":360,"wires":[["a26cd0b7.90aef"]]}]
Clone this wiki locally