skip to Main Content

I am a newbie in javascript.
I work three shifts and want to control events in home assistant via node-red.
I wrote a short script using various examples, it only works until the end of the table.
I can’t get it to loop, so here I am asking for help.
I want to go back to the beginning of the array after day 20 in the change cycle.

var dayStart = new Date(2023, 2, 4);
const list = ['night1', 'night2', 'night3', 'night4',
    'free_n', 'afternoon1', 'afternoon2', 'afternoon3', 'afternoon4',
    'free_a', 'morning1', 'morning2', 'morning3', 'morning4',
    'free1', 'free2', 'free3', 'free4', 'free5', 'free6'];
const date = new Date;
var day = Math.floor((date.getTime() - dayStart.getTime()) / (1000 * 60 * 60 * 24));
var day = (day + 1);
var change = (list[(day - 1)]);
msg.day = day;
msg.payload = change;
return msg;

2

Answers


  1. you can add a new variable which resets to 0 every time it went over day 20. For example, let’s name the new variable dayArray

    outside the loop, add var dayArray = 0

    and inside the loop you can add dayArray by 1, just like you did to the day variable you have right now, then add an if statement. If dayArray is bigger/equal than 20, then dayArray = 0

    so this is what your code will be:

    var dayStart = new Date(2023, 2, 4);
    const list = ['night1', 'night2', 'night3', 'night4',
        'free_n', 'afternoon1', 'afternoon2', 'afternoon3', 'afternoon4',
        'free_a', 'morning1', 'morning2', 'morning3', 'morning4',
        'free1', 'free2', 'free3', 'free4', 'free5', 'free6'];
    const date = new Date;
    var day = Math.floor((date.getTime() - dayStart.getTime()) / (1000 * 60 * 60 * 24));
    var day = (day + 1);
    var change = (list[dayArray]);
    if(dayArray >= 20){
    var dayArray = 0;
    } else {
    var dayArray = dayArray + 1;
    }
    msg.day = day;
    msg.payload = change;
    return msg;
    

    Please let me know if this works or not, I am not sure of what you’re trying to do with the code, so I answered based of what you asked for in the description

    Edit: After some thinking, why not just put % 20 after the (day – 1). % means remainder after division. so 21/20 is 1 with 1 remainder. so 21 % 20 will return 1. So you can simply just add % 20 after (day – 1)

    Login or Signup to reply.
  2. If I understand correctly, when 20 * k + n days have passed since dayStart, you need the nth element of the array.

    To achieve this, you can use the remainder of the division day / 20. In all programming languages I know, there is what we call the remainder operator. You can find more details here. In JavaScript, you would want to write

    var change = list[day % list.lenght];
    

    Also, I would consider day day + 1 and day - 1 unnecessary because arrays in JavaScript are 0-based and the result saved in day is also 0-based. So, the final code should look like this (including some more improvements to make it clean):

    const startDate = new Date(2023, 2, 4);
    const list = ['night1', 'night2', 'night3', 'night4',
        'free_n', 'afternoon1', 'afternoon2', 'afternoon3', 'afternoon4',
        'free_a', 'morning1', 'morning2', 'morning3', 'morning4',
        'free1', 'free2', 'free3', 'free4', 'free5', 'free6'];
    const today = new Date();
    var daysPassed = Math.floor((today.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24));
    var changeDay = daysPassed % list.length
    var change = list[changeDay];
    // Here, you need to add 1 day, because this will be diplayed to the user and, as we said, this value starts from 0 for the 1st element
    msg.day = daysPassed + 1; // Depending on the implementation you need, you may use changeDay + 1 instead
    msg.payload = change;
    return msg;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search