[SOLVED] How to store datetime from python via MQTT into OpenHab

Raspberry Pi - OpenHab 2.3 installed via APT

I have a Python program successfully sending temperature data and motion detection information via MQTT to OpenHab.

I am unable to send date-time data into a DateTime item, I’ve tried various formats, and always see an error

given new state is NULL, couldn't post update for 'moteino_0_date

Items definition
DateTime moteino_0_date "Node 0 updated [%1$tm/%1$td %1$tH:%1$tM]" (Moteino) {mqtt="<[mosquitto:home/status/moteino_0_update:state:default]"}

My understanding, from this topic -

Is that I just need to make sure my python program publishes this in the correct ISO format - so I am using the following in python

datetime.datetime.now().isoformat()

which publishes as, according to mosquitto_sub

home/status/moteino_0_update "2018-08-13T14:54:58.859502

Any help would be appreciated!

Your string is missing the time zone indicator at the end, I am in “Z” for “Zulu” time

Hi - Thanks for the tip, I’ve now tried adding “Z” at the end, but I still get the same error.

I’ve now tried with the following formats
home/status/moteino_0_update "2018-08-13T15:57:04Z"
home/status/moteino_0_update "2018-08-13T14:49:48.474467+00:00"
home/status/moteino_0_update "2018-08-13T15:02:22.921355+00:00Z"

Still get the same error …
When comparing to a binding I have installed, I see datetime items being updated in line with the second example above.

See:

Conversions of items states:
https://www.openhab.org/docs/configuration/rules-dsl.html#datetime-item

Hi - thanks again - I know this page, but I don’t see how this will help in resolving my problem, which is that I am unable to store a date directly into a datetype item via MQTT. Or is it not supported?

Only if the data is properly formatted

The format I use for mqtt is 2018-08-13T16:21:26+0000, that works for me!

Hi @vzorglub and @pacive

Thanks for your help and input - the root cause was the way I was serializing (using json.dumps in Python) and then publishing as key, value … so despite using an apparently valid date, my program was mangling some element (perhaps forcing it to be interpreted as a string?). I’ve now managed to find a more direct way to publish onto mqtt successfully and now Openhab updates the datetype item correctly.

So, indeed, was a formatting issue … of sorts.

Could you publish your solution, please?
For other users. The more documentation about eveything, the better

I think my problem was very esoteric, but here goes.

Moteino (small arduinos with radio) devices publish a very simple stream of values based on attached sensors
A python program loads these values into a json message and publishes over MQTT
I was using the json library to load, and then dump the values out … and somehow mangled the formatting of the json fields
The following gist shows the whole program, now successfully delivering the correct values to Openhab.

https://gist.github.com/nickbalch/81e46f41914360b67a770574b8e1deed

Instead of using .isoformat() you should use .strftime("%Y-%m-%dT%H:%M:%SZ") I think. For parsing correctly from openHAB to a python datetime object I think you have to use as example parser.parse("2022-04-24T06:20:00.402978-0700").