Rising edge counter - cflurin/node-red-contrib-dsm GitHub Wiki

rising-edge-counter

Configuration

{
    "data": {
        "counter": 0
    },
    "methods": {
    "reset": "sm.data.counter = 0;",
    "onTransition": [
        "if (msg.payload === 1) {",
        "   sm.data.counter++;",
        "   msg.payload = sm.data.counter;",
        "   output = true;",
        "}"
    ],
    "status": {
        "fill": "blue",
        "shape": "dot",
        "text": {
            "get": "sm.data.counter;"
            }
        }
    }
}

Flow

[{"id":"c4db8bc7.2f5ce8","type":"dsm","z":"895bbff8.0e6c4","name":"rising-edge counter","sm_config":"{\n    \"data\": {\n        \"counter\": 0\n    },\n\t\"methods\": {\n\t    \"reset\": \"sm.data.counter = 0;\",\n\t    \"onTransition\": [\n\t        \"if (msg.payload === 1) {\",\n            \"   sm.data.counter++;\",\n            \"   msg.payload = sm.data.counter;\",\n            \"   output = true;\",\n            \"}\"\n        ],\n\t    \"status\": {\n\t        \"fill\": \"blue\",\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"sm.data.counter;\"\n            }\n        }\n\t}\n}","x":340,"y":3720,"wires":["c405d790.5d05b8"](/cflurin/node-red-contrib-dsm/wiki/"c405d790.5d05b8")},{"id":"37b9f905.e18f96","type":"inject","z":"895bbff8.0e6c4","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":3740,"wires":["c4db8bc7.2f5ce8"](/cflurin/node-red-contrib-dsm/wiki/"c4db8bc7.2f5ce8")},{"id":"c405d790.5d05b8","type":"debug","z":"895bbff8.0e6c4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":530,"y":3720,"wires":[]},{"id":"b6fa6979.28dbf8","type":"inject","z":"895bbff8.0e6c4","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":3700,"wires":["c4db8bc7.2f5ce8"](/cflurin/node-red-contrib-dsm/wiki/"c4db8bc7.2f5ce8")},{"id":"19164333.2f75ad","type":"inject","z":"895bbff8.0e6c4","name":"reset counter","topic":"reset","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":3660,"wires":["c4db8bc7.2f5ce8"](/cflurin/node-red-contrib-dsm/wiki/"c4db8bc7.2f5ce8")}]

Expanded configuration with contextStorage (counter persists across restarts)

Configuration

{
    "data": {
        "counter": 0
    },
    "methods": {
        "init": [
            "sm.data.counter = context.get('counter') || 0;",
            "sta.fill = 'blue';",
            "sta.text = sm.data.counter;"
        ],
        "reset": [
            "sm.data.counter = 0;",
            "context.set('counter', sm.data.counter);"
        ],
        "onTransition": [
            "if (msg.payload === 1) {",
            "   sm.data.counter++;",
            "   context.set('counter', sm.data.counter);",
            "   msg.payload = sm.data.counter;",
            "   output = true;",
            "}"
        ],
        "status": {
            "fill": "blue",
            "shape": "dot",
            "text": {
                "get": "sm.data.counter;"
            }
        }
    }
}

Flow

[{"id":"1b8f0a20.7be0e6","type":"dsm","z":"6ff0723.8c6b78c","name":"rising-edge counter","sm_config":"{\n    \"data\": {\n        \"counter\": 0\n    },\n    \"methods\": {\n        \"init\": [\n            \"sm.data.counter = context.get('counter') || 0;\",\n            \"sta.fill = 'blue';\",\n            \"sta.text = sm.data.counter;\"\n        ],\n        \"reset\": [\n            \"sm.data.counter = 0;\",\n            \"context.set('counter', sm.data.counter);\"\n        ],\n        \"onTransition\": [\n            \"if (msg.payload === 1) {\",\n            \"   sm.data.counter++;\",\n            \"   context.set('counter', sm.data.counter);\",\n            \"   msg.payload = sm.data.counter;\",\n            \"   output = true;\",\n            \"}\"\n        ],\n        \"status\": {\n            \"fill\": \"blue\",\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"sm.data.counter;\"\n            }\n        }\n    }\n}\n","x":380,"y":1400,"wires":["b0d2d3ff.b9719"](/cflurin/node-red-contrib-dsm/wiki/"b0d2d3ff.b9719")},{"id":"54e8dc78.3d1304","type":"inject","z":"6ff0723.8c6b78c","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":1440,"wires":["1b8f0a20.7be0e6"](/cflurin/node-red-contrib-dsm/wiki/"1b8f0a20.7be0e6")},{"id":"b0d2d3ff.b9719","type":"debug","z":"6ff0723.8c6b78c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":570,"y":1400,"wires":[]},{"id":"d962967a.d874e8","type":"inject","z":"6ff0723.8c6b78c","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":1400,"wires":["1b8f0a20.7be0e6"](/cflurin/node-red-contrib-dsm/wiki/"1b8f0a20.7be0e6")},{"id":"35c1f0b9.ad41b","type":"inject","z":"6ff0723.8c6b78c","name":"reset counter","topic":"reset","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":1360,"wires":["1b8f0a20.7be0e6"](/cflurin/node-red-contrib-dsm/wiki/"1b8f0a20.7be0e6")}]