Conversion of millis to human readable

Hey there=)

My idea is that I want to show the next upcomming Alarm in my sitemap. For this I got the item

Numer AndroidAlarm

which has an state of sth. like this: 1587188700000
This then gets transfered in my rule to the variable nextAlarmOriginal which then has a output sth. like this: 07:45, 18.04 which then gets logged as you can see in the rule:

rule "Android Alarm"
    Item Android_Alarm changed


var nextAlarmOrginal = (new DateTime((Android_Alarm.state as Number).longValue )).toString(“HH:mm, dd.MM.”)

logInfo("Rule triggered", "android_events.rules: Android Alarm: Nächster Alarm: " + nextAlarmOrginal.toString)


This all works fine. However I’d like to show the Date and Time (as formatted for the logInfo) as well in the Sitemap.

As I can see there are two options:

  1. Converting the original Number Item (AndroidAlarm) to formatted DateTime Item usable in the sitemap
  2. Converting the already formatted voriable nextAlarmOriginal to a string Item.

I googled so many times, tried all sorts of convertions and combinations of configurations but I just could not get it to work.

Can anyone please help me? Thanks a lot!

Would you not use a little javascript JS transformation in the [state format] part of your Number Item label?

These might help

The docs were never updated, but the Android app has supported sending the alarm to a DateTimeItem for a while now…

Once you’ve changed this, you can put the formatting into the Item label definition or the Sitemap. If you go a different route, I recommend not using Joda, since it is not avaialble in OH 3.0.

Thanks, that helped me quite a lot. My current code:

rule “Android Alarm”
Item Test_Trigger_For_Testrules changed or
Item Android_Alarm changed
if (Android_Alarm.state.toString == “1970-01-01T00:00:00.000+0000”) {
timerTillAlarm = null
logInfo(“Rule triggered”, “android_events.rules: Android Alarm: Es ist kein Alarm eingestellt”)
var nextAlarmMinusTimespanForEarlierAlarm = new DateTime(Android_Alarm.state.toString)
logInfo(“Rule triggered”, “android_events.rules: Android Alarm: Nächster Alarm: " + Android_Alarm.state.format(”%1$td.%1$tm %1$tH:%1$tM")")


DateTime Android_Alarm “Nächster Wecker[%1$tH:%1$tM Uhr, %1$td.%1$tm.]”

The Alarm works properly and triggers at the right time. I can display the alarm time in the sitemap and it shows the correct time. However the log message doesn’t show the right time. It shows UTC and not +0200 (where I am):

[marthome.model.script.Rule triggered] - android_events.rules: Android Alarm: Nächster Alarm: 18.04 12:25

while the sitemap shows the correct time: 14:25 Uhr, 18.04. .The alarm triggers at the right time (14:25).
How can I adjust the logmessage to show the time in my timezone?

I just the settings in the paperui and there the timezone is configured correctly

What do you get if you add this import to the very top of the rule file…

import java.time.format.DateTimeFormatter

… and then use this…

logInfo(“Rule triggered”, “android_events.rules: Android Alarm: Nächster Alarm: {}", Android_Alarm.getStateAs(DateTimeType).zonedDateTime.format(DateTimeFormatter.ofPattern("HH:mm, dd.MM.")))

This is what you will want to use in OH 3.0 (no more Joda).

this does half the work… :confused:
This is the new output:

android_events.rules: Android Alarm: Nächster Alarm: 14:00, 18.04.

So it formatted the String, but the timezone problem didnt change. I set the alarm for 16:00 and the log says 14:00. So the converting the time didnt work.

I tried a quick and dirty solution like this:

[…]Nächster Alarm: {}", (Android_Wecker.getStateAs(DateTimeType).plusHours(2).zonedDateTime.format(DateTimeFormatter.ofPattern(“HH:mm, dd.MM.”))))

but just got errors with libraries.

Isn’t there a way to just add two hours to the output? However I dont like this solution because it has to be adjusted every 6 month due to the clock change.

1 Like

From the command line what’s the output of timedatectl? If your local time is different then Universal time then you may want to use openhbian-config tool to set the correct time.

The output is:

Local time: Sa 2020-04-18 15:48:46 CEST
Universal time: Sa 2020-04-18 13:48:46 UTC
RTC time: n/a
Time zone: Europe/Berlin (CEST, +0200)

However isn’t this how it is supposed to be just like that?
Even the time says:

Set System Timezone Change the your timezone, execute if it’s not ‘15:50’ now

which it is at this moment. Or do I have to apply changes here?
However OpenHAB knows the right time as it correct at the start of all log entries

Yes, that is correct. :+1:

Is the time and zone in PaperUI set correctly?

I setup an Item to test this with and this appears to be a bug. Habdroid is sending the alarm in UTC, not your local time. I’ll open an issue for this. The workaround is to convert from UTC to your local timezone…

logInfo("Rule triggered", "android_events.rules: Android Alarm: Nächster Alarm: {}", Android_Alarm.getStateAs(DateTimeType).zonedDateTime.withZoneSameInstant((new DateTimeType).zonedDateTime.getZone).format(DateTimeFormatter.ofPattern("HH:mm, dd.MM.")))

With this configuration it works properly! Thanks!

1 Like