Fade in out

Fade in or out can be used for example to control the brightness of a lightbulb or generally to increase/decrease a value. The dsm configuration can be modified/improved without the need to publish a new version. Nevertheless it has the same performance as a node-red-contrib-node.

Go to the dsm Wiki Fade in out for more details and to get the latest version.

[{"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":"payload","x":510,"y":2200,"wires":[]}]

Flow Info

Created 5 years, 8 months ago
Rating: 5 1

Owner

Actions

Rate:

Node Types

Core
  • debug (x1)
  • inject (x3)
Other

Tags

  • fade
  • fading
  • increase
  • decrease
  • brightness
  • linear_function
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option