Is it possible to send channel triggered event from a script?

I’m replacing mihome binding for mqtt (+zigbee2mqtt). I stuck with door sensor. I’ve several rules activated by alarm trigger when a door is opened for a long time. mihome binging has a trigger for it, but zigbee2mqtt does not. I’m trying to keep unchanged as much previous rules as possible. That’s why I created a trigger with same name and need to activate it from a script (javascript). Is anybody know how to do this? Or it is impossible?

How would that help, what would trigger that script?

It activate other rules activating by the trigger without any changes in them. That’s the goal.

You can update an Item to the same state and trigger rules from that.

Why don’t you trigger these other rules from whatever-it-is that you proposing to use for “this” rule? Rules may have more than one trigger.

The main idea is to leave 100500 rules triggered by old alarm trigger unchanged. It is a main idea of the question. I know that I can change the condition of all these rules for something that I have, but it is out of scope of the question.

If I understand correctly, no. A Channel Event Trigger can only come from a Channel. In all other cases that’s what Items are for.

You can have an rule call another rule, but you won’t have event.event which is likely needed by a lot of your rules.

I dug in the OpenHab sources and it seems I found the solution.

var thingEventFactory = Java.type("org.openhab.core.thing.events.ThingEventFactory");
var channelUID = Java.type("org.openhab.core.thing.ChannelUID");
var eventPublisher = Java.type("org.openhab.core.model.script.ScriptServiceUtil").getEventPublisher();
var event=thingEventFactory.createTriggerEvent("<put the expected event value here>", new channelUID("<put the channel id here>"));
eventPublisher.post(event);

It works for me. I’m going to create a small library for it.

I created a small library for it.

It can be used like this in the rule triggered by door/window sensors state changed:

var log = Java.type("org.slf4j.LoggerFactory").getLogger("mak.DoorStateChanged");
this.DEFAULT_TIMEOUT = 300;
this.MIN_TIMEOUT = 30;
this.OPENHAB_CONF = (this.OPENHAB_CONF === undefined) ? java.lang.System.getProperty("openhab.conf") : this.OPENHAB_CONF;
load(this.OPENHAB_CONF + "/automation/lib/javascript/community/timerMgr.js");
this.tm = (this.tm === undefined) ? new TimerMgr() : this.tm;

var OPENHAB_CONF = java.lang.System.getenv("OPENHAB_CONF");
load(OPENHAB_CONF+'/automation/lib/javascript/personal/eventPublisher.js');
this.eventPublisher = (this.eventPublisher === undefined) ? new EventPublisher() : this.eventPublisher;
this.newItem2OldItem = {
  "<new item name>": "<old item UID like mihome:sensor_magnet:xxxxxxxx>"
}

var itemName = event.itemName;
var itemState = event.itemState;

var triggerName;
if(lastUnderscore>-1){
  var thingName = itemName.substring(0, lastUnderscore);
  var oldItemName = newItem2OldItem[thingName];
  if(oldItemName){
    triggerName = oldItemName+":isOpenAlarm";
  }
  if(!triggerName){
    log.error("Unable to find old thing for {}", thingName);
  }
} else {
  log.error("Unable to find thing name of {}.", itemName);
}

if(triggerName && itemState===OPEN) {
  var thingItemTimerName = thingName+"_AlarmTimer";
  var timeout;
  try{
    var timerItem = ir.getItem(thingItemTimerName);
    var timerItemState = timerItem.getState();
    if("NULL" != timerItemState){
      timeout = timerItemState.intValue();
    } else {
      log.warn("There is no value in {}. Default timeout value is used.", thingItemTimerName);
      timeout = this.DEFAULT_TIMEOUT;
    }
  }catch(err){
    log.warn("There is no {}. Default timeout value is used.", thingItemTimerName);
    timeout = this.DEFAULT_TIMEOUT;
  }
  if(timeout<this.MIN_TIMEOUT){
    log.warn("The timeout value in {} ({}) is less than minimum value ({}). Minimum value is used.", 
             [thingItemTimerName, timeout, this.MIN_TIMEOUT]);
    timeout=this.MIN_TIMEOUT;
  }
  this.tm.check(triggerName, timeout*1000, function(){eventPublisher.postChannelTriggerEvent("ALARM", triggerName);}, true);
} else if(triggerName) {
  this.tm.cancel(triggerName);
}

rlkoshak, I like your library. You are free to add the script into it if you find it usefull.

I’m going to move it all to JS Scripting, migrate what makes sense to openhab-js and turn the rest into a Node.js module so it can be installed using npm. I think it might need to be implemented a little differently in that environment. I’d have to take a look and see.