OH3 Copy state from one item to another in rule

  • Platform information:
    • Hardware: x86 VM
    • OS: Ubuntu 20.04
    • Java Runtime Environment: OpenJDK 11
    • openHAB version: 3.1.0M2
  • Issue of the topic:
    I have the ZoneMinder binding which has an image item ZoneminderMonitorFrontPorch_Image that is updated every 5 seconds. I would like to copy the image to another item when motion is detected so I can remotely see what triggered the latest alarm. The item is manually created (not linked to a thing) and is called ZoneminderMonitorFrontPorch_LastAlarmImage

In OH2 I had the following rule which copied this image to another item when motion was detected.

rule "Front Door Motion"
    Item CameraFrontPorch_Alarm changed to ON
    logInfo("org.openhab","Motion Detected at front door")
    sendNotification("me@email.com", "Motion detected at front door")

I’ve tried recreating this in the OH3 rules engine, but assume when it’s looking for a state to set, you can’t reference another item’s state?

Is the best way round this to create a separate DSL rule in OH3 to copy state from one item to another? Is it worth raising it as an enhancement request?

If you push the code button in the rule editor to see the underlying YAML that configures this rule, you’ll see something like this:

  - id: "1"
      itemName: CameraFrontPorch_Alarm
      state: ON
    type: core.ItemStateChangeTrigger
conditions: []
  - id: "2"
      itemName: ZoneminderMonitorFrontPorch_LastAlarmImage
      state: ZoneminderMonitorFrontPorch_Image.state
    type: core.ItemStateUpdateAction

What this shows you is that the rule is currently treating ZoneminderMonitorFrontPorch_Image.state as a string and is trying to set the state of ZoneminderMonitorFrontPorch_LastAlarmImage to that string. Instead you want an expression so that OH knows that you want it to get a result from some evaluation and then pass that result. I assume that expressions work here in this dialog as they do in the rest of the UI, so in this case that would be something like this:

However, there’s probably a better solution in this case. Based on your OH2 DSL rule, you also have logging and notification actions you would like along with this. With the rule editor you can just port your DSL rule directly into OH3. Instead of choosing Item Action in the rule action popup, choose Run Script

and then select Rules DSL as the script type. You can then just paste or recreate the section of your old rule between the then and end in the text editor popup that appears (changing the information to match your new item names, of course).

1 Like

Using the items[] option didn’t work.

2021-03-06 19:21:37.762 [WARN ] [handler.ItemStateUpdateActionHandler] - State '=items["ZoneminderMonitorFrontPorch_Image"].state' is not valid for item 'ZoneminderMonitorFrontPorch_LastAlarmImage'.

Using DSL throws a huge exception, but does work, so I’ll live with that for now

2021-03-06 19:36:52.034 [ERROR] [ypes.access.impl.AbstractClassMirror] - resource is empty: java:/Objects/org.openhab.core.model.script.actions.Semantics
java.lang.IllegalStateException: null
        at org.eclipse.xtext.common.types.access.impl.AbstractClassMirror.getEObject(AbstractClassMirror.java:95) [bundleFile:?]
        at org.eclipse.xtext.common.types.access.TypeResource.getEObject(TypeResource.java:95) [bundleFile:?]
        at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getEObject(ResourceSetImpl.java:223) [bundleFile:?]

Thanks for the pointer Justin