Code to know the execution time of an action until it is blocked

Hi to all
I’m searching a way to know the time from the activation of a action until to block of it.
For Example:

if (LivingRoom_Light2.state == ON) {
	logWarn("Alarm", "The {} dentro", LivingRoom_Light2.state)

    createTimer(now.plusSeconds(24)) [|

//controlla se la serranda è già per un pò abbassata e calcola di quanto
//la deve ancora abbassare	
		    y = (Percentuale.state).intValue
            logWarn("Alarm", "The {} valore di y in ap automat.", Percentuale.state)
            createTimer(now.plusSeconds(y)) [|




When LivingRoom_Light1 swithes to ON i wait for Y seconds and then i command LivingRoom_Light1 to switch to OFF.
But if before of the Y seconds another command turn off LivingRoom_Light1, i want to know how many seconds it take to have this switch to OFF.

I searched the documentation but couldn’t find anything

Can you help me please?

You will need to take a timestamp before you create the Timer. When the second command comes in to turn off the light, check the timestamp and do the math to see how many seconds are left.

I’m using in command now.gotSecondOfDay

Just use now. With getSecondOfDay you will have problems when something starts before midnight and ends after midnight.

i work in a range of 24 seconds.
It is very hard to have problems with this minimal range

Well, you can either write code that rarely has problems, or you can write code that never has problems. If you want code that never has problems, just use now instead of getSecondsOfDay.

but “timestamp” is a command of openhabian?

A timestamp is a generic term for saving when something occurred. In OH rules you just save now to a variable.

var timestamp = now

If i do: timestamp at moment 2 - timestamp at moment 1 = a.
Is the var "a"in seconds?

now is joda datetime
.getMillis is a useful method on datetimes

var t = now


var elapsedSeconds = (now.getMillis - t.getMillis) / 60

i resolved so

       actual_time = (actual_time - ((ActualTime.state as Number).intValue)) 

if (actual_time < 0) {

	actual_time = (86400 - actual_time).intValue


actual_time is = now.getSecondOfDay then the action
ActualTime is = now.getSecondOfDay befor the action
A day is formed by 86400 seconds