Skip to content

Fade in out

cflurin edited this page Feb 1, 2019 · 5 revisions

Input

msg.topic: "start"
msg.payload:
{
    "initialdelay": 2,
    "start": 0,
    "end": 1,
    "duration": 3,
    "step": 0.2,
    "unit": "Seconds"
}

or

msg.topic: "stop"
msg.payload: <any>
  • initialdelay: time before fading starts
  • start: start value
  • end: end value
  • duration: fading time
  • step: output interval
  • unit: unit for all time values <Milliseconds> | <Seconds> | <Minutes>

Output

msg.payload: <value> between start and end

fadeinout

Configuration

{
    "inputTrigger": "topic",
    "stateOutput": "current",
    "currentState": "stopped",
    
    "states": {
        "stopped": {
            "start": "started",
            "stop": "stopped"
        },
        "started": {
            "fade_in": "fade_in",
            "fade_out": "fade_out",
            "stop": "stopped"
        },
        "fade_in": {
            "fade_in": "fade_in",
            "stop": "stopped"
        },
        "fade_out": {
            "fade_out": "fade_out",
            "stop": "stopped"
        }
    },
    "data": {
        "initialdelay": 0,
        "start": 0,
        "end": 1,
        "duration": 3000,
        "step": 100,
        "unit": "Milliseconds",
        "factor": 1
    },
    "methods": {
        "init": [
            "sm.tick = function(tran, msg) {",
            "   timeout.id = setTimeout(function() {",
            "       sm.text = 'fading ' + sm.data.duration + ' ' + sm.data.unit + ' - ' + sm.data.actual;",
            "       msg.payload = sm.data.actual;",
            "       node.send(msg);",
            "       resume(tran, msg);",
            "   }, sm.data.step * sm.data.factor);",
            "};",
            "sm.factors = {'milliseconds': 1, 'seconds': 1000, 'minutes': 60000};",
            "sta.fill = 'grey';"
        ],
        "start": [
            "if (typeof msg.payload.initialdelay !== 'undefined') sm.data.initialdelay = msg.payload.initialdelay;",
            "if (typeof msg.payload.start !== 'undefined') sm.data.start = msg.payload.start;",
            "if (typeof msg.payload.end !== 'undefined') sm.data.end = msg.payload.end;",
            "if (typeof msg.payload.duration !== 'undefined') sm.data.duration = msg.payload.duration;",
            "if (typeof msg.payload.step !== 'undefined') sm.data.step = msg.payload.step;",
            "if (typeof msg.payload.unit !== 'undefined') {",
            "   if (sm.factors.hasOwnProperty(msg.payload.unit.toLowerCase())) {",
            "       sm.data.factor = sm.factors[msg.payload.unit.toLowerCase()];",
            "       sm.data.unit = msg.payload.unit;",
            "   } else {",
            "       node.warn('>' + msg.payload.unit + '< unknown, available: Milliseconds, Seconds, Minutes');",
            "   };",
            "} else {",
            "   sm.data.factor = 1;",
            "   sm.data.unit =  'Milliseconds';",
            "};",
            
            "clearTimeout(timeout.id);",
            "timeout.id = setTimeout(function() {",
            "   sm.data.delta = (sm.data.step / sm.data.duration) * Math.abs(sm.data.start - sm.data.end);",
            "   sm.data.actual = sm.data.start;",
            
            "   if (sm.data.end > sm.data.start) {",
            "       sm.text = 'fade_in';",
            "       resume('fade_in', msg);",
            "   } else {",
            "       sm.text = 'fade_out';",
            "       resume('fade_out', msg);",
            "   };",
            "}, sm.data.initialdelay * sm.data.factor);",
            "sm.text = 'initial delay ' + sm.data.initialdelay + ' ' + sm.data.unit;",
            "msg.payload = sm.data.start;",
            "node.send(msg);"
        ],
        "fade_in": [
            "if (sm.data.actual < sm.data.end) {",
            "   sm.data.actual = Math.round((sm.data.actual + sm.data.delta) * 100) / 100;",
            "   if (sm.data.actual > sm.data.end) {sm.data.actual = sm.data.end;};",
            "   sm.tick('fade_in', msg);",
            "} else {",
            "   resume('stop', msg);",
            "};"
        ],
        "fade_out": [
            "if (sm.data.actual > sm.data.end) {",
            "   sm.data.actual = Math.round((sm.data.actual - sm.data.delta) * 100) / 100;",
            "   if (sm.data.actual < sm.data.end) {sm.data.actual = sm.data.end;};",
            "   sm.tick('fade_out', msg);",
            "} else {",
            "   resume('stop', msg);",
            "};"
        ],
        "stop": [
            "sm.text = sm.currentState;",
            "clearTimeout(timeout.id);"
        ],
        "onAfterTransition": "output = false;",
        "status": {
            "fill": {
                "get": "sm.currentState === 'stopped' ? 'grey': 'green';"
            },
            "shape": "dot",
            "text": {
                "get": "sm.text || sm.currentState;"
            }
        }
    }
}

Flow

[{"id":"b3c0022e.f99bc","type":"dsm","z":"6ff0723.8c6b78c","name":"fade in/out","sm_config":"{\n    \"inputTrigger\": \"topic\",\n    \"stateOutput\": \"current\",\n    \"currentState\": \"stopped\",\n    \n    \"states\": {\n        \"stopped\": {\n            \"start\": \"started\",\n            \"stop\": \"stopped\"\n        },\n        \"started\": {\n            \"fade_in\": \"fade_in\",\n            \"fade_out\": \"fade_out\",\n            \"stop\": \"stopped\"\n        },\n        \"fade_in\": {\n            \"fade_in\": \"fade_in\",\n            \"stop\": \"stopped\"\n        },\n        \"fade_out\": {\n            \"fade_out\": \"fade_out\",\n            \"stop\": \"stopped\"\n        }\n    },\n    \"data\": {\n        \"initialdelay\": 0,\n        \"start\": 0,\n        \"end\": 1,\n        \"duration\": 3000,\n        \"step\": 100,\n        \"unit\": \"Milliseconds\",\n        \"factor\": 1\n    },\n    \"methods\": {\n        \"init\": [\n            \"sm.tick = function(tran, msg) {\",\n            \"   timeout.id = setTimeout(function() {\",\n            \"       sm.text = 'fading ' + sm.data.duration + ' ' + sm.data.unit + ' - ' + sm.data.actual;\",\n            \"       msg.payload = sm.data.actual;\",\n            \"       node.send(msg);\",\n            \"       resume(tran, msg);\",\n            \"   }, sm.data.step * sm.data.factor);\",\n            \"};\",\n            \"sm.factors = {'milliseconds': 1, 'seconds': 1000, 'minutes': 60000};\",\n            \"sta.fill = 'grey';\"\n        ],\n        \"start\": [\n            \"if (typeof msg.payload.initialdelay !== 'undefined') sm.data.initialdelay = msg.payload.initialdelay;\",\n            \"if (typeof msg.payload.start !== 'undefined') sm.data.start = msg.payload.start;\",\n            \"if (typeof msg.payload.end !== 'undefined') sm.data.end = msg.payload.end;\",\n            \"if (typeof msg.payload.duration !== 'undefined') sm.data.duration = msg.payload.duration;\",\n            \"if (typeof msg.payload.step !== 'undefined') sm.data.step = msg.payload.step;\",\n            \"if (typeof msg.payload.unit !== 'undefined') {\",\n            \"   if (sm.factors.hasOwnProperty(msg.payload.unit.toLowerCase())) {\",\n            \"       sm.data.factor = sm.factors[msg.payload.unit.toLowerCase()];\",\n            \"       sm.data.unit = msg.payload.unit;\",\n            \"   } else {\",\n            \"       node.warn('>' + msg.payload.unit + '< unknown, available: Milliseconds, Seconds, Minutes');\",\n            \"   };\",\n            \"} else {\",\n            \"   sm.data.factor = 1;\",\n            \"   sm.data.unit =  'Milliseconds';\",\n            \"};\",\n            \n            \"clearTimeout(timeout.id);\",\n            \"timeout.id = setTimeout(function() {\",\n            \"   sm.data.delta = (sm.data.step / sm.data.duration) * Math.abs(sm.data.start - sm.data.end);\",\n            \"   sm.data.actual = sm.data.start;\",\n            \n            \"   if (sm.data.end > sm.data.start) {\",\n            \"       sm.text = 'fade_in';\",\n            \"       resume('fade_in', msg);\",\n            \"   } else {\",\n            \"       sm.text = 'fade_out';\",\n            \"       resume('fade_out', msg);\",\n            \"   };\",\n            \"}, sm.data.initialdelay * sm.data.factor);\",\n            \"sm.text = 'initial delay ' + sm.data.initialdelay + ' ' + sm.data.unit;\",\n            \"msg.payload = sm.data.start;\",\n            \"node.send(msg);\"\n        ],\n        \"fade_in\": [\n            \"if (sm.data.actual < sm.data.end) {\",\n            \"   sm.data.actual = Math.round((sm.data.actual + sm.data.delta) * 100) / 100;\",\n            \"   if (sm.data.actual > sm.data.end) {sm.data.actual = sm.data.end;};\",\n            \"   sm.tick('fade_in', msg);\",\n            \"} else {\",\n            \"   resume('stop', msg);\",\n            \"};\"\n        ],\n        \"fade_out\": [\n            \"if (sm.data.actual > sm.data.end) {\",\n            \"   sm.data.actual = Math.round((sm.data.actual - sm.data.delta) * 100) / 100;\",\n            \"   if (sm.data.actual < sm.data.end) {sm.data.actual = sm.data.end;};\",\n            \"   sm.tick('fade_out', msg);\",\n            \"} else {\",\n            \"   resume('stop', msg);\",\n            \"};\"\n        ],\n        \"stop\": [\n            \"sm.text = sm.currentState;\",\n            \"clearTimeout(timeout.id);\"\n        ],\n        \"onAfterTransition\": \"output = false;\",\n        \"status\": {\n            \"fill\": {\n                \"get\": \"sm.currentState === 'stopped' ? 'grey': 'green';\"\n            },\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"sm.text || sm.currentState;\"\n            }\n        }\n    }\n}","x":350,"y":2200,"wires":[["4e43e7f0.d1eee8"]]},{"id":"5226bacb.b02654","type":"inject","z":"6ff0723.8c6b78c","name":"fade in","topic":"start","payload":"{\"initialdelay\":2,\"start\":0,\"end\":1,\"duration\":3,\"step\":0.2,\"unit\":\"Seconds\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":2200,"wires":[["b3c0022e.f99bc"]]},{"id":"de736c65.b889a","type":"inject","z":"6ff0723.8c6b78c","name":"fade out","topic":"start","payload":"{\"initialdelay\":2000,\"start\":1,\"end\":0,\"duration\":5000,\"step\":500}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":2240,"wires":[["b3c0022e.f99bc"]]},{"id":"6820c9c9.829228","type":"inject","z":"6ff0723.8c6b78c","name":"stop","topic":"stop","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":2280,"wires":[["b3c0022e.f99bc"]]},{"id":"4e43e7f0.d1eee8","type":"debug","z":"6ff0723.8c6b78c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":490,"y":2200,"wires":[]}]
Clone this wiki locally