Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[blockly] Add historicState option to Persistence blocks #1309

Closed
digitlength opened this issue Feb 12, 2022 · 7 comments · Fixed by #1617
Closed

[blockly] Add historicState option to Persistence blocks #1309

digitlength opened this issue Feb 12, 2022 · 7 comments · Fixed by #1617
Labels
enhancement New feature or request main ui Main UI

Comments

@digitlength
Copy link

digitlength commented Feb 12, 2022

The problem

At the moment it is not possible to retrieve a single value from persistence without jumping through a hoop or two (see workarounds below).

In code this would use the historicState function.

Your suggestion

Add a feature which enables retrieval of a single value from persistence (re)using a single block.

Current workarounds

OH3.2+

For numerical values only, use the delta function to get the difference between now and the specific point in the past. Then take this value away from the current state of the item.

image

which generates the following relevant code

var current_state, historic_state;
var zdt = Java.type("java.time.ZonedDateTime");
var persistence = Java.type('org.openhab.core.persistence.extensions.PersistenceExtensions');

current_state = itemRegistry.getItem('MyItem').getState();
historic_state = current_state - (persistence.deltaSince(itemRegistry.getItem('MyItem'), zdt.now().minusHours(13)));

OH3.3M1+

The above workaround still works, or use the inline script block to use the historicState function directly, which enables getting the state of any type of Item, not just numerical.

image

which contains the following code

var zdt = Java.type("java.time.ZonedDateTime");

var persistence = Java.type('org.openhab.core.persistence.extensions.PersistenceExtensions');

historic_state = persistence.historicState(itemRegistry.getItem('MyItem'), zdt.now().minusHours(13)).getState();

Your environment

runtimeInfo:
  version: 3.3.0.M1
  buildString: Milestone Build
locale: en-GB
systemInfo:
  configFolder: /etc/openhab
  userdataFolder: /var/lib/openhab
  logFolder: /var/log/openhab
  javaVersion: 11.0.14
  javaVendor: Azul Systems, Inc.
  javaVendorVersion: Zulu11.54+23-CA
  osName: Linux
  osVersion: 5.13.19-2-pve
  osArchitecture: amd64
  availableProcessors: 2
  freeMemory: 108793552
  totalMemory: 314572800
bindings:
  - astro
  - http
  - mqtt

Additional information

@digitlength digitlength added enhancement New feature or request main ui Main UI labels Feb 12, 2022
@stefan-hoehn
Copy link
Contributor

@digitlength
I think this was fixed with #1549
Do you mind checking that and then close this issue?

@digitlength
Copy link
Author

digitlength commented Jan 2, 2023

@digitlength I think this was fixed with #1549 Do you mind checking that and then close this issue?

As far as I can tell, on 3.4.0 Release Build, this issue is not fixed. If it is, please share a screenshot of the block which mimics the behaviour of the inline script example in the OP. I am trying to retrieve a value from persistence from an arbitrary time in history (not just the previous [unique] state).

runtimeInfo:
  version: 3.4.0
  buildString: Release Build
locale: en-GB
systemInfo:
  configFolder: /etc/openhab
  userdataFolder: /var/lib/openhab
  logFolder: /var/log/openhab
  javaVersion: 11.0.17
  javaVendor: Azul Systems, Inc.
  javaVendorVersion: Zulu11.60+19-CA
  osName: Linux
  osVersion: 5.15.64-1-pve
  osArchitecture: amd64
  availableProcessors: 2
  freeMemory: 205030344
  totalMemory: 429916160
  startLevel: 100
bindings:
  - http
  - mqtt
  - solarforecast

@stefan-hoehn
Copy link
Contributor

Ok, so the requirement would be to retrieve a state at a particular point in time which can only be achieved with .historicState(ZonedDateTime) ?

I will put it on my list as soon as #1617 is merged.

@digitlength
Copy link
Author

digitlength commented Jan 7, 2023

Ok, so the requirement would be to retrieve a state at a particular point in time which can only be achieved with .historicState(ZonedDateTime) ?

Correct. This is all about the .historicState function, quoted in the title, that openHAB makes available - third row in the table of extensions in the documentation.

I will put it on my list as soon as #1617 is merged.

Fantastic! Your work is much appreciated!

@stefan-hoehn
Copy link
Contributor

While I had to fix a breaking change in the 4.0 API persistence methods I also added the historic state to it and it will be available as part of #1617 and looks as follows

image

@digitlength
Copy link
Author

@ghys can you explain how I can use this?

Or is this only available in unreleased OH4?

@ghys
Copy link
Member

ghys commented Jan 22, 2023

Or is this only available in unreleased OH4?

It was literally merged 15 minutes ago, so, yes :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request main ui Main UI
Projects
None yet
3 participants