-
Notifications
You must be signed in to change notification settings - Fork 4
Scheduler
cflurin edited this page Dec 7, 2018
·
8 revisions
This example shows how to combine several schedulers (schedex) for turning on/off devices. Additionally a manual control can be used which turns the auto mode off.
Note: You need to install node-red-contrib-schedex
>>> Set the latitude and longitude for your city.
{
"currentState": "auto",
"stateOutput": "payload",
"states": {
"auto": {
"man": "man"
},
"man": {
"auto": "auto"
}
},
"data": {
"latitude": 52.2,
"longitude": 0.12,
"sunrise": null,
"sunset": null,
"schedon": null,
"schedoff": null
},
"methods": {
"init": [
"sm.suncalc = require('suncalc');",
"sm.getmins = function() {",
" var date = new Date();",
" return date.getHours()*60 + date.getMinutes();",
"}"
],
"onBeforeTransition": [
"var now = new Date();",
"var times = sm.suncalc.getTimes(now, sm.data.latitude, sm.data.longitude);",
"sm.data.sunrise = times.sunrise.getHours()*60 + times.sunrise.getMinutes();",
"sm.data.sunset = times.sunset.getHours()*60 + times.sunset.getMinutes();"
],
"auto": "output = false;",
"man": "output = false;",
"sunrise": [
"output = sm.currentState === 'auto' ? true : false;"
],
"sunset": [
"if (sm.currentState === 'auto') {",
" if (sm.data.schedoff !== null && sm.getmins() > sm.data.schedoff) {",
" output = false;",
" } else {",
" output = true;",
" }",
"}"
],
"schedon": [
"sm.data.schedon = sm.getmins();",
"if (sm.currentState === 'auto' && sm.getmins() < sm.data.sunrise) {",
" output = true;",
"}"
],
"schedoff": [
"sm.data.schedoff = sm.getmins();",
"output = sm.currentState === 'auto' ? true : false;"
],
"default": [
"if (msg.payload === 'on' || msg.payload === 'off') {",
" sm.currentState = 'man';",
" output = true;",
"}"
],
"status": {
"fill": {
"get": "sm.currentState === 'auto' ? 'green' : 'grey';"
},
"shape": "dot",
"text": {
"get": "sm.currentState"
}
}
}
}
[{"id":"a60eee37.7e1a1","type":"dsm","z":"895bbff8.0e6c4","name":"","sm_config":"{\n \"currentState\": \"auto\",\n \"stateOutput\": \"payload\",\n \"states\": {\n \"auto\": {\n \"man\": \"man\"\n },\n \"man\": {\n \"auto\": \"auto\"\n }\n },\n \"data\": {\n \"latitude\": 52.2,\n \"longitude\": 0.12,\n \"sunrise\": null,\n \"sunset\": null,\n \"schedon\": null,\n \"schedoff\": null\n },\n \"methods\": {\n \"init\": [\n \"sm.suncalc = require('suncalc');\",\n \"sm.getmins = function() {\",\n \" var date = new Date();\",\n \" return date.getHours()*60 + date.getMinutes();\",\n \"}\"\n ],\n \"onBeforeTransition\": [\n \"var now = new Date();\",\n \"var times = sm.suncalc.getTimes(now, sm.data.latitude, sm.data.longitude);\",\n \"sm.data.sunrise = times.sunrise.getHours()*60 + times.sunrise.getMinutes();\",\n \"sm.data.sunset = times.sunset.getHours()*60 + times.sunset.getMinutes();\"\n ],\n \"auto\": \"output = false;\",\n \"man\": \"output = false;\",\n \"sunrise\": [\n \"output = sm.currentState === 'auto' ? true : false;\"\n ],\n \"sunset\": [\n \"if (sm.currentState === 'auto') {\",\n \" if (sm.data.schedoff !== null && sm.getmins() > sm.data.schedoff) {\",\n \" output = false;\",\n \" } else {\",\n \" output = true;\",\n \" }\",\n \"}\"\n ],\n \"schedon\": [\n \"sm.data.schedon = sm.getmins();\",\n \"if (sm.currentState === 'auto' && sm.getmins() < sm.data.sunrise) {\",\n \" output = true;\",\n \"}\"\n ],\n \"schedoff\": [\n \"sm.data.schedoff = sm.getmins();\",\n \"output = sm.currentState === 'auto' ? true : false;\"\n ],\n \"default\": [\n \"if (msg.payload === 'on' || msg.payload === 'off') {\",\n \" sm.currentState = 'man';\",\n \" output = true;\",\n \"}\"\n ],\n \"status\": {\n \"fill\": {\n \"get\": \"sm.currentState === 'auto' ? 'green' : 'grey';\"\n },\n \"shape\": \"dot\",\n \"text\": {\n \"get\": \"sm.currentState\"\n }\n }\n }\n}","x":590,"y":880,"wires":[["803d20e4.012ed","8772fce4.b9dcd"]]},{"id":"36d962f4.f78b8e","type":"inject","z":"895bbff8.0e6c4","name":"","topic":"","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":780,"wires":[["a60eee37.7e1a1"]]},{"id":"6e3d8ee9.2ef1a","type":"inject","z":"895bbff8.0e6c4","name":"","topic":"","payload":"off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":820,"wires":[["a60eee37.7e1a1"]]},{"id":"7cd7448.e2390bc","type":"schedex","z":"895bbff8.0e6c4","name":"","suspended":false,"lat":"52.2","lon":"0.12","ontime":"07:00","ontopic":"schedon","onpayload":"on","onoffset":0,"onrandomoffset":0,"offtime":"sunrise","offtopic":"sunrise","offpayload":"off","offoffset":0,"offrandomoffset":0,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"sun":true,"x":300,"y":860,"wires":[["a60eee37.7e1a1"]]},{"id":"4ab6726b.2ab82c","type":"schedex","z":"895bbff8.0e6c4","name":"","suspended":false,"lat":"52.2","lon":"0.12","ontime":"sunset","ontopic":"sunset","onpayload":"on","onoffset":0,"onrandomoffset":0,"offtime":"23:00","offtopic":"schedoff","offpayload":"off","offoffset":0,"offrandomoffset":0,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"sun":true,"x":300,"y":920,"wires":[["a60eee37.7e1a1"]]},{"id":"bd42809b.25ffa","type":"inject","z":"895bbff8.0e6c4","name":"","topic":"auto","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":550,"y":780,"wires":[["a60eee37.7e1a1"]]},{"id":"f37be184.f9aa4","type":"inject","z":"895bbff8.0e6c4","name":"","topic":"man","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":550,"y":820,"wires":[["a60eee37.7e1a1"]]},{"id":"803d20e4.012ed","type":"debug","z":"895bbff8.0e6c4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":730,"y":840,"wires":[]},{"id":"7b1f747a.64348c","type":"ui_chart","z":"895bbff8.0e6c4","name":"","group":"39eef9a2.f62ff6","order":1,"width":"7","height":"3","label":"scheduler","chartType":"line","legend":"false","xformat":"HH:mm","interpolate":"step","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"86400","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":740,"y":940,"wires":[["9ba59016.eb256"],[]]},{"id":"8772fce4.b9dcd","type":"change","z":"895bbff8.0e6c4","name":"parse","rules":[{"t":"change","p":"payload","pt":"msg","from":"on","fromt":"str","to":"1","tot":"num"},{"t":"change","p":"payload","pt":"msg","from":"off","fromt":"str","to":"0","tot":"num"},{"t":"set","p":"topic","pt":"msg","to":"state","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":880,"wires":[["7b1f747a.64348c"]]},{"id":"9ba59016.eb256","type":"dsm","z":"895bbff8.0e6c4","name":"memory","sm_config":"{\n \"currentState\": \"retrieved\",\n \"states\": {\n \"retrieved\": {\n \"retrieve\": \"retrieved\",\n \"store\": \"stored\",\n \"clear\": \"cleared\"\n },\n \"stored\": {\n \"retrieve\": \"retrieved\",\n \"store\": \"stored\",\n \"clear\": \"cleared\"\n },\n \"cleared\": {\n \"retrieve\": \"retrieved\",\n \"store\": \"stored\",\n \"clear\": \"cleared\"\n }\n },\n \"data\": {\n \"name\": \"chart_data\"\n },\n \"methods\": {\n \"init\": [\n \"msg = {};\",\n \"msg.payload = context.get(sm.data.name);\",\n \"if (typeof msg.payload !== 'undefined') {\",\n \" msg.token = 'skip';\",\n \" node.send(msg);\",\n \"}\"\n ],\n \"retrieve\": [\n \"msg.payload = context.get(sm.data.name);\",\n \"if (typeof msg.payload !== 'undefined') {\",\n \" msg.token = 'skip';\",\n \"} else {\",\n \" output = false;\",\n \"}\"\n ],\n \"store\": [\n \"context.set(sm.data.name, msg.payload);\",\n \"output = false;\"\n ],\n \"clear\": [\n \"msg.payload = [];\",\n \"msg.token = 'skip';\"\n ],\n \"default\": [\n \"if (msg.token !== 'skip') {\",\n \" resume('store', msg);\",\n \"}\"\n ],\n \"status\": {\n \"fill\": {\n \"get\": \"sm.currentState === 'cleared' ? 'yellow' : 'green';\"\n },\n \"shape\": {\n \"get\": \"sm.currentState === 'cleared' ? 'ring' : 'dot';\"\n },\n \"text\": {\n \"get\": \"sm.currentState;\"\n }\n }\n }\n}","x":730,"y":1020,"wires":[["7b1f747a.64348c"]]},{"id":"a26d3435.e7d128","type":"inject","z":"895bbff8.0e6c4","name":"","topic":"clear","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":550,"y":1020,"wires":[["9ba59016.eb256"]]},{"id":"39eef9a2.f62ff6","type":"ui_group","z":"","name":"charts","tab":"7d65d785.39b868","order":1,"disp":true,"width":"7","collapse":false},{"id":"7d65d785.39b868","type":"ui_tab","z":"","name":"dsm","icon":"dashboard","order":5}]