Advertisement
K20

Untitled

K20
May 16th, 2022
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let upcomingBins = [];
  2. let gardenDone = false;
  3. let mixedDone = false;
  4. let generalDone = false;
  5.  
  6. msg.payload.forEach((i) => {
  7.   if (i.length > 0) {
  8.     const item = i.split("\n");
  9.     const items = item.map((s) => s.trim());
  10.  
  11.     if (items.includes("Empty Standard Garden Waste") && !gardenDone) {
  12.       gardenDone = true;
  13.       upcomingBins.push(populatePayload(items, "sensor.garden_waste"));
  14.     }
  15.     if (items.includes("Empty Standard Mixed Recycling") && !mixedDone) {
  16.       mixedDone = true;
  17.       upcomingBins.push(populatePayload(items, "sensor.recycling_waste"));
  18.     }
  19.     if (items.includes("Empty Standard General Waste") && !generalDone) {
  20.       generalDone = true;
  21.       upcomingBins.push(populatePayload(items, "sensor.general_waste"));
  22.     }
  23.   }
  24. });
  25.  
  26. function populatePayload(items, id) {
  27.   const d = items[2];
  28.   const displayDate = displayDays(d);
  29.  
  30.   return {
  31.     entity_id: id,
  32.     payload: {
  33.       data: {
  34.         state: items[1],
  35.         attributes: {
  36.             date: d,
  37.             displayDate: displayDate
  38.         }
  39.       }
  40.     }
  41.   };
  42. }
  43.  
  44.  
  45. function displayDays(d) {
  46.     const date = new Date(d.split("/")[2], d.split("/")[1] - 1, d.split("/")[0]);
  47.     const now = new Date();
  48.  
  49.     const diff = Math.floor((Date.parse(date) - Date.parse(now)) / 86400000) + 1;
  50.    
  51.     if(diff === 0) return "Today"
  52.     if(diff === 1) return "Tomorrow"
  53.     return `${diff + 1} days time`
  54. }
  55.  
  56. const slicedArray = upcomingBins.slice(0, 3);
  57. msg.payload = slicedArray;
  58.  
  59. return msg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement