Calculate number of hours/minutes between "now" and a future point in time

I would like to calculate dynamically the # hours or minutes between the moment a given flow is started and a known future time.

For example target time is 20.00 and the flow is started at 13.40, number of hours is 6 or 7 depending on the rounding etc.

I cant find any means of doing so. Any app that could help? Or am I missong something? Thx

Check out BLL from fellow developer @Arie_J_Godschalk

Or check out Flow Utilities App for Homey | Homey

Also interesting maybe:

1 Like

Hello @Jux2k2,

Did I understand you question correctly? Atleast one of these three:

  1. 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.

  2. 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.

  3. And finally the Flow Utilities app can do similar things like calculate the elapsed time from a known start time.

1 Like

Hei

Thanks for the help so far.

To your question:
-I do NOT know the start time, it has to be “now”, when the flow is started

-Flow is started by an automatic trigger, so I do not manually control it

-I DO know the end time, an hour (integer).

-My objective is to calculate the period (in hours, integer variable, it will require rounding up or down) so I can use it in other flows.

-Given that the end time is just an hour without date, the duration is below 24hrs :slight_smile:

I guess what I cant find is a card that allows me to identify what “now” is and extract the hour from it…

Better Logic Library can extract the current hour for you.

I’m trying to understand what is you end goal? What do you want to achieve as a result?

here’s a BLL example

Thanks guys, will look into this!

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).

I hope it makes sense :slight_smile:

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
  }
}

@Jux2k2, did you find a solution?

Between work and toddlers I havent had time yet. Will keep the group updated!

3 Likes

calculated the time between two variable moments. you can replace one of the with “now”

Did you check out PBTH?

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.

The script:

// 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;
1 Like

Thanks, this script works great! Exactly what I needed for practically the same usecase.

What do you mean with shrinking the 5 lowest?

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.

Ah clear.

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.

add a timer that counts down when charging and stops when charging stops? The remaing timer you can read out and use I suppose