Page 1 of 1

Unixtime ist truncated when exported with mqqt

Posted: 23 Sep 2022, 23:29
by Rini52
all,

The system variable %unixtime% is truncated when exported by the 'Home Assistant (openHAB) MQTT' controller in the Pulse_counter device. I assume that this is caused by the fact somewhere intermediate the value is stored in a 32bit IEEE floating point variable. These 32bit variables can handle about 8 digits while the current unix time contains 10 digits. I tested it by doing a measurement every 20 secs and after 6 measurements the unix time was updated. (see attached log) . As you see from the log the truncation is exactly 128 secs. I hope someone can put some light in the darkness about this?
Regards Rini

Re: Unixtime ist truncated when exported with mqqt

Posted: 23 Sep 2022, 23:58
by TD-er
You're right.

That's why we have the %unixday% and %unixday_sec% which essentially split both into roughly half of the 32 bits needed to store unixtime.

To recombine them into unixtime: %unixday% * 86400 + %unixday_sec%

N.B. see the systemvariables page (button on the tools page) for more ideas/suggestions on other system variables.

Re: Unixtime ist truncated when exported with mqqt

Posted: 24 Sep 2022, 00:33
by Rini52
Td

Spot on! I overlooked this button. I had a look in the documentation(https://www.letscontrolit.com/wiki/inde ... _Variables) and there only the unixtime was mentioned.
Thanks a lot . it solved indeed my problem.

Regard, Have a nice weekend

Re: Unixtime ist truncated when exported with mqqt

Posted: 24 Sep 2022, 00:45
by Rini52
TD,

Sorry i was to quick. It turns out , that producing the same problem . after six intervals its updated. May be you use a 32bit in the export of the mqtt?

Regards

Re: Unixtime ist truncated when exported with mqqt

Posted: 25 Sep 2022, 00:06
by TD-er
How do you now send it to the controller?
Using the number of unixdays and the sec on the current day both fit in roughly 16 a 17 bits. Or at least they can be stored in a float without loosing any information.
The values are kept as a float until they are sent to controller.

So you should not combine them until after they are sent.

If you need to process it as full unixtime in one MQTT message, you can send it via the rules using the publish command.
When sending it from the rules, it is converted to a string and further processed as a string without modifying.
You can trigger sending this via publish, by using the event sent by the task.

Re: Unixtime ist truncated when exported with mqqt

Posted: 25 Sep 2022, 18:22
by Rini52
TD thanks,

I think I will go for option 2 (via rules) . Indeed I did unixday*64600 + unix_sec in the export field of the device ,which is send by mqtt. There apparently you use a 32bit float.
I will test it next week.

Regards
Rini