Help with javascript function node

Im trying to use a forumla to calc sunset. However when I save it, it has an error on line 9.

var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);

Its complaing that now and start are the wrong type. But when i run the code in js it works fine.... How do i change this to work with node red?

See the function code:

function sunriseset(la, lo, sunrise) {

    const now = new Date();

    const xmonth = now.getMonth() + 1;
    const xyear = now.getFullYear();

    var start = new Date(now.getFullYear(), 0, 0);
    var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);
    var oneDay = 1000 * 60 * 60 * 24;
    var day = Math.floor(diff / oneDay);

    const zenith = 90.83333333333333;
    const D2R = Math.PI / 180;
    const R2D = 180 / Math.PI;

    // convert the longitude to hour value and calculate an approximate time
    const lnHour = lo / 15;
    let t;
    if (sunrise) {
        t = day + ((6 - lnHour) / 24);
    } else {
        t = day + ((18 - lnHour) / 24);
    }

    //calculate the Sun's mean anomaly
    const M = (0.9856 * t) - 3.289;

    //calculate the Sun's true longitude
    let L = M + (1.916 * Math.sin(M * D2R)) + (0.020 * Math.sin(2 * M * D2R)) + 282.634;
    if (L > 360) {
        L = L - 360;
    }
    else if (L < 0) {
        L = L + 360;
    }

    //calculate the Sun's right ascension
    let RA = R2D * Math.atan(0.91764 * Math.tan(L * D2R));
    if (RA > 360) {
        RA = RA - 360;
    }
    else if (RA < 0) {
        RA = RA + 360;
    }

    //right ascension value needs to be in the same qua
    const Lquadrant = (Math.floor(L / (90))) * 90;
    const RAquadrant = (Math.floor(RA / 90)) * 90;
    RA = RA + (Lquadrant - RAquadrant);

    //right ascension value needs to be converted into hours
    RA = RA / 15;

    //calculate the Sun's declination
    const sinDec = 0.39782 * Math.sin(L * D2R);
    const cosDec = Math.cos(Math.asin(sinDec));

    //calculate the Sun's local hour angle
    const cosH = (Math.cos(zenith * D2R) - (sinDec * Math.sin(la * D2R))) / (cosDec * Math.cos(la * D2R));
    let H;
    if (sunrise) {
        H = 360 - R2D * Math.acos(cosH);
    } else {
        H = R2D * Math.acos(cosH)
    }
    H = H / 15;

    //calculate local mean time of rising/setting
    const T = H + RA - (0.06571 * t) - 6.622;

    //adjust back to UTC
    let UT = T - lnHour;
    if (UT > 24) {
        UT = UT - 24;
    }
    else if (UT < 0) {
        UT = UT + 24;
    }

    const ms = UT * 60 * 60 * 1000;

    const time = new Date(ms);
    time.setFullYear(now.getFullYear());
    time.setMonth(now.getMonth());
    time.setDate(now.getDate());
    return time;
}

// sunset
var lat = -34.92866000
var lon = 138.59863000
var sunset = sunriseset(lat, lon, false)

msg.payload = sunset
return msg;

okay, so the code runs in node red just fine! just has this error all the time, how can i clear the error?

At a guess...

const diff = (now.valueOf() - start.valueOf()) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);

awesome thats fixed it :slight_smile:

I assume you are aware that there are multiple nodes that will give you different astronomical times - including sunrise/set for your location?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.