# historicState dont work with jython in OH3

**URL:** <https://community.openhab.org/t/historicstate-dont-work-with-jython-in-oh3/141693>\
**Category:** Scripts & Rules\
**Tags:** jython, openhab3\
**Created:** [December 4, 2022, 9:15am UTC](https://community.openhab.org/t/historicstate-dont-work-with-jython-in-oh3/141693 "2022-12-04T09:15:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dezito](https://community.openhab.org/user_avatar/community.openhab.org/dezito/32/23907_2.png) [@dezito](https://community.openhab.org/u/dezito)\
**Post date:** [December 4, 2022, 9:15am UTC](https://community.openhab.org/t/historicstate-dont-work-with-jython-in-oh3/141693/1 "2022-12-04T09:15:10Z")

</div>

I cant get historicState to work in jython, but averageSince works fine.

I tried to call historicState in rules file, and that works fine.

Jython code:

```auto
@rule(baseName+".System started")
@when("System started")
def test4Started(event):
    from core.actions import PersistenceExtensions
    test4Started.log.info(baseName+" started")
    date = ZonedDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0)
    test4Started.log.info(unicode(date))
    test4Started.log.info(unicode(type(date)))
    test4Started.log.info(unicode(PersistenceExtensions.averageSince(ir.getItem("DayAheadPricesMaxPrice"), date, "influxdb")))
    test4Started.log.info(unicode(PersistenceExtensions.historicState(ir.getItem("DayAheadPricesMaxPrice"), date, "influxdb")))
    

```

log:

```auto
2022-12-04 10:08:17.926 [INFO] [sr223.jython.test4.py.System started] - test4.py started
2022-12-04 10:08:17.928 [INFO] [sr223.jython.test4.py.System started] - 2022-12-04T00:00+01:00[Europe/Paris]
2022-12-04 10:08:17.930 [INFO] [sr223.jython.test4.py.System started] - <type 'java.time.ZonedDateTime'>
2022-12-04 10:08:17.938 [INFO] [sr223.jython.test4.py.System started] - 6.02
2022-12-04 10:08:17.987 [WARN] [jsr223.jython] - Traceback (most recent call last):
  File "/openhab/conf/automation/lib/python/core/log.py", line 96, in wrapper
    return function(*args, **kwargs)
  File "/openhab/conf/automation/lib/python/core/rules.py", line 108, in execute
    self.callback(inputs.get('event'))
  File "/openhab/conf/automation/jsr223/python/personal/test4.py", line 59, in test4Started
    test4Started.log.info(unicode(PersistenceExtensions.historicState(ir.getItem("DayAheadPricesMaxPrice"), date, "influxdb")))
IllegalArgumentException: java.lang.IllegalArgumentException: Cannot format given Object as a Date

```

rules file:

```auto
val date = ZonedDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0)
logInfo("test5.rules", date.toString())
logInfo("test5.rules", "historicState " + date.toString() +" - influxdb: " + DayAheadPricesMaxPrice.historicState(date, "influxdb").state)

```

log:

```auto
2022-12-04 10:04:56.984 [INFO] [penhab.core.model.script.test5.rules] - 2022-12-04T00:00+01:00[Europe/Paris]
2022-12-04 10:04:57.009 [INFO] [penhab.core.model.script.test5.rules] - historicState 2022-12-04T00:00+01:00[Europe/Paris] - influxdb: 5.71

```

---

<div class="post-metadata">

**Author:** ![J-N-K](https://community.openhab.org/user_avatar/community.openhab.org/j-n-k/32/19276_2.png) [@J-N-K](https://community.openhab.org/u/J-N-K)\
**Post date:** [December 4, 2022, 9:59am UTC](https://community.openhab.org/t/historicstate-dont-work-with-jython-in-oh3/141693/2 "2022-12-04T09:59:43Z")

</div>

Maybe you can split the lines so we can see if the issue occurs in `unicode(...)` or in `historicState(...)`.

---

<div class="post-metadata">

**Author:** ![dezito](https://community.openhab.org/user_avatar/community.openhab.org/dezito/32/23907_2.png) [@dezito](https://community.openhab.org/u/dezito)\
**Post date:** [December 4, 2022, 11:03am UTC](https://community.openhab.org/t/historicstate-dont-work-with-jython-in-oh3/141693/3 "2022-12-04T11:03:14Z")

</div>

if i added .state after it worked

```auto
test4Started.log.info(unicode(PersistenceExtensions.historicState(ir.getItem("DayAheadPricesMaxPrice"), date, "influxdb")))
    

```

to

```auto
test4Started.log.info(unicode(PersistenceExtensions.historicState(ir.getItem("DayAheadPricesMaxPrice"), date, "influxdb").state))
    

```
