Have you tried just displaying it? openHAB frameworknormally converts to local timezone on display.
If that does not give you what you expect, itās probably because you update your Item with no timezone given. The framework will assume that is local time.
Maybe you need to append text ā+0000ā to your update to declare it as UTC.
On the risk of sounding stupid: where do I add the +0000?
The item DateTime TeslaM3_since gets the date-time as a string {mqtt="<[mymosquitto:teslamate/cars/1/since:state:default]"}
I assume OH does not know which timezone it is given/assumed⦠which is what you seem to be eluding to.
I read the link you provided, but I am lost⦠it implies I need to work with a rule?!
I just noticed that the item has +1000 attached to it (when I hover over the item in VScode). But this is actually +0000. I want it to display plus 10 hours (which is my local time).
OH can only know if you tell it. Otherwise it will reasonably assume ālocalā time. So it all depends on the mystery string that you are feeding in via MQTT, which is invisible to us here.
If you need to add a timezone on the end, thatās just string manipulation. I canāt recall how to do with OH1.x syntax, but I think youāre just looking to use REGEX to add a fixed string onto your input in the binding configuration.
So I gave up on this approach, using a rule instead.
with the help of this post:
I arrived at this rule:
rule "Convert UTC timestamp to local date-time"
when
Item TeslaM3_since changed
then
var Number EpochFromDateTime = (TeslaM3_since.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli
EpochFromDateTime = EpochFromDateTime + (10 * 60 * 60 * 1000)
val DateTimeType MyDateTimeFromEpoch = new DateTimeType(new DateTime(EpochFromDateTime).toString)
TeslaM3_since.postUpdate(MyDateTimeFromEpoch)
logInfo(logPrefix + "1.01", "Updated time stamp")
end
However, it raised this error:
2021-11-02 14:25:00.109 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Convert UTC timestamp to local date-time': No instant converter found for type: java.math.BigDecimal
I am really peeved⦠what an effort to add four zeros and a plus sign to a MQTT string to get the proper timestamp.
[edit1]
added log entries to rule:
rule "Convert UTC timestamp to local date-time"
when
Item TeslaM3_since changed or
// s m h D M DoW Y
Time cron "0 0/5 * * * ?"
// here: 10 seconds past midnight; use http://www.cronmaker.com/
then
var Number EpochFromDateTime = (TeslaM3_since.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli
logInfo(logPrefix + "1.01", "Updated time stamp")
EpochFromDateTime = EpochFromDateTime + (10 * 60 * 60 * 1000)
logInfo(logPrefix + "1.02", "Updated time stamp")
val DateTimeType MyDateTimeFromEpoch = new DateTimeType(new DateTime(EpochFromDateTime).toString)
logInfo(logPrefix + "1.03", "Updated time stamp")
TeslaM3_since.postUpdate(MyDateTimeFromEpoch)
logInfo(logPrefix + "1.04", "Updated time stamp")
end
resultā¦
2021-11-02 14:39:43.761 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'tesla.rules'
2021-11-02 14:39:49.982 [INFO ] [.smarthome.model.script.TeslaM3.0.01] - System Start: Tesla rules
2021-11-02 14:40:00.013 [INFO ] [.smarthome.model.script.TeslaM3.1.01] - Updated time stamp
2021-11-02 14:40:00.022 [INFO ] [.smarthome.model.script.TeslaM3.1.02] - Updated time stamp
2021-11-02 14:40:00.025 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Convert UTC timestamp to local date-time': No instant converter found for type: java.math.BigDecimal
A caution, unrelated to your time troubles; the remote repository for OH2 has changed. Which means an existing set-up will not be able to add new features. Worse, in the case of some minor disaster OH2 could attempt to refresh all add-ons. Then you will be in deep trouble, because it canāt.
If I were you Iād get ahead of that disaster before it bites you
Okay, thatās good. This is in ISO8601 format which is accepted to update an openHAB Datetime Item.
The Z at the end instructs openHAB that this timestamp is in UTC.
What I would expect to happen here -
OH Item state holds the UTC value (from MQTT)
So thatās what you see in logs, rules etc. together with the Z 0000 zone.
But when you display via sitemap, OH framework autoconverts to local timezone before formatting.
So if your local zone was +1 youād see 00:46 in Basic UI
which should all do as you asked here, without further work.
Thanks⦠what I have right now is the least elegant solution, but it works.
The UTC timestamp via MQTT is displayed as UTC and not corrected for local time (here +1000). The type conversions always did my head in, and based on my post, I am copying other solutions and hope they work for me.
I tried a few things, which did not work, as in throwing errors.
Given the work-around I have (time is not critical to the second or minute for my purpose) is providing a sufficient result.
Thanks for the hint on the OH repository⦠I will leave the paperUI alone, rarely work in karaf, hence, so far so good.
I will eventually set up a new OH3 environment, and recreate my 961 items plus associated rules.
DateTime testtimeA "test time A [%s]"
DateTime testtimeB "test time B [%s]"
test rule to post updates
// post UTC in ISO8601
testtimeA.postUpdate("2021-10-26T23:46:12.040463Z")
// post some offset zone
testtimeB.postUpdate("2021-10-26T23:46:12.040463+10:00")
As expected, the datetime with zone is stored in Item state
2021-11-02 22:32:36.837 [vent.ItemStateChangedEvent] - testtimeA changed from NULL to 2021-10-26T23:46:12.040463+0000
2021-11-02 22:32:36.839 [vent.ItemStateChangedEvent] - testtimeB changed from NULL to 2021-10-26T23:46:12.040463+1000
So we got A in UTC and B in Vladivostok time
But we donāt care about that, want them displayed in local time
Now to the sitemapā¦
(to show both default and formatted times)
Text item=testtimeA
Text item=testtimeA label="A hh mm [%1$tH:%1$tM]"
Text item=testtimeB
Text item=testtimeB label="B hh mm [%1$tH:%1$tM]"
where we can see in action āNever mind the stored timezone, display in local timeā
You might get different results in non-sitemap UI like HABpanel, Iāve no idea.
Now hereās a funny thing ⦠despite the BasicUI display for +01:00 I am actually today in +00:00 UK time - we changed a couple of days ago. Looks like some legacy of summer time, even though OH2 has been rebooted since.
This is going to be a Java thing - raises the important point, Iām sure the local timezone used for display will be taken from Java locale, which may be different to openHAB settings. Unless itās being really clever, and using browser locale?!
Iād never have noticed this other than doing the default display; my logging stamps, ānowā in rules etc. are all correct for +00:00 GMT
Try a default unformatted display of your DateTime, find out what OH thinks is your ālocalā.