Skip to content

Incrementing a value

cflurin edited this page Oct 8, 2018 · 6 revisions

Incrementing a value can be used e.g. to turn on a light incrementing the brightness from 0 to 100 by a step of 10.

Note: version 0.12.1 or newer is required.

increment

Configuration

{
    "currentState": "stopped",
    "states": {
        "stopped": {
            "start": "started"
        },
        "started": {
            "run": "running",
            "end": "stopped"
        },
        "running": {
            "run": "running",
            "end": "stopped"
        }
    },
    "data": {
      "step": 10
    },
    "methods": {
        "start":  [
            "if (sm.currentState === 'started') {",
            "   node.send(msg);",
            "   resume('run', msg);",
            "}"
        ],
        "run": [
            "if (msg.payload.brightness < 100) {",
            "   msg.payload.brightness = msg.payload.brightness + sm.data.step;",
            "   if (msg.payload.brightness > 100) msg.payload.brightness=100;",
            "   sm.tr = 'run';",
            "} else {",
            "   sm.tr = 'end';",
            "}",
            "timeout.interval = setTimeout(function() {",
            "   if (sm.tr === 'run') node.send(msg);",
            "   resume(sm.tr, msg);",
            "}, 1000);"
        ],
        "onAfterTransition": "output=false;",
        "status": {
            "fill": {
                "get": "sm.currentState === 'running' ? 'green' : 'grey'"
            },
            "shape": "dot",
            "text": {
                "get": "sm.currentState"
            }
        }
    }
}

Flow

[{"id":"9874ce75.a457f","type":"inject","z":"895bbff8.0e6c4","name":"","topic":"start","payload":"{\"brightness\": 0}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":1660,"wires":[["b071523a.91c04"]]},{"id":"602893b2.cdb4ec","type":"debug","z":"895bbff8.0e6c4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","x":530,"y":1660,"wires":[]},{"id":"b071523a.91c04","type":"dsm","z":"895bbff8.0e6c4","name":"increment","sm_config":"{\n    \"currentState\": \"stopped\",\n    \"states\": {\n        \"stopped\": {\n            \"start\": \"started\"\n        },\n        \"started\": {\n            \"run\": \"running\",\n            \"end\": \"stopped\"\n        },\n        \"running\": {\n            \"run\": \"running\",\n            \"end\": \"stopped\"\n        }\n    },\n    \"data\": {\n      \"step\": 10\n    },\n    \"methods\": {\n        \"start\":  [\n            \"if (sm.currentState === 'started') {\",\n            \"   node.send(msg);\",\n            \"   resume('run', msg);\",\n            \"}\"\n        ],\n        \"run\": [\n            \"if (msg.payload.brightness < 100) {\",\n            \"   msg.payload.brightness = msg.payload.brightness + sm.data.step;\",\n            \"   if (msg.payload.brightness > 100) msg.payload.brightness=100;\",\n            \"   sm.tr = 'run';\",\n            \"} else {\",\n            \"   sm.tr = 'end';\",\n            \"}\",\n            \"timeout.interval = setTimeout(function() {\",\n            \"   if (sm.tr === 'run') node.send(msg);\",\n            \"   resume(sm.tr, msg);\",\n            \"}, 1000);\"\n        ],\n        \"onAfterTransition\": \"output=false;\",\n        \"status\": {\n            \"fill\": {\n                \"get\": \"sm.currentState === 'running' ? 'green' : 'grey'\"\n            },\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"sm.currentState\"\n            }\n        }\n    }\n}","x":370,"y":1660,"wires":[["602893b2.cdb4ec"]]}]
Clone this wiki locally