Did I understand you question correctly? Atleast one of these three:
You want to know how much time is on between known start and end times? The the answer could be Better Logic Library to do math between the too times.
Or you only know the end time / target time for something to be ready and want to start a flow X minutes before that target time, then the answer is Alarm Utilities as @Peter_Kawa pointed out. However only the test version currently has the flow card to do the math for you.I expect to get that version published early next week.
And finally the Flow Utilities app can do similar things like calculate the elapsed time from a known start time.
Im setting up a smart EV charging plan with this flow.
From the moment I plug the car I dont care when it charges as long as its charged by 9 am.
I can calculate how many hours of charge are required but still need to find that # of cheapest hours within the period from “now” and 9 am.
Also, I am running a peak shaving program so the charging speed may be drastically reduced to allow for heating to run. Each time the charging current is changed I need to update the charging plan (hence update the period duration).
Here’s another way, a HomeyScript which returns the number of minutes to a target time:
let now = new Date();
let now_local = now.toLocaleTimeString("nl-NL",
{
hour12: false,
timeZone: "Europe/Amsterdam",
hour: "2-digit",
minute: "2-digit",
});
let now_hours = Number(now_local.slice(0,2));
let now_minutes = Number(now_local.slice(3,5));
let target = args[0]; //For example "20:45";
let target_hours = Number(target.slice(0,2));
let target_minutes = Number(target.slice(3,5));
if (target_hours==now_hours && target_minutes==now_minutes){
//Target time is equal to now, so return 0
console.log("Target time is now!");
return 0;
}else{
if ((target_hours>now_hours) || (target_hours==now_hours && target_minutes>=now_minutes)){
console.log("Target time is today!");
return (target_minutes+60-now_minutes)+(target_hours-1-now_hours)*60
}else{
console.log("Target time is tomorrow!");
return (target_minutes+60-now_minutes)+(target_hours+23-now_hours)*60
}
}
Ha, that’s nice. I was struggling with the same problem. I want to charge the car during the 5 cheapest hours of the night. This can be done well with PBTH, but if you enter a fixed value for the period (10 hours, for example) and the price is lower during the day than at night, the 5 hours will continuously lie in the future. So, it is important to narrow the window so that it stops at the end time.
I managed to do it with a script that calculates the number of remaining hours until the deadline. Ideally, I would also like to shrink the 5 lowest, but for now, it meets my needs.
// BepaalUren
const date = new Date();
var resterendeUren;
var doelUur = 8;
var huidigUur = date.getHours()+2; //schijnbaar werkt Homey met universele tijd
if (huidigUur > doelUur){ //het doeluur is morgen
resterendeUren = 24-huidigUur+doelUur;
} else {
resterendeUren = doelUur-huidigUur;
}
return resterendeUren;
At the beginning of the night, the car needs to charge for 5 hours. Now, this 5 hours remains a fixed value, but it should actually decrease throughout the night. So, if the car has already charged for an hour, it should ideally search for the 4 lowest-cost hours. Towards the end of the night, it only needs to charge for 1 or 2 more hours.
In my situation 20% increase in battery level is around 1 hour of charging. Fortunately I’m able to import the current battery level of my car in Homey and with a very simple calculation I’m able to dynamically feed the remaining hours into the PBTH card.
So when the car is connected to the charger Homey calculates every hour:
(target % - actual %)/20
And feed that answer into the PBTH card.
Target % is a variable I choose at the beginning of a charging session with Dummy buttons.