-
Notifications
You must be signed in to change notification settings - Fork 4
Time gate
cflurin edited this page Feb 18, 2019
·
7 revisions
The time-gate
dsm combines some properties of timer/scheduler nodes and make it easy to use for severals use cases.
In the configuration you can set the start
and end
time, inside the time-range the messages are passed through.
Note1: Please, install the latest dsm version.
Note2: If suncalc
isn't already installed, please install suncalc in your user directory e.g. /home/pi/.node-red
"data": {
"start": "sunrise",
"end": "14:00",
"day": {
"Sun": true,
"Mon": true,
"Tue": true,
"Wed": true,
"Thu": true,
"Fri": true,
"Sat": true
},
"lat": 51.2,
"lon": -1.4
},
- "start": [00:00 ... 23:59] or a valid suncalc property.
- "end": [00:00 ... 23:59] or a valid suncalc property.
- "day": true > active, false > inactive
- "lat": latitude
- "lon": longitude
{
"triggerInput": "cmd",
"stateOutput": "currentState",
"currentState": "closed",
"states": {
"closed": {
"open": "opened",
"close": "closed"
},
"opened": {
"close": "closed",
"open": "opened"
}
},
"doc": {
"sun_pos": "sunrise, sunset, dawn, dusk, goldenHour, ... see suncalc",
"suncalc-link": "https://github.com/mourner/suncalc"
},
"data": {
"start": "sunrise",
"end": "17:00",
"day": {
"Sun": true,
"Mon": true,
"Tue": true,
"Wed": true,
"Thu": true,
"Fri": true,
"Sat": true
},
"lat": 51.2,
"lon": -1.4
},
"dn": ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
"methods": {
"init": [
"sm.suncalc = require('suncalc');",
"sm.getm = function (t) {",
"var arr = t.split(':');",
"return parseInt(arr[0])*60 + parseInt(arr[1]);",
"};",
"sm.gethhmm = function (d, pos) {",
"sm.time = sm.suncalc.getTimes(d, sm.data.lat, sm.data.lon)[pos];",
"return sm.time.getUTCHours() + ':' + sm.time.getUTCMinutes();",
"};"
],
"default": [
"var d = new Date();",
"if (!sm.data.start.includes(':')) {",
"sm.start = sm.gethhmm(d, sm.data.start);",
"} else {",
"sm.start = sm.data.start;",
"}",
"sm.start_m = sm.getm(sm.start);",
"if (!sm.data.end.includes(':')) {",
"sm.end = sm.gethhmm(d, sm.data.end);",
"} else {",
"sm.end = sm.data.end;",
"}",
"sm.end_m = sm.getm(sm.end);",
"sm.now = pad(d.getHours()) + ':' + pad(d.getMinutes());",
"sm.mins = d.getHours()*60 + d.getMinutes();",
"sm.day = sm.dn[d.getDay()];",
"if (sm.start_m < sm.end_m) {",
"if (sm.mins >= sm.start_m && sm.mins <= sm.end_m && sm.data.day[sm.day]) {",
"resume('open', msg);",
"} else {",
"resume('close', msg);",
"}",
"} else if (!(sm.mins >= sm.end_m && sm.mins < sm.start_m) && sm.data.day[sm.day]) {",
"resume('open', msg);",
"} else {",
"resume('close', msg);",
"}",
"output = false;"
],
"open": [
"sm.fill = 'yellow';",
"output = true;"
],
"close": [
"sm.fill = 'blue';",
"output = false;"
],
"status": {
"fill": {
"get": "sm.fill || 'grey';"
},
"shape": "dot",
"text": {
"get": "sm.now + ' [start: ' + sm.start + ' - end: ' + sm.end + ']';"
}
}
}
}
[{"id":"5fb1d99.fe69b28","type":"inject","z":"ac4aa9f6.c24288","name":"","topic":"","payload":"my msg","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":2640,"wires":[["d6123332.a561c"]]},{"id":"d6123332.a561c","type":"dsm","z":"ac4aa9f6.c24288","name":"time gate","sm_config":"{\n \"triggerInput\": \"cmd\",\n \"stateOutput\": \"currentState\",\n \"currentState\": \"closed\",\n \"states\": {\n \"closed\": {\n \"open\": \"opened\",\n \"close\": \"closed\"\n },\n \"opened\": {\n \"close\": \"closed\",\n \"open\": \"opened\"\n }\n },\n \"doc\": {\n \"sun_pos\": \"sunrise, sunset, dawn, dusk, goldenHour, ... see suncalc\",\n \"suncalc-link\": \"https://github.com/mourner/suncalc\" \n },\n \"data\": {\n \"start\": \"sunrise\",\n \"end\": \"17:00\",\n \"day\": {\n \"Sun\": true,\n \"Mon\": true,\n \"Tue\": true,\n \"Wed\": true,\n \"Thu\": true,\n \"Fri\": true,\n \"Sat\": true\n },\n \"lat\": 51.2,\n \"lon\": -1.4\n },\n \"dn\": [\"Sun\", \"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\"],\n \"methods\": {\n \"init\": [\n \"sm.suncalc = require('suncalc');\",\n \n \"sm.getm = function (t) {\",\n \"var arr = t.split(':');\",\n \"return parseInt(arr[0])*60 + parseInt(arr[1]);\",\n \"};\",\n \n \"sm.gethhmm = function (d, pos) {\",\n \"sm.time = sm.suncalc.getTimes(d, sm.data.lat, sm.data.lon)[pos];\",\n \"return sm.time.getUTCHours() + ':' + sm.time.getUTCMinutes();\",\n \"};\"\n ],\n \"default\": [\n \"var d = new Date();\",\n \"if (!sm.data.start.includes(':')) {\",\n \"sm.start = sm.gethhmm(d, sm.data.start);\",\n \"} else {\",\n \"sm.start = sm.data.start;\",\n \"}\",\n \"sm.start_m = sm.getm(sm.start);\",\n\n \"if (!sm.data.end.includes(':')) {\",\n \"sm.end = sm.gethhmm(d, sm.data.end);\",\n \"} else {\",\n \"sm.end = sm.data.end;\",\n \"}\",\n \"sm.end_m = sm.getm(sm.end);\",\n\n \"sm.now = pad(d.getHours()) + ':' + pad(d.getMinutes());\",\n \"sm.mins = d.getHours()*60 + d.getMinutes();\",\n \"sm.day = sm.dn[d.getDay()];\",\n \n \"if (sm.start_m < sm.end_m) {\",\n \"if (sm.mins >= sm.start_m && sm.mins <= sm.end_m && sm.data.day[sm.day]) {\",\n \"resume('open', msg);\",\n \"} else {\",\n \"resume('close', msg);\",\n \"}\",\n \"} else if (!(sm.mins >= sm.end_m && sm.mins < sm.start_m) && sm.data.day[sm.day]) {\",\n \"resume('open', msg);\",\n \"} else {\",\n \"resume('close', msg);\", \n \"}\",\n \"output = false;\"\n ],\n \"open\": [\n \"sm.fill = 'yellow';\",\n \"output = true;\" \n ],\n \"close\": [\n \"sm.fill = 'blue';\",\n \"output = false;\" \n ],\n \"status\": {\n \"fill\": {\n \"get\": \"sm.fill || 'grey';\"\n },\n \"shape\": \"dot\",\n \"text\": {\n \"get\": \"sm.now + ' [start: ' + sm.start + ' - end: ' + sm.end + ']';\"\n }\n }\n }\n}\n","x":310,"y":2640,"wires":[["d459100f.8c55b"]]},{"id":"d459100f.8c55b","type":"debug","z":"ac4aa9f6.c24288","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":490,"y":2640,"wires":[]}]