Next-gen rules with persistence?

Hello,

I’ve created a very simple rule in the UI. I want to update an item state with persistence information. This is my code:

triggers:
  - id: "1"
    configuration:
      cronExpression: 0 0/1 * 1/1 * ? *
    type: timer.GenericCronTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      itemName: DSMR_Gas_Verbruik_Actueel
      state: DSMR_Gas_Geleverd.deltaSince(now.minusMinutes(5))
    type: core.ItemStateUpdateAction

Unfortunately the target item does not get updated. I do not see any errors in the logs.

Does the next-gen rules work with persistence like I’ve used it?

Regards,
Bastiaan

Pure UI rules do not support running code. As written that means you are trying to update DSMR_GAS_Verbruik_Actueel with the literal string “DSMR_Gas_Geleverd.deltaSince(now.minusMinutes(5))”,.

If you want to execute code, for example, execute DSMR_Gas_Geleverd.deltaSince(now.minusMinutes(5)) to get the change in the states value between now and five minutes ago, you have to use a Script Action. You can use Rules DSL and just add one line as the code to execute.

DSMR_Gas_Verbruik_Actueel.postUpdate(DSMR_Gas_Geleverd.deltaSince(now.minusMinutes(5)))

Pure UI rules are simple simple simple and only able to handle the most basic of use cases. A Script Action using Blockly can handle more complex use cases and soon will support persistence calls but not yet. For everything else you still need to write code.

Ok, that makes more sense now. It indeed works when moving to DSL rule. Thanks!

I’m trying to migrate all my text files to the UI, items, rules, things ext… I have more multi-line complex rules, would you recon to move it to the UI with DSL script rules or keep them in text files.

That’s completely up to you. I find that UI rules open some opportunities that are not available in text files like the separate “but only if…” section and the ease in disabling, enabling, and calling other rules. It’s also the only way to write rule templates which I’m very interested in.

But if you are happy with your rules as they are, why do work you don’t have to. Keep them as they are (with the required updates to deal with the breaking changes). You don’t have to change your approach unless you want to; you see some benefit from it.