Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let upcomingBins = [];
- let gardenDone = false;
- let mixedDone = false;
- let generalDone = false;
- msg.payload.forEach((i) => {
- if (i.length > 0) {
- const item = i.split("\n");
- const items = item.map((s) => s.trim());
- if (items.includes("Empty Standard Garden Waste") && !gardenDone) {
- gardenDone = true;
- upcomingBins.push(populatePayload(items, "sensor.garden_waste"));
- }
- if (items.includes("Empty Standard Mixed Recycling") && !mixedDone) {
- mixedDone = true;
- upcomingBins.push(populatePayload(items, "sensor.recycling_waste"));
- }
- if (items.includes("Empty Standard General Waste") && !generalDone) {
- generalDone = true;
- upcomingBins.push(populatePayload(items, "sensor.general_waste"));
- }
- }
- });
- function populatePayload(items, id) {
- const d = items[2];
- const displayDate = displayDays(d);
- return {
- entity_id: id,
- payload: {
- data: {
- state: items[1],
- attributes: {
- date: d,
- displayDate: displayDate
- }
- }
- }
- };
- }
- function displayDays(d) {
- const date = new Date(d.split("/")[2], d.split("/")[1] - 1, d.split("/")[0]);
- const now = new Date();
- const diff = Math.floor((Date.parse(date) - Date.parse(now)) / 86400000) + 1;
- if(diff === 0) return "Today"
- if(diff === 1) return "Tomorrow"
- return `${diff + 1} days time`
- }
- const slicedArray = upcomingBins.slice(0, 3);
- msg.payload = slicedArray;
- return msg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement