What language are you trying to use here? If Rules DSL, that’s not how to define a function (not that you need that function in the first place, you’re doing it the hard way to get the difference between two times). I’d expect you to be getting errors in openhab.log when this file is loaded because it is syntactically incorrect.
First of all, Rules DSL doesn’t really have functions, it has lambdas and you can put a lambda in a global variable but global variables have to go before the rule. See Reusable Functions: A simple lambda example with copious notes for details.
But you don’t need the function. You also don’t need the imports from joda since, Joda no longer exists in OH 3 (which should also generate errors in the logs when this file is loaded).
[[
is syntactically incorrect and you never declare elapsed as a variable, both of which should also generate syntax errors when this file is loaded.
Java ZonedDateTime, which replaced Joda also doesn’t have a millis.
Something like this would be a better approach (I’m just typing this in, it will likely have typos). There might be some logical errors here too because you set the timer for one second but are posting 24 hours worth of seconds minus the difference between now and when start was set which should be exactly 1 seconds since that’s what you scheduled the timer for. So you are not creating a 24 hour timer and you are not measuring a 24 hour timer.
var Timer timer = NULL
var start = now
rule "start timer"
when
Item mySwitch received command ON
then
start = now
timer = createTimer(1000, [ |
Timer_state.postUpdate(Duration.between(now, start).toHours+":"+toMinutesPart+":"+toSecondsPart))
]
end
That will post how much time has elapsed since the Timer was created which I guess kind of makes sense though it seems like you’d want to post that before the Timer goes off, not after.
By default Duration will print out in ISO8601 format which is pretty readable as well so you could use just
Timer_state.postUpdate(Duration.between(now, start).toString)
If for some reason you want to know how much is left of a 24 hour period based on the difference between now and start you’d use
Duration.ofHours(24).minus(Duration.between(now, start)).
But you know how long the Timer is going to take to run ahead of time, so why wait until the timer exits to actually post that?