Created
January 23, 2022 14:01
-
-
Save toneomgomg/8fed6c717dbbecf1cf3f69478d9cc9d9 to your computer and use it in GitHub Desktop.
Node-RED flöde för postnord utdelning. [proof-of-concept]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"id": "21e6c0acf26de00d", | |
"type": "tab", | |
"label": "Postnord API", | |
"disabled": false, | |
"info": "", | |
"env": [] | |
}, | |
{ | |
"id": "1e6ec6836f9188ca", | |
"type": "http request", | |
"z": "21e6c0acf26de00d", | |
"name": "Use postnord API", | |
"method": "GET", | |
"ret": "obj", | |
"paytoqs": "ignore", | |
"url": "https://portal.postnord.com/api/sendoutarrival/closest?postalCode=PUT_YOUR_POSTNUMMER_HERE", | |
"tls": "", | |
"persist": false, | |
"proxy": "", | |
"authType": "", | |
"senderr": false, | |
"x": 310, | |
"y": 120, | |
"wires": [ | |
[ | |
"5f3ae0d253eb2aea" | |
] | |
] | |
}, | |
{ | |
"id": "057e8f3647213fa4", | |
"type": "inject", | |
"z": "21e6c0acf26de00d", | |
"name": "Varje timme", | |
"props": [], | |
"repeat": "3600", | |
"crontab": "", | |
"once": true, | |
"onceDelay": "5", | |
"topic": "", | |
"x": 110, | |
"y": 120, | |
"wires": [ | |
[ | |
"1e6ec6836f9188ca" | |
] | |
] | |
}, | |
{ | |
"id": "5ff820f926556f44", | |
"type": "comment", | |
"z": "21e6c0acf26de00d", | |
"name": "README", | |
"info": "\nAPI:\n Endpoint: https://portal.postnord.com/api/sendoutarrival/closest?postalCode=71431\n \nResult:\n{\n msg.statusCode: 200,\n msg.payload: \n {\n postalCode: 71431\n city: \"Kopparberg\"\n delivery: \"21 januari, 2022\"\n upcoming: \"24 januari, 2022\"\n }\n // [...]\n}", | |
"x": 100, | |
"y": 60, | |
"wires": [] | |
}, | |
{ | |
"id": "5f3ae0d253eb2aea", | |
"type": "function", | |
"z": "21e6c0acf26de00d", | |
"name": "Parse", | |
"func": "if (msg.statusCode !== 200) {\n return null;\n}\n\nconst formatDate = (indata) => {\n return new Intl.DateTimeFormat('sv-SE').format(indata);\n}\n\nlet today = false\nlet next = 'okänt';\n\nconst delivery = formatDate(new Date(msg.payload.delivery));\nconst upcoming = formatDate(new Date(msg.payload.upcoming));\n\nlet now = formatDate(Date.now());\n\n\n/*\n * Är det någon utdelning idag?\n */\nif (delivery === now || upcoming === now) {\n today = true;\n} \n\n/*\n * När är nästa utdelning?\n */\nif (upcoming > now) {\n next = new Date(upcoming);\n}\n\nif (delivery > now) {\n next = new Date(delivery);\n}\n\nif (next) {\n /*\n * Skapa en vänlig sensor för nästa utdelning:\n * - Imorgon\n * - I övermorgon\n * - Om x dagar\n */\n\n const delta = (new Date(next) - new Date(now)) / 1000 / 3600 / 24;\n\n switch (delta) {\n case 1:\n next = 'imorgon';\n break;\n case 2:\n next = 'i övermorgon';\n break;\n default:\n next = `om ${delta} dagar`;\n break;\n }\n}\n\nreturn [\n { payload: today },\n { payload: next }\n];\n", | |
"outputs": 2, | |
"noerr": 0, | |
"initialize": "", | |
"finalize": "", | |
"libs": [], | |
"x": 490, | |
"y": 120, | |
"wires": [ | |
[ | |
"09abe22b44d07edd" | |
], | |
[ | |
"e86c7e059831a791" | |
] | |
], | |
"outputLabels": [ | |
"Utdelning idag?", | |
"Nästa utdelning" | |
] | |
}, | |
{ | |
"id": "09abe22b44d07edd", | |
"type": "ha-entity", | |
"z": "21e6c0acf26de00d", | |
"name": "binary_sensor.postnord_delivery_today", | |
"server": "450ad165dd77c3a2", | |
"version": 2, | |
"debugenabled": false, | |
"outputs": 1, | |
"entityType": "binary_sensor", | |
"config": [ | |
{ | |
"property": "name", | |
"value": "postnord_delivery_today" | |
}, | |
{ | |
"property": "device_class", | |
"value": "" | |
}, | |
{ | |
"property": "icon", | |
"value": "mdi:email-alert" | |
}, | |
{ | |
"property": "unit_of_measurement", | |
"value": "" | |
}, | |
{ | |
"property": "state_class", | |
"value": "" | |
}, | |
{ | |
"property": "last_reset", | |
"value": "" | |
} | |
], | |
"state": "payload", | |
"stateType": "msg", | |
"attributes": [], | |
"resend": true, | |
"outputLocation": "payload", | |
"outputLocationType": "none", | |
"inputOverride": "allow", | |
"outputOnStateChange": false, | |
"outputPayload": "$entity().state ? \"on\": \"off\"", | |
"outputPayloadType": "jsonata", | |
"x": 760, | |
"y": 120, | |
"wires": [ | |
[] | |
] | |
}, | |
{ | |
"id": "e86c7e059831a791", | |
"type": "ha-entity", | |
"z": "21e6c0acf26de00d", | |
"name": "sensor.postnord_delivery_next", | |
"server": "450ad165dd77c3a2", | |
"version": 2, | |
"debugenabled": false, | |
"outputs": 1, | |
"entityType": "sensor", | |
"config": [ | |
{ | |
"property": "name", | |
"value": "postnord_delivery_next" | |
}, | |
{ | |
"property": "device_class", | |
"value": "" | |
}, | |
{ | |
"property": "icon", | |
"value": "mdi:email-fast" | |
}, | |
{ | |
"property": "unit_of_measurement", | |
"value": "" | |
}, | |
{ | |
"property": "state_class", | |
"value": "" | |
}, | |
{ | |
"property": "last_reset", | |
"value": "" | |
} | |
], | |
"state": "payload", | |
"stateType": "msg", | |
"attributes": [], | |
"resend": true, | |
"outputLocation": "payload", | |
"outputLocationType": "none", | |
"inputOverride": "allow", | |
"outputOnStateChange": false, | |
"outputPayload": "$entity().state ? \"on\": \"off\"", | |
"outputPayloadType": "jsonata", | |
"x": 790, | |
"y": 180, | |
"wires": [ | |
[] | |
] | |
}, | |
{ | |
"id": "5e0ba44c2d76fc9c", | |
"type": "comment", | |
"z": "21e6c0acf26de00d", | |
"name": "CHANGELOG", | |
"info": "", | |
"x": 250, | |
"y": 60, | |
"wires": [], | |
"icon": "node-red/alert.svg" | |
}, | |
{ | |
"id": "14f1860186ea11a1", | |
"type": "comment", | |
"z": "21e6c0acf26de00d", | |
"name": "customize: packages/customize/postnord.yaml", | |
"info": "", | |
"x": 750, | |
"y": 60, | |
"wires": [] | |
}, | |
{ | |
"id": "450ad165dd77c3a2", | |
"type": "server", | |
"name": "USE YOUR OWN HOME ASSISTANT SERVER", | |
"version": 2, | |
"addon": false, | |
"rejectUnauthorizedCerts": true, | |
"ha_boolean": "y|yes|true|on|home|open", | |
"connectionDelay": true, | |
"cacheJson": true, | |
"heartbeat": true, | |
"heartbeatInterval": "30" | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ============================================================================= | |
# Postnord API (packages/customize/postnord.yaml) | |
# | |
# friendly_name's för Node-RED flöde "Postnord API". | |
# ============================================================================= | |
# | |
# Filen behöver inkluderas som package i configuration.yaml, exempelvis såhär: | |
# | |
# homeassistant: | |
# packages: !include_dir_merge_named packages/ | |
# | |
# ============================================================================= | |
packages_customize_postnord: | |
homeassistant: | |
customize: | |
# ======================================================================== | |
# Node-RED: [Postnord API] | |
# ======================================================================== | |
binary_sensor.postnord_delivery_today: | |
friendly_name: "Postutdelning idag" | |
sensor.postnord_delivery_next: | |
friendly_name: "Nästa postutdelning" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment