Node-Red flow for blinds based on sun position

This is my node-red flow for automating my blinds based on the position of the sun. My home office is south facing, so if you copy this you will need to adjust the Azimuth angles for the direction you window is facing.

The automation starts as soon as the rises above the horizon. The first function saves the current azimuth so it can be refered to later on in the flow and checks to see if it has hit the critical point just before the sun starts to shine directly into the room. Based on the logic, if the sun has passed 165 degrees, it will wait for 5 minutes then get the current azimuth. The next function will check to see if the previously stored azimuth and the latest azimuth are now 5 degrees apart. If not it will wait another 5 minutes, then test again.

Once more than 5 degrees of motion has been detected, the flow will get the current position of the blind. If the blind is still open, the last function subtracts 10 from the blinds position (0 is fully extended 100 fully open) and then passes that to the blind. Once the blind is fully closed, the process stops. Once the sun is beyond 280 degrees, the process stops.

Code in the functions are below. Note that I am not a coder so this is probably terrible:

Save current azimuth:
var azimuth = msg.data.attributes.azimuth;
if (azimuth < "165") {
msg.azimuth = "0";

msg.switch = "reset";
} else {
msg.azimuth = azimuth;
msg.switch = "continue";
}
return msg;

Calculate the azimuth change:
var enow = msg.data.attributes.azimuth;
var eold = msg.azimuth;
var result = (enow - eold);

if (result >= "5" && enow < "280") {
msg.payload = "true";
} else if (enow > "280") {
msg.payload = "end";
} else {
msg.payload = "false";
}
msg.result = result;
return msg;

Update position:
var current = msg.data.attributes.current_position;
if (current > 0) {
msg.newPosition = (current - 10)
} else {
msg.newPosition = 0
}
newMsg = {
payload: {
"data": {
"position": msg.newPosition
}
}
}
return newMsg;