DateTime Item via Python

Hello all,
how can I change/write the value of a DateTime item via Python? Here is my script:

DateTime Flower_1_TIME      "FC2 LastUpdate"                     <time>                (g1)
    from openhab import OpenHAB
    UpdateTime = dt.datetime.now()
    UpdateTime = UpdateTime.strftime("%Y-%m-%dT%H:%M:%S.000+0200")
    print("UpdateTime:",UpdateTime)
    url = 'http://192.168.XXX.X:8080/rest'
    openhab = OpenHAB(url)

    item = openhab.get_item('Flower_1_TIME')
    item.state = UpdateTime

Try calling item.postUpdate instead of assigning directly

Is this Jython or HABApp?

If Jython, I recommend against any new development using that. It is our most at risk add-on and it’s a near guarantee that it will stop working at some point. Maybe soon, maybe years from now. But for all intents and purposes it’s no longer maintained.

What’s with the URL and HTTP stuff?

Are you using the Jython Helper Library?

If this is HABApp, please adjust your tags and title of post so it’s clear that’s what you are referring to.

Assuming this is Jython, actually there isn’t a postUpdate nor sendCommand on the Item. You have to use the event bus actions event.postUpdate('ItemName", "NewStateAsString"). If it’s HABApp :person_shrugging: . I’ve not really used HABApp.

Oops indeed it appears to be habapp, posted in 3rd party.

HABApp uses the following import

from HABApp import XXX
# or
import HABApp

This is some other 3rd party library to interact with the rest api.
It’s not a rule engine but a thin wrapper to make rest calls with python easier.

1 Like

Python runs on Raspberry Pi4.

No it isn’t: I Use this: python-openhab · PyPI


Now I have an workaround:

I take the content from a DateTime Item. In my case is it the Local Time Item:
And put it in another item.

Here my code:

    item = openhab.get_item('Date')
    UpdateTime = item.state

    item = openhab.get_item('Flower_1_TIME')
    item.state = UpdateTime

I have no idea who wrote nor maintains that library. I’ve never heard of it nor seen anything posted about it on this forum. It’s definitely not something that’s officially known nor supported by the OH project itself.

Because there are at least two other Python ways to interact with OH, it helps us a lot to understand what you are asking about.

see initial announcement Python module for easily accessing the openHAB REST API

1 Like

Hi @kl8ter

I am the python-openhab author, feel free to seek help at GitHub - sim0nx/python-openhab: python library for accessing the openHAB REST API regarding this lib.

The issue in your initial post is:

UpdateTime = UpdateTime.strftime(“%Y-%m-%dT%H:%M:%S.000+0200”)

You must not convert a datetime to str, but instead use the datetime directly, which is what you did in your workaround :-).

So this will work

    from openhab import OpenHAB
    UpdateTime = dt.datetime.now()
    print("UpdateTime:",UpdateTime)
    url = 'http://192.168.XXX.X:8080/rest'
    openhab = OpenHAB(url)

    item = openhab.get_item('Flower_1_TIME')
    item.state = UpdateTime
1 Like