openhab / openhab-core

Core framework of openHAB
https://www.openhab.org/
Eclipse Public License 2.0
923 stars 424 forks source link

[persistence] previousState(true) does not work with QuantityType #1379

Closed 5iver closed 3 years ago

5iver commented 4 years ago

When using PersistenceExtensions.previousState(true) with an Item that uses QuantityType, it always returns the current state, since historicItem.getState() will never be a QuantityType...

!historicItem.getState().equals(item.getState())

https://github.com/openhab/openhab-core/blob/master/bundles/org.openhab.core.model.persistence/src/org/openhab/core/model/persistence/extensions/PersistenceExtensions.java#L704

Is this a bug or by design? I notice the test doesn't do anything with QuanityType for previousState, so I'm thinking bug.

I simple test is to enable persistence, add an Item without Dimension, add data, then compare previousState(true) to the current state. They will be the different. Now, add a Dimension to the Item and previousState(true) will be the same, without even changing the data.

Number:Length                Test_Number_1            "Test Number 1"
from core.actions import PersistenceExtensions
from core.log import logging, LOG_PREFIX#, log_traceback

LOG = logging.getLogger("{}.TEST_2".format(LOG_PREFIX))
LOG.warn("previousState(True) [{}], state [{}]".format(PersistenceExtensions.previousState(ir.getItem("Test_Number_1"), True).state, items["Test_Number_1"]))

Without Dimension...

2020-02-26 15:54:55.200 [WARN ] [jsr223.jython.TEST_2] - previousState [5.0], state [5.0]
2020-02-26 15:54:55.230 [WARN ] [jsr223.jython.TEST_2] - previousState(True) [55.0], state [5.0]

With Dimension...

2020-02-26 15:55:19.415 [WARN ] [jsr223.jython.TEST_2] - previousState [5.0], state [5.0 in]
2020-02-26 15:55:19.449 [WARN ] [jsr223.jython.TEST_2] - previousState(True) [5.0], state [5.0 in]
cweitkamp commented 4 years ago

The root cause for this issue is that persistence in general is not ready for UoM (see e.g. https://github.com/eclipse/smarthome/issues/5675#issuecomment-396184230). The units are not persisted in legacy persistence add-ons in openhab1-addons. I think https://github.com/openhab/openhab-addons/pull/5275 will change the situation and we can enhance some persistence services.

cweitkamp commented 3 years ago

Will be fixed once this feature has been implemented in all persistence add-ons (see #1954 for more information).