[SOLVED] Epoch Number to Integer

Dear comunity,

I try to convert an epoche number from zigbee2mqtt epoche to datetime to display the date properly. I use mainly this page: DateTime Conversion

When I receive the Number Item for the epoche with Milliseconds (13 digits) the following rule outputs different numbers.
Rule:

logInfo("Zigbee Devices","Aktualisierung DateTime Lastseen, Epoche aus Item: " + Aqara_Balkon_Lastseen_epoche.state.toString)
val int epoche_Millis = (Aqara_Balkon_Lastseen_epoche.state as Number).intValue
logInfo("Zigbee Devices","Aktualisierung DateTime Lastseen, Epoche Millis: " + epoche_Millis.toString)

But the log shows two different numbers:

2020-02-27 18:50:45.267 [INFO ] [marthome.model.script.Zigbee Devices] - Aktualisierung DateTime Lastseen, Epoche aus Item: 1582825530384
2020-02-27 18:50:45.270 [INFO ] [marthome.model.script.Zigbee Devices] - Aktualisierung DateTime Lastseen, Epoche Millis: -2017401840

Probably some easy error but 60 min + google do not help me somehow :thinking:

Happy for the right hint - thanks!
Best
MatzeMuc86

-2017401840 is the 2’s complement of lower 4 bytes of 1582825530384. 1582825530384 does not fit into type int.

Try to use long instead of int:

val long epoche_Millis = (Aqara_Balkon_Lastseen_epoche.state as Number).longValue
logInfo("Zigbee Devices","Aktualisierung DateTime Lastseen, Epoche Millis: " + epoche_Millis.toString)
2 Likes

Thanks - no idea why I have not realized. With long it works perfectly.

It may not help depending on what you are doing exactly, but in recent months the openHAB Datetime type Item has been enhanced to parse epoch timestamps directly.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.