Rules in Openhab 3

  • Platform information:
    • Hardware: RaspberryPi4
    • OS: Openhab3
    • openHAB version: 3…0.3
  • Issue of the topic: please be detailed explaining your issue
  • Please post configurations (if applicable):
    • Items configuration related to the issue
      Number Stromzaehler_Verbrauch_Tag "Verbrauch heute [%.2f kWh]" (Strom)
      Number Stromzaehler_kWh "Zählerstand [%.2f kWh]" (Strom)

    • Sitemap configuration related to the issue

    • Rules code related to the issue

rule "Stromzaehler Verbrauch Tag"
when
Item Stromzaehler_kWh received update
then
Stromzaehler_Verbrauch_Tag.postUpdate(Stromzaehler_kWh.deltaSince(now.withTimeAtStartOfDay))
end

and in the rrd4j.persist database I added
Stromzaehler_kWh : strategy = everyMinute, restoreOnStartup
Stromzaehler_Verbrauch_Aktuell : strategy = everyMinute, restoreOnStartup
Stromzaehler_Speicher_Tag : strategy = everyMinute, restoreOnStartup

I tried to implement the rule over the GUI “Rules” and I tried to add the rule above in “DSL Rule”
Nothing worked.

Could anyone help me please?

The best way to get help for UI generated rules is to post the entire rule structure. Without that we’re only guessing. Click on the Code tab in the upper right of the window when looking at the rule and copy paste the whole text you find there.

In this case, without seeing the full rule structure, the best guess is that you pasted the entire text of the rule into the script window. This isn’t how the UI rules work, because much of the rule structure is already accounted for in the other parts of the rules window. So when converting a DSL rule to a UI rule with a DSL script action you just paste the code of the then section into the action script. The when section of the rule should already be accounted for by the trigger section that you have set.

HI Justion,
thanks for ur replay. I hopefully I understand u correctly.
Here my code from the “Rules” section:

triggers:
  - id: "1"
    configuration:
      itemName: Stromzaehler_kWh
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: >
        Stromzaehler_Verbrauch_Tag.postUpdate(Stromzaehler_kWh.deltaSince(now.withTimeAtStartOfDay))
    type: script.ScriptAction

That one line of code hits on a bunch of different things that could go wrong. Is there anything in the logs?

Beak that long line up into multiple lines, logging out the intermediate steps.

  1. log at the start to verify that the rule is running
  2. log out now.withTimeAtStartOfDay and verify it’s what you expect
  3. Log out what gets returned by the call to deltaSince

Hi rlkoshak,
I need to check how I can log that in the GUI. I will do this asap and will let you know.

Thanks

The same as you log in a .rules file.

Hi Rich,
I get the following error in the console
16:58:31.888 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘910dc4bd36’ failed: Stromzaehler_Verbrauch_Tag. ___ postUpdate(Stromzaehler_kWh.deltaSince(now. ___ withTimeAtStartOfDay))

  1. The method or field withTimeAtStartOfDay is undefined for the type ZonedDateTime; line 1, column 70, length 20
  2. Ambiguous feature call.
    The extension methods
    postUpdate(Item, State) in BusEvent and
    postUpdate(Item, Number) in BusEvent
    both match.; line 1, column 27, length 10

and when I reduce the rule to

Stromzaehler_Verbrauch_Tag.postUpdate(Stromzaehler_kWh)

I get the following error

17:00:43.809 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘910dc4bd36’ failed: Stromzaehler_Verbrauch_Tag.postUpdate( ___ Stromzaehler_kWh)
Type mismatch: cannot convert from NumberItem to State; line 1, column 38, length 16

But both items are “Numbers”

Please post code and logs using code fences.

```
code goes here
```

Well, that’s a pretty clear and easy error to fix. See ZonedDateTime (Java SE 11 & JDK 11 ) for what is and is supported by ZonedDateTime.

Because now.withTimeAtStartOfDay doesn’t exist, the call to deltaSince fails and Rules DSL can’t figure out what you mean in the postUpdate.

Well, that doesn’t make much sense. Stromzaehler_kWh is an Item. You can’t update an Item to another Item. You need to update Items with a State. Maybe try Stromzaehler_kWh.state?

No, as the errors make clear, neither are numbers.