# DateTime Item via Python

**URL:** <https://community.openhab.org/t/datetime-item-via-python/150066>\
**Category:** 3rd Party\
**Tags:** restapi, python\
**Created:** [October 2, 2023, 1:44pm UTC](https://community.openhab.org/t/datetime-item-via-python/150066 "2023-10-02T13:44:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kl8ter](https://community.openhab.org/user_avatar/community.openhab.org/kl8ter/32/28999_2.png) [@kl8ter](https://community.openhab.org/u/kl8ter)\
**Post date:** [October 2, 2023, 1:44pm UTC](https://community.openhab.org/t/datetime-item-via-python/150066/1 "2023-10-02T13:44:55Z")

</div>

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

```csv
DateTime Flower_1_TIME "FC2 LastUpdate" <time> (g1)

```

```auto
    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

```

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [October 2, 2023, 2:17pm UTC](https://community.openhab.org/t/datetime-item-via-python/150066/2 "2023-10-02T14:17:29Z")

</div>

Try calling item.postUpdate instead of assigning directly

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 2, 2023, 2:34pm UTC](https://community.openhab.org/t/datetime-item-via-python/150066/3 "2023-10-02T14:34:06Z")

</div>

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.

> [@jimtng](#):
>
> Try calling item.postUpdate instead of assigning directly

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 🤷 . I’ve not really used HABApp.

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [October 2, 2023, 2:36pm UTC](https://community.openhab.org/t/datetime-item-via-python/150066/4 "2023-10-02T14:36:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Spaceman\_Spiff](https://community.openhab.org/user_avatar/community.openhab.org/spaceman_spiff/32/56883_2.png) [@Spaceman\_Spiff](https://community.openhab.org/u/Spaceman_Spiff)\
**Post date:** [October 5, 2023, 6:02pm UTC](https://community.openhab.org/t/datetime-item-via-python/150066/5 "2023-10-05T18:02:57Z")

</div>

> [@rlkoshak](#):
>
> If this is HABApp

HABApp uses the following import

```python
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.

---

<div class="post-metadata">

**Author:** ![kl8ter](https://community.openhab.org/user_avatar/community.openhab.org/kl8ter/32/28999_2.png) [@kl8ter](https://community.openhab.org/u/kl8ter)\
**Post date:** [October 8, 2023, 5:21pm UTC](https://community.openhab.org/t/datetime-item-via-python/150066/6 "2023-10-08T17:21:34Z")

</div>

> [@rlkoshak](#):
>
> Is this Jython or HABApp?

Python runs on Raspberry Pi4.

> [@rlkoshak](#):
>
> If this is HABApp

No it isn’t: I Use this: [Client Challenge](https://pypi.org/project/python-openhab/)

* * *

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:

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

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

```

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 10, 2023, 3:48pm UTC](https://community.openhab.org/t/datetime-item-via-python/150066/7 "2023-10-10T15:48:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Wolfgang\_S](https://community.openhab.org/user_avatar/community.openhab.org/wolfgang_s/32/52719_2.png) [@Wolfgang\_S](https://community.openhab.org/u/Wolfgang_S)\
**Post date:** [October 10, 2023, 6:37pm UTC](https://community.openhab.org/t/datetime-item-via-python/150066/8 "2023-10-10T18:37:01Z")

</div>

> [@rlkoshak](#):
>
> 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.

see initial announcement [Python module for easily accessing the openHAB REST API](https://community.openhab.org/t/python-module-for-easily-accessing-the-openhab-rest-api/19449/)

---

<div class="post-metadata">

**Author:** ![sim0nx](https://community.openhab.org/user_avatar/community.openhab.org/sim0nx/32/71872_2.png) [@sim0nx](https://community.openhab.org/u/sim0nx)\
**Post date:** [October 10, 2023, 7:50pm UTC](https://community.openhab.org/t/datetime-item-via-python/150066/9 "2023-10-10T19:50:14Z")

</div>

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](https://github.com/sim0nx/python-openhab) 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

```python
    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

```
