OH3: rule to fast for changing item?

I have a rule, which should parse a JSON sent to an item, which works “normally” fine, but I’m afraid, the rule/script engine is too fast for the changing value, if I look at the logs:

==> /var/log/openhab/events.log <==
2021-01-08 15:34:04.063 [INFO ] [openhab.event.ItemCommandEvent      ] - Item NukiSmartLock_CallbackJSON' received command {"deviceType": 0, "nukiId": 1234, "mode": 2, "state": 3, "stateName": "unlocked", "batteryCritical": "OFF", "batteryCharging": "OFF", "batteryChargeState": 72, "doorsensorState": 2, "doorsensorStateName": "door closed"}
==> /var/log/openhab/openhab.log <==
2021-01-08 15:34:04.068 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'NukiCallbackJSON': Fail to execute action: 2
==> /var/log/openhab/events.log <==
2021-01-08 15:34:04.071 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'NukiSmartLock_CallbackJSON' changed from {"mode":2,"state":3,"stateName":"unlocked","batteryCritical":false,"batteryCharging":false,"batteryChargeState":72,"doorsensorState":3,"doorsensorStateName":"door opened","timestamp":"2021-01-08T14:31:12+00:00"} to {"deviceType": 0, "nukiId": 1234, "mode": 2, "state": 3, "stateName": "unlocked", "batteryCritical": "OFF", "batteryCharging": "OFF", "batteryChargeState": 72, "doorsensorState": 2, "doorsensorStateName": "door closed"}

I know, the update-JSON has a different structure as the old one, I interpret only attributes in both JSONs

triggers:
  - id: "1"
    configuration:
      itemName: NukiSmartLock_CallbackJSON
    type: core.ItemCommandTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >
        var nukicallback = JSON.parse(items["NukiSmartLock_CallbackJSON"]); 
        events.postUpdate("NukiSmartLock_Batteriestatus", nukicallback.batteryChargeState); 
        events.postUpdate("NukiSmartLock_DoorState", nukicallback.doorsensorState); 
        events.postUpdate("NukiSmartLock_LockState", nukicallback.state); 
        events.postUpdate("NukiSmartLock_NiedrigerBatteriestatus", nukicallback.batteryCritical);
    type: script.ScriptAction

Is there a way to have the rule wait for the “changedValue” or something?

Wild guess:
Trigger on the changed event instead on the command.

problem:
I have other rules, which rely on command, because it could be the same value sent again… I then had to make some wild workarounds and set the items to NULL, if the rule is complete…

Then you need to use the value of the command in your rule.
You can get at that with an implicit variable.
You cannot rely on a command to an Item causing any state change at all, let alone an instant change.

This is not a bug or defect, it’s how openHAB is supposed to work.
Send a command UP to a shutter.
Some time later
The shutter announces its new state, fully open.

I’ll think so, but how can I achieve this in OH3-GUI styled ECMA?

Looks like examples here

1 Like

ah. cool. It’s so easy: event.itemCommand

Check here for all available event properties:

https://openhab-scripters.github.io/openhab-helper-libraries/Guides/Event%20Object%20Attributes.html

I use this as my template rule script:

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var NotificationAction = org.openhab.io.openhabcloud.NotificationAction;
var ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution");
var ZonedDateTime = Java.type("java.time.ZonedDateTime");

this.myVar = (this.myVar === undefined) ? "whatever": this.myVar;
this.myTimer = (this.mytimer === undefined) ? null: this.myTimer;

//logger.info("event: " + this.event);
//NotificationAction.sendNotification("my@email.tld","Message");
//NotificationAction.sendBroadcastNotification("Message");

/*
this.myTimer = ScriptExecution.createTimer(ZonedDateTime.now().plusMinutes(1), function(){
                  // do something
                  this.myTimer = null;
                });
*/

if((typeof this.event) == 'undefined'){
  //cron event?

}else{
  if(this.event.itemName !== undefined){
    //multiple triggers ?
    switch(this.event.itemName.toString()){
      case 'myItem':
        //command
        if(this.event.itemCommand !== undefined){
          //do something
        }
        //update
        if(this.event.itemState !== undefined){
          //do something
        }
        break;
    }
  }
  if(this.event.channel !== undefined){
    //multiple triggering channels?
    switch(this.event.channel.toString()){
      case 'TriggeringChannel':
        //do something
        // 'value' is available in this.event.this.event
        break;
  }
}

this.event = undefined;
2 Likes

That’s a great template! :wink: I’d add

var NotificationAction = org.openhab.io.openhabcloud.NotificationAction;
NotificationAction.sendNotification("my@email.com", text);
NotificationAction.sendNotification("my@email.com", text, icon, severity); // both additional values have to be set in parallel

I used notification in another rule but now I added them to the template and also some logic for channel triggers.

1 Like