How can I get the Sunset/Rise as a middle node and not just an input?
This, but not as the trigger.
Using the data from Home Assistant I have a problem with the times not quite matching.
So replace that middle node with the one that's the input at the first image.
Make use of function nodes and use code to save a variable that you can re-use.
In "Save Babyroom-heater-state" this is the code:
Code:
flow.set('babyroomheaterswitch', msg.payload);
return msg;
Then in my manage switch I have:
Code:
var switchstate = flow.get('babyroomheaterswitch')||'';
var floatval = parseFloat(msg.payload);
var time = new Date();
var displaytime = time.toLocaleTimeString();
var hour = time.getHours();
if(parseFloat(msg.payload) > 21){
if(switchstate === "ON") {
node.warn("Turning off switch. \nhour is " + hour + " temp is " + parseFloat(msg.payload))
msg.payload = "OFF";
return msg;
} else { return; }
}
if(hour < 7 || hour >= 17){
if(parseFloat(msg.payload) < 20){
if(switchstate === "OFF") {
node.warn("Turning on switch. \nhour is " + hour + " temp is " + parseFloat(msg.payload))
msg.payload = "ON";
return msg;
} else { return; }
}
}
//node.warn("Not changing switch. \nhour is " + hour + " temp is " + parseFloat(msg.payload))
return;
The code above is not important, what is important is that you can set and get variables. "flow" is specific to this flow. You can also save variables that is only available to a specific node, or flow or global.
So what you will do is, have the sun rise/set go to a function flow, then save whatever you looking for to the flow variable in a function node.
Then change your below horizon node to a function node, where you do an if statement based on the flow value with flow.get.
Edit:
So in short, above flow, whenever the switch state changes, I save it to the variable.
Whenever the temperature changes, I get the current switch state, then based on temp and switchstate and time, I do something if all align. If nothing is needed, I just return; which is equal to stop the flow right here.
Edit2:
Or use bigtimer node!
https://flows.nodered.org/node/node-red-contrib-bigtimer