Help with Difference in Hours:Minutes:Seconds between Dates and Times


#1

I am using various triggers to capture the Dates and Times of various Events {$now} to populate DateTime Variables.

The goal is to output the duration of time between events in Hours:Minutes:Seconds.

{eventstart}={$now}=December 3, 2017 @9:30:00pm
{eventend}={$now}=December 4, 2017 @3:00:00am

{eventend}-{eventstart}=5 Hours and 30 Minutes and 0 Seconds

I tried using the addHours function but I am get large negative day values…

Assume I know nothing about coding! Speaking of, what language does webCoRE use for expressions?

Thanks

Update, Wanted to post this Piston at the top just in case it might be used to help someone else out.

Summary


#2

You probably want to use the formatDuration() function instead.

Something along the lines of

formatDuration(eventend-eventstart, true)

(assuming that your local variables are correctly set) should do what you are looking for.


#3

This is for a Car Charging Piston I have been working on.

formatDuration(eventend-eventstart, true) worked fine when I had my variable set as time…The problem was that my car was still charging after midnight so when the car finished charging, the chargeend time was now smaller than the chargestart time…This caused for the durationtime to be off.

So, I then changed the time variable to datetime to capture the date as well. With the same formula, I am now getting this:

My datetime variable is populated with {$now)

Here’s the Piston:


#4

Why do you have addHours() in the formatDuration() call? That shouldn’t be there…

What do you get when you use chargestop-chargeend? You might have to use formatDuration(int(chargestop-chargeend), true)


#5

Leftover from me trying to fix the duration error… I put the correct formula in a Do section at the start as the section you are referring to will not be executed until the charging event stops.

The last section was my attempt to do an offset if charging was still active at midnight as I though this would be the easiest solution. Once charging stops then reset offset to 0. However, I guess time is in decimal value and I have no idea how to find the value of the offset, which I would assume to be 12hours…


#6


#7

Sorry, I meant formatDuration(), not formatDateTime()

So — your issue clearly is that chargestop-chargestart is negative. Did you re-assign those variables after changing their type?


#8

The variable $now is in milliseconds and does not reset at midnight (or ever, it just keeps increasing indefnintely), making your calculations a bit easier to manage.

Your original will work correctly whether your car charges through midnight and beyond or not.


#9

I tested this logic with a basic Piston. I was scratching my head as to this because it worked well but this was all during the day.However, I tried it overnight and got the message that the car had charged for +19 hours even though it fully charged withing 5 hours. I was thinking that this is because I had time and not datetime. When I try to use date time, I am not getting the desired results in the current Piston even though the I am using the same logic.

I set the variables with the Do section for evaluating. I also set a start value for evaluating. I am thinking that this must be a bug in this Piston?..I am not sure how to re-assign but I did a quick Piston just to check the logic and by all means, it should be working…


#10

Interesting. What were the values of your StartTime and StopTime when the result gave you 19 hours?

Did you have that time offset running for “If time happens daily at midnight” piece in the piston at that time?


#11

No, I didn’t start changing stuff until I got the 19 hours notification. This was the current times when that notification happened.

The current {chargtime} is all over the place and there is something just wrong with the Piston…I don’t know what happened to the piston but it just isn’t working correctly.

Now that I know that I can use datetime and formatDuration and it will result in Days:Hours:Minutes:Seconds, I am just going to rebuild it or maybe try importing it from backup code…


#12

How would I do this? Do you mean go back into any section that has those variables and reclick them?


#13

Before you do all that, I think you might be suffering from something I recently beat my head against the wall trying to sort out:

Try doing your chargetime calculation after the chartstop variable has had some time to settle. Setting a variable and then using its new value in the next instruction was not working in one of my pistons earlier this week either and I was getting some really odd returns.


#14

You could also just use

formatDuration($now-chargestart, true)

there – no need to use the just assigned variable…


#15

Wouldn’t this still need to be an expressing within the variable? Would still would want to see when the charge ended…When I try that, I don’t get a DateTime value but the actual expression.


#16

What if you put a couple instructions between setting chargestop and performing the chargestop - chargestart calculation?

When I was going through the logs it was only 3-5 milliseconds to set a variable… just long enough the next instruction or two might not go according to plan.


#17

I set chargestart to $now and chargestop to $nextMidnight + 1 hour to get over the hump and this was the result (note - you need to swap stop and start or you get a negative value):

image

If you’re getting bogus results from your piston, I’d take a look at the logs to determine what ChargeStop was when the calculation was executed.


#18

Did a restore from backup bin, ran a couple of test, and everything is working now. Don’t know what happened but the Piston somehow got corrupted…

I simulated a 3 hour charge, a 12 hour charge and a 2 day charge and it all worked like it should have.

Thanks again for all of you guys help! I love this stuff but sometimes…yeesh. :tired_face:

Here’s the working Piston! It will populate values once a full charge cycle completes. It will also keep tabs on how much energy is being used and the cost per charge, a weekly running total and a monthly running total.


#19

Glad you got it sorted out!! I’ve had a couple of those times too… once I actually saw an IF condition fail that mathematically should have passed. Even the values in the logs should have been true.

Sometimes all you can do is dump it and start again with a clean plate!


#20

Dragging up this old thread. I can’t find a way to compare date/time values and take action on them. FormatDuration is not helpful because it does not list one mathematical result (e.g. 2,160 minutes instead of “1 d 12 hours 0 minutes” or “1 d 12:00:00”. I would parse the result, but cannot find a suitable function within WebCore. addMinutes and similar functions do not seem helpful unless I am missing something. Time(@variable1 - @variable2)/1000/60 did work, but then I realized it does not cross dates and is literally only comparing the time data without date information.

Am I missing something? I need one mathematical result on the difference between two dates with times (either variables or $now).

Thanks in advance.