Extract time (HH:mm) out of timestamp

  • Platform information:
    • Hardware: RPI4 4GB
    • OS: openHABian
    • openHAB version: 3.2.0 - Release Build
    • Rules code related to the issue

Hi all, sorry for this post, but I am searching and reading now since 7 hours. Need help, please.
:face_with_peeking_eye:

I’d like to write the max. value of wind speed and the time when it occurred within 8 hours back, to a string item within a rule.
Persistence and everything is working, I am persisting wind speed into a MariaDB.
Therefore in order to read the state and timestamp, I am using maximumSince.

Please find the rule:

var String strMaxWind = ""

rule "Maximale Windgeschwindigkeit der letzten 8h"
when
    Item Windgeschwindigkeit changed
then
    strMaxWind = Windgeschwindigkeit.maximumSince(now.minusHours(8)).state.toString + " um " + Windgeschwindigkeit.maximumSince(now.minusHours(8)).getTimestamp().toString
    maxWindgeschwindigkeit.sendCommand(strMaxWind)
    // logInfo("max. Windgeschwindigkeit", Windgeschwindigkeit.maximumSince(now.minusHours(8)).getTimestamp().toString)
    // logInfo("max. Windgeschwindigkeit", Windgeschwindigkeit.maximumSince(now.minusHours(8)).state.toString)
    
end

The result I get is:

2022-06-08 18:40:35.969 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'maxWindgeschwindigkeit' received command 6 m/s um 2022-06-08T18:24:46.066+02:00[Europe/Berlin]

But what I would like to have is:

2022-06-08 18:40:35.969 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'maxWindgeschwindigkeit' received command 6 m/s um 18:24:46

Can someone please help me to extract 18:24:46 out of 2022-06-08T18:24:46.066+02:00[Europe/Berlin] ?

Yeah well, 1h after initial post I recognized, regex will do… :roll_eyes:

strMaxWind = Windgeschwindigkeit.maximumSince(now.minusHours(8)).state.toString + " um " + 
        transform("REGEX", ".*(([0-9:]{8})+).*", Windgeschwindigkeit.maximumSince(now.minusHours(8)).getTimestamp().toString)

delivers:

2022-06-08 19:36:33.234 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'maxWindgeschwindigkeit' received command 6 m/s um 18:24:46

But maybe someone knows how to format the timestamp? :thinking:

Not tested. That also may work:

Windgeschwindigkeit.maximumSince(now.minusHours(8)).getTimestamp().toString.split("T").get(1).split(".").get(0)

Could be that the dot in “.” needs to be escaped to search for literal dots and not for an expression which matches all characters.

Nice, thanks Wolfgang!
Yes you are right, have to escape the dot:

Windgeschwindigkeit.maximumSince(now.minusHours(8)).getTimestamp().toString.split("T").get(1).split("\\.").get(0)

split("\\.")

1 Like