let timeswitch handle high latitudes better

to close #1002
This commit is contained in:
Dave Conway-Jones 2023-05-27 19:40:19 +01:00
parent a5faf13c4d
commit ddaa9b24b8
No known key found for this signature in database
GPG Key ID: 1DDB0E91A28C2643
2 changed files with 48 additions and 14 deletions

View File

@ -1,10 +1,10 @@
{
"name" : "node-red-node-timeswitch",
"version" : "1.0.0",
"version" : "1.1.0",
"description" : "A Node-RED node to provide a simple timeswitch to schedule daily on/off events.",
"dependencies" : {
"spacetime": "^7.4.0",
"suncalc": "^1.8.0"
"spacetime": "^7.4.3",
"suncalc": "^1.9.0"
},
"repository" : {
"type":"git",

View File

@ -46,9 +46,16 @@ module.exports = function (RED) {
// all sun events for the given lat/long
const sunEvents = SunCalc.getTimes(nowNative, node.lat, node.lon);
const sunAlt = SunCalc.getPosition(nowNative, node.lat, node.lon).altitude;
let sunriseDateTime = spacetime(sunEvents[SUNRISE_KEY]).nearest("minute");
let sunsetDateTime = spacetime(sunEvents[SUNSET_KEY]).nearest("minute");
var aday
if (sunEvents[SUNRISE_KEY] == "Invalid Date" || sunEvents[SUNSET_KEY] == "Invalid Date") {
if (sunAlt >= 0) { aday = 1; }
else { aday = 0; }
}
// add optional sun event offset, if specified
sunriseDateTime = sunriseDateTime.add(Number(node.sunriseOffset), "minutes");
sunsetDateTime = sunsetDateTime.add(Number(node.sunsetOffset), "minutes");
@ -126,19 +133,36 @@ module.exports = function (RED) {
// var o = nextTime.goto(selectedTimeZone.name).offset()/60;
// if (o > 0) { o = "+" + o; }
// else {o = "-" + o; }
if (payload == 1) {
node.status({
fill: "yellow",
shape: "dot",
text: `on until ${nextTime.goto(selectedTimeZone.name).format("time-24")}`
});
if (nextTime) {
if (payload == 1) {
node.status({
fill: "yellow",
shape: "dot",
text: `on until ${nextTime.goto(selectedTimeZone.name).format("time-24")}`
});
} else {
node.status({
fill: "blue",
shape: "dot",
text: `off until ${nextTime.goto(selectedTimeZone.name).format("time-24")}`
});
}
} else {
node.status({
fill: "blue",
shape: "dot",
text: `off until ${nextTime.goto(selectedTimeZone.name).format("time-24")}`
});
if (payload == 1) {
node.status({
fill: "yellow",
shape: "dot",
text: `on`
});
} else {
node.status({
fill: "blue",
shape: "dot",
text: `off`
});
}
}
var msg = {};
if (node.mytopic) {
msg.topic = node.mytopic;
@ -186,6 +210,16 @@ module.exports = function (RED) {
return;
}
if (proceed && aday === 1) {
sendPayload(1);
return;
}
if (proceed && aday === 0) {
sendPayload(0);
return;
}
// if the chronological order is NOW --> ON --> OFF, then now should be OFF
if (proceed && selectedOffTime.isAfter(selectedOnTime)) {
sendPayload(0, selectedOnTime);