openhab-core icon indicating copy to clipboard operation
openhab-core copied to clipboard

[persistence] previousState(true) returns current value when there is an unit applied on channel

Open travnick opened this issue 1 year ago • 6 comments

I've changed persistence engine from RRD4j to JDBC (mariadb) sice I've read somewhere that previousState is not working with RRD4j.

Unfortunately, previousState(true) does not work correctly for channels with unit of measurement set.

function PrintCurrentAndLastUpdate(thingName)
{
  var item = itemRegistry.getItem(thingName);
  
  logger.info(thingName + " previous state:\t" + persistence.previousState(item, true));
  logger.info(thingName + " last update:\t" + persistence.lastUpdate(item));
  logger.info(thingName + " current state:\t" + item.getState());
}

PrintCurrentAndLastUpdate("Rtl433_consumed_water");
PrintCurrentAndLastUpdate("Rtl433_time");
PrintCurrentAndLastUpdate("Rtl433_rssi");
2022-09-29 12:47:14.495 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Rtl433_time' changed from 2022-09-29T12:46:44.000+0200 to 2022-09-29T12:47:14.000+0200
2022-09-29 12:47:14.496 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Rtl433_rssi' changed from -0.112511 dBm to -0.213509 dBm

==> /var/log/openhab/openhab.log <==
2022-09-29 12:47:14.521 [INFO ] [org.openhab.rule.water_volume_change] - Rtl433_time previous state:    JdbcItem [name=Rtl433_time, state=2022-09-29T12:46:44.000+0200, timestamp=2022-09-29T12:46:44.457+02:00[Europe/Warsaw]]
2022-09-29 12:47:14.522 [INFO ] [org.openhab.rule.water_volume_change] - Rtl433_time last update:       2022-09-29T12:47:14.492+02:00[Europe/Warsaw]
2022-09-29 12:47:14.522 [INFO ] [org.openhab.rule.water_volume_change] - Rtl433_time current state:     2022-09-29T12:47:14.000+0200
2022-09-29 12:47:14.524 [INFO ] [org.openhab.rule.water_volume_change] - Rtl433_rssi previous state:    JdbcItem [name=Rtl433_rssi, state=-0.213509, timestamp=2022-09-29T12:47:14.496+02:00[Europe/Warsaw]]
2022-09-29 12:47:14.525 [INFO ] [org.openhab.rule.water_volume_change] - Rtl433_rssi last update:       2022-09-29T12:47:14.496+02:00[Europe/Warsaw]
2022-09-29 12:47:14.525 [INFO ] [org.openhab.rule.water_volume_change] - Rtl433_rssi current state:     -0.213509 dBm

without unit set (see rssi):

2022-09-29 12:47:50.420 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Rtl433_time' changed from 2022-09-29T12:47:14.000+0200 to 2022-09-29T12:47:50.000+0200
2022-09-29 12:47:50.420 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Rtl433_rssi' changed from -0.213509 to -0.125042

==> /var/log/openhab/openhab.log <==
2022-09-29 12:47:50.445 [INFO ] [org.openhab.rule.water_volume_change] - Rtl433_time previous state:    JdbcItem [name=Rtl433_time, state=2022-09-29T12:47:14.000+0200, timestamp=2022-09-29T12:47:14.492+02:00[Europe/Warsaw]]
2022-09-29 12:47:50.446 [INFO ] [org.openhab.rule.water_volume_change] - Rtl433_time last update:       2022-09-29T12:47:50.424+02:00[Europe/Warsaw]
2022-09-29 12:47:50.446 [INFO ] [org.openhab.rule.water_volume_change] - Rtl433_time current state:     2022-09-29T12:47:50.000+0200
2022-09-29 12:47:50.447 [INFO ] [org.openhab.rule.water_volume_change] - Rtl433_rssi previous state:    JdbcItem [name=Rtl433_rssi, state=-0.213509, timestamp=2022-09-29T12:47:18.177+02:00[Europe/Warsaw]]
2022-09-29 12:47:50.447 [INFO ] [org.openhab.rule.water_volume_change] - Rtl433_rssi last update:       2022-09-29T12:47:50.421+02:00[Europe/Warsaw]
2022-09-29 12:47:50.447 [INFO ] [org.openhab.rule.water_volume_change] - Rtl433_rssi current state:     -0.125042

Using openHAB 3.3.0

travnick avatar Sep 29 '22 10:09 travnick

Can you show the item definition? It seems that JDBC checks the item and adds a unit but your historic value has no unit.

J-N-K avatar Sep 29 '22 17:09 J-N-K

I have my mqtt channels handled with this:

UID: mqtt:topic:7e6ae66bff:988ecf11b0
label: Rtl 433
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:7e6ae66bff
channels:
  - id: water_timestamp
    channelTypeUID: mqtt:datetime
    label: time
    description: null
    configuration:
      stateTopic: sensor/rtl_433/1946185/time
  - id: water_rssi
    channelTypeUID: mqtt:number
    label: rssi
    description: null
    configuration:
      unit: dBm
      min: -30
      stateTopic: sensor/rtl_433/1946185/rssi
      max: 0
  - id: water_use_total
    channelTypeUID: mqtt:number
    label: total use
    description: null
    configuration:
      unit: m³
      min: 0
      stateTopic: sensor/rtl_433/1946185/data
      transformationPattern: EXEC:/usr/local/bin/exec_wmbusmeters %s∩JSONPATH:$.total_m3
      max: 1000
  - id: water_use_delta
    channelTypeUID: mqtt:number
    label: delta use
    description: null
    configuration:
      unit: dm³
      min: 0
      stateTopic: sensor/rtl_433/1946185/water_use_delta
      max: 1000
  - id: rtl433_raw_event
    channelTypeUID: mqtt:string
    label: Rtl433 raw event
    description: ""
    configuration:
      stateTopic: sensor/rtl_433/events

travnick avatar Sep 30 '22 08:09 travnick

Persistence knows nothing about things or channels, only about items and states. If there is an issue, it's with the item, so I need the item definition.

J-N-K avatar Sep 30 '22 16:09 J-N-K

Where can I find this? Would you like to see the screenshots? I'm using UI, I do not see any .item file (as found in documentation) in a filesystem

openhab-cli info

Version:     3.3.0 (Build)

User:        openhab (Active Process 22307)
User Groups: openhab tty dialout audio wmbusmeters

Directories: Folder Name      | Path                        | User:Group
             -----------      | ----                        | ----------
             OPENHAB_HOME     | /usr/share/openhab          | openhab:openhab
             OPENHAB_RUNTIME  | /usr/share/openhab/runtime  | openhab:openhab
             OPENHAB_USERDATA | /var/lib/openhab            | openhab:openhab
             OPENHAB_CONF     | /etc/openhab                | openhab:openhab
             OPENHAB_LOGDIR   | /var/log/openhab            | openhab:openhab

travnick avatar Oct 05 '22 13:10 travnick

Bildschirmfoto 2022-10-05 um 18 29 43

Something like this would help.

J-N-K avatar Oct 05 '22 16:10 J-N-K

image

travnick avatar Oct 06 '22 08:10 travnick

Which openHAB version is this? I can't find anything that could go wrong there.

J-N-K avatar Oct 15 '22 15:10 J-N-K

as mentioned openhab 3.3.0-1, running on Debian from https://openhab.jfrog.io/artifactory/openhab-linuxpkg stable main

# lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 10 (buster)
Release:        10
Codename:       buster

travnick avatar Oct 18 '22 11:10 travnick

Can you try adding state description Metadata (like %.4f dBm) and see if that helps? This probably the same as #3121, because if no state description is set and no default unit is provided (i.e. the UnitProvider has doesn't know about that dimension, which is the case for "Power"), the states are restored without unit and comparison fails to determine the correct state. It works for me for "Pressure" and "Temperature".

J-N-K avatar Nov 05 '22 13:11 J-N-K