Okay, I spent quite a bit of time looking at your flow and couldn’t follow the logic, so I’ve changed it around and come-up with this, which also includes day numbers as well.
The output that feeds into the debug node is a 1 if the device controlled by the timer ios on, a 0 of its off.
As this would be sent once every second (the frequency that I’ve set for the timestamp inject node, I’ve added the RBE node that only allows the value to pass if it changes.
Functions:
Get values from Time Input Widget
var start_seconds = parseInt(msg.arrayOfValues[0],10);
var stop_seconds = parseInt(msg.arrayOfValues[1],10);
var active_days = msg.arrayOfValues[3]
flow.set('start_seconds',start_seconds);
flow.set('stop_seconds',stop_seconds);
flow.set('active_days',active_days);
// Update the text on the node to show the data from Blynk
node.status({text: "[ Start " + start_seconds + ", Stop " + stop_seconds + ", Days " + active_days + " ]"});
Seconds since midnight & Day Number
var time = msg.payload;
var th = time.split(':');
var result1 = th[0]*3600+th[1]*60; // result1 is current number of seconds since midnight
var date = new Date();
var str_daynum = date.getDay().toString(); // Get the day of the week (Mon = 1) as a string
flow.set('current_day',str_daynum); // we are using a sring as we want to find it within the list of active days
// Update the text on the node to show the data from Blynk
node.status({text: "[ Time Now = " + result1 + ", Today = " + str_daynum + " ]"});
msg.payload = result1; // output the current number of seconds since midnight
return msg;
Calculate On/Off Status
var current_seconds = msg.payload;
var start_seconds = flow.get('start_seconds');
var stop_seconds = flow.get('stop_seconds');
var current_day = flow.get("current_day");
var active_days = flow.get("active_days")
if (active_days.includes(current_day)) // Check that today is an active day
{
if (current_seconds >= start_seconds && current_seconds < stop_seconds) // if we're within the on/off times then output a 1
{
msg.payload = 1 // ON
}
else
{
msg.payload = 0 // OFF // else output a zero
}
}
else
{
msg.payload = 0 // OFF // we get here if it wasn't an active day, so output a 0
}
// Update the text on the node to show the on/off status
node.status({text: "[ " + msg.payload + " ]"});
return msg; // 1=on, 0=off This will be output once per second
These two nodes in the top right hand corner ensure that the values from the Time Input widget are synchronised when you do a deploy, or when the RPi reboots:
It’s going to be important that your RPI’s time is set correctly, as it needs to be in sync with the time on your mobile device (and I think the Blynk server, but I cant get my head around that now, it’s a bit too late in the day).
I have no idea what effect daylight saving time will have on all of this, but I guess we’ll find out this weekend
Pete.