From ddaa9b24b8f09784d5f504d308cd015fafc09f1e Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Sat, 27 May 2023 19:40:19 +0100 Subject: [PATCH] let timeswitch handle high latitudes better to close #1002 --- time/timeswitch/package.json | 6 ++-- time/timeswitch/timeswitch.js | 56 ++++++++++++++++++++++++++++------- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/time/timeswitch/package.json b/time/timeswitch/package.json index 71917d79..52cdad25 100644 --- a/time/timeswitch/package.json +++ b/time/timeswitch/package.json @@ -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", diff --git a/time/timeswitch/timeswitch.js b/time/timeswitch/timeswitch.js index fc5e3c87..731a3642 100644 --- a/time/timeswitch/timeswitch.js +++ b/time/timeswitch/timeswitch.js @@ -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);