Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [
- {
- "id": "ec1e5bf6.b0b6a8",
- "type": "http request",
- "z": "39b2077c.e1fce8",
- "name": "sunrise-sunset api",
- "method": "GET",
- "ret": "obj",
- "url": "http://api.sunrise-sunset.org/json?lat=39.37797&lng=-74.60392&formatted=0",
- "tls": "",
- "x": 310,
- "y": 100,
- "wires": [
- [
- "2c351243.c8fcee",
- "c1334b82.13f578"
- ]
- ]
- },
- {
- "id": "2c351243.c8fcee",
- "type": "function",
- "z": "39b2077c.e1fce8",
- "name": "set global context",
- "func": "// detect api errors\nif (msg.payload.status != \"OK\") {\n return msg;\n}\n\nvar sunrise = new Date(msg.payload.results.sunrise);\nvar sunset = new Date(msg.payload.results.sunset);\n\nglobal.set(\"sunrise\", sunrise.toJSON());\nglobal.set(\"sunset\", sunset.toJSON());\n\nreturn { payload: {} };",
- "outputs": "1",
- "noerr": 0,
- "x": 530,
- "y": 100,
- "wires": [
- [
- "b5eb7c3c.30bdc"
- ]
- ]
- },
- {
- "id": "349e5d6f.0ef762",
- "type": "inject",
- "z": "39b2077c.e1fce8",
- "name": "00:00 kick",
- "topic": "",
- "payload": "",
- "payloadType": "date",
- "repeat": "",
- "crontab": "00 00 * * *",
- "once": false,
- "x": 110,
- "y": 100,
- "wires": [
- [
- "ec1e5bf6.b0b6a8"
- ]
- ]
- },
- {
- "id": "fcaa38ac.b628e8",
- "type": "inject",
- "z": "39b2077c.e1fce8",
- "name": "boot",
- "topic": "",
- "payload": "",
- "payloadType": "date",
- "repeat": "",
- "crontab": "",
- "once": true,
- "x": 130,
- "y": 40,
- "wires": [
- [
- "ec1e5bf6.b0b6a8"
- ]
- ]
- },
- {
- "id": "ec3f963e.71f698",
- "type": "interval",
- "z": "39b2077c.e1fce8",
- "name": "every 5m",
- "interval": "5",
- "onstart": false,
- "msg": "ping",
- "showstatus": true,
- "unit": "minutes",
- "statusformat": "YYYY-MM-D HH:mm:ss",
- "x": 560,
- "y": 180,
- "wires": [
- [
- "b5eb7c3c.30bdc"
- ]
- ]
- },
- {
- "id": "b5eb7c3c.30bdc",
- "type": "function",
- "z": "39b2077c.e1fce8",
- "name": "set time of day",
- "func": "// input is a trigger, payload does not matter\n\nvar sunrise = new Date(global.get(\"sunrise\"));\nvar sunset = new Date(global.get(\"sunset\"));\nvar now = new Date();\n\nvar sunrise_day = sunrise.getDate();\nvar now_day = now.getDate();\n\nif (sunrise_day != now_day) {\n node.warn(\"stale sunrise data, sunrise_day=\" + sunrise_day + \", now_day=\" + now_day);\n return null;\n}\n\nvar tod;\n\nif (now >= sunrise && now.getHours() < 12) {\n tod = \"morning\";\n} else if (now.getHours() >= 12 && now < sunset) {\n tod = \"afternoon\";\n} else if (now >= sunset && now.getHours() < 23) {\n tod = \"evening\";\n} else {\n tod = \"night\";\n}\n\nnode.status({text: tod});\n\nvar last = context.get(\"tod\");\nif (tod == last) {\n return null;\n}\n\n// local context so we lose state on restart\ncontext.set(\"tod\", tod);\n\n// global context for other consumers\nglobal.set(\"tod\", tod);\n\n// only fires on boot and state change\nreturn { payload: { tod: tod } };",
- "outputs": 1,
- "noerr": 0,
- "x": 740,
- "y": 140,
- "wires": [
- [
- "c1334b82.13f578",
- "54bcc22a.d9574c"
- ]
- ]
- },
- {
- "id": "c1334b82.13f578",
- "type": "debug",
- "z": "39b2077c.e1fce8",
- "name": "tod_debug",
- "active": true,
- "console": "false",
- "complete": "true",
- "x": 750,
- "y": 40,
- "wires": []
- },
- {
- "id": "54bcc22a.d9574c",
- "type": "mqtt out",
- "z": "39b2077c.e1fce8",
- "name": "",
- "topic": "state/tod",
- "qos": "",
- "retain": "",
- "broker": "c77e45a7.481c88",
- "x": 920,
- "y": 140,
- "wires": []
- },
- {
- "id": "c77e45a7.481c88",
- "type": "mqtt-broker",
- "z": "",
- "broker": "127.0.0.1",
- "port": "1883",
- "clientid": "",
- "usetls": false,
- "compatmode": true,
- "keepalive": "60",
- "cleansession": true,
- "willTopic": "",
- "willQos": "0",
- "willPayload": "",
- "birthTopic": "",
- "birthQos": "0",
- "birthPayload": ""
- }
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement