Gå til innhold
  • Bli medlem
Støtt hjemmeautomasjon.no!

Sjekk postleveranse (Node-Red)


hflatoey

Anbefalte innlegg

Flow som sjekkar når du får levert post.

 

Same som Moskus sitt skript i Homeseer, berre tilpassa for Node-Red, håpar det går fint?

Eg har berre teke info frå mybring.com og lagt inn i ChatGPT4, bingo bada bom! Ut kjem ein kode 🙂

 

1. Du treng ein konto hos MyBring. Du kan opprette gratis her (du treng ikkje firmanamn):

https://www.mybring.com/signup/register/user 

 

2. API-nøkkel:

https://www.mybring.com/useradmin/account/settings/api

 

image.thumb.png.7fc1b3200986fe9baf87fc29b8f852c8.png

 

[{"id":"3ed0f98078ca02e6","type":"http request","z":"3dd3d2d8256ec458","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":560,"y":3720,"wires":[["9db2c222c0abe499","c40fb33b184c8e12"]]},{"id":"1484f1a4f28e6944","type":"inject","z":"3dd3d2d8256ec458","name":"","props":[],"repeat":"","crontab":"05 00 * * *","once":false,"onceDelay":0.1,"topic":"","x":240,"y":3720,"wires":[["6b764b240fb78bb5"]]},{"id":"44d94fad163c2ac7","type":"debug","z":"3dd3d2d8256ec458","name":"dates","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":920,"y":3720,"wires":[]},{"id":"9db2c222c0abe499","type":"function","z":"3dd3d2d8256ec458","name":"delivery-dates","func":"const data = JSON.parse(msg.payload);\nconst today = new Date();\nconst tomorrow = new Date();\ntomorrow.setDate(today.getDate() + 1);\n\nconst formatDate = (date) => {\n  const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];\n  const day = days[date.getDay()];\n  const month = date.toLocaleString('en-us', { month: 'long' });\n  return `${day} ${date.getDate()}.${month}`;\n};\n\nconst formattedDates = data.delivery_dates.map((dateStr) => {\n  const date = new Date(dateStr);\n  \n  if (date.toDateString() === today.toDateString()) {\n    return `Today - ${formatDate(date)}`;\n  } else if (date.toDateString() === tomorrow.toDateString()) {\n    return `Tomorrow - ${formatDate(date)}`;\n  } else {\n    return formatDate(date);\n  }\n});\n\nconst nextDeliveryDate = new Date(data.delivery_dates[0]);\nconst msPerDay = 1000 * 60 * 60 * 24;\n\n// Remove the time component from the dates for the comparison\nconst utcToday = Date.UTC(today.getFullYear(), today.getMonth(), today.getDate());\nconst utcNextDelivery = Date.UTC(nextDeliveryDate.getFullYear(), nextDeliveryDate.getMonth(), nextDeliveryDate.getDate());\n\nconst countdown = Math.floor((utcNextDelivery - utcToday) / msPerDay);\n\nreturn [\n  { payload: formattedDates },\n  { payload: countdown }\n];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":750,"y":3720,"wires":[["44d94fad163c2ac7"],["834338375e3770fd"]]},{"id":"834338375e3770fd","type":"debug","z":"3dd3d2d8256ec458","name":"countdown","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":940,"y":3760,"wires":[]},{"id":"6b764b240fb78bb5","type":"function","z":"3dd3d2d8256ec458","name":"user-info","func":"const postalCode = 'postalCode'; // Replace with the desired postal code\nconst email = 'email'; // Replace with your Mybring user account email\nconst apiKey = 'apiKey'; // Replace with your user account's API key\n\nconst today = new Date();\nconst formattedToday = today.toISOString().split('T')[0];\n\n// Set the base URL\nconst url = \"https://api.bring.com/address/api/no/postal-codes/\" + postalCode + \"/mailbox-delivery-dates\";\nconst fullUrl = url + \"?from_date=\" + encodeURIComponent(formattedToday) + \"&look_ahead_days=\" + encodeURIComponent(\"14\");\n\nconst headers = {\n  'X-Mybring-API-Uid': email,\n  'X-Mybring-API-Key': apiKey,\n  'Accept': 'application/json'\n};\n\nreturn {\n    url: fullUrl,\n    headers: headers,\n    payload: msg.payload\n};","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":3720,"wires":[["3ed0f98078ca02e6"]]},{"id":"c40fb33b184c8e12","type":"change","z":"3dd3d2d8256ec458","name":"context-flow-out","rules":[{"t":"set","p":"posten-delivery-date","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":3760,"wires":[[]]},{"id":"132a690db1c4f8b8","type":"change","z":"3dd3d2d8256ec458","name":"content-flow-in","rules":[{"t":"set","p":"payload","pt":"msg","to":"posten-delivery-date","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":3760,"wires":[["9db2c222c0abe499"]]},{"id":"812f4eb131262359","type":"inject","z":"3dd3d2d8256ec458","name":"","props":[],"repeat":"","crontab":"","once":true,"onceDelay":"1","topic":"","x":240,"y":3760,"wires":[["132a690db1c4f8b8"]]}]

 

Lenke til kommentar
Del på andre sider

hflatoey skrev (På 29.4.2023 den 15.03):

Same som Moskus sitt skript i Homeseer, berre tilpassa for Node-Red, håpar det går fint?

Selvfølgelig! 😄

Skulle jo bare mangle, verden går bare fremover hvis vi kan stjele vennskapelig fra hverandre. 🙂 

  • Like 1
Lenke til kommentar
Del på andre sider

Bli med i samtalen

Du kan publisere innhold nå og registrere deg senere. Hvis du har en konto, logg inn nå for å poste med kontoen din.

Gjest
Skriv svar til emnet...

×   Du har limt inn tekst med formatering.   Lim inn uten formatering i stedet

  Du kan kun bruke opp til 75 smilefjes.

×   Lenken din har blitt bygget inn på siden automatisk.   Vis som en ordinær lenke i stedet

×   Tidligere tekst har blitt gjenopprettet.   Tøm tekstverktøy

×   Du kan ikke lime inn bilder direkte. Last opp eller legg inn bilder fra URL.

×
×
  • Opprett ny...

Viktig informasjon

Vi har plassert informasjonskapsler/cookies på din enhet for å gjøre denne siden bedre. Du kan justere dine innstillinger for informasjonskapsler, ellers vil vi anta at dette er ok for deg.