Thing Status Reporting [3.2.0;3.4.9]

It was ecma 21 in text based script action.
Today I tried again in blockly and… it worked on the first try :flushed:. I cannot tell what I did wrong.
Now it works with your template rule. :slight_smile:

Here the code (for pasting in code-tab of new rule):

Click to see code
configuration: {}
triggers: []
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      blockSource: "<xml
        xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable
        id=\"]8Kt_8cXjoij*UWfL]}Z\">ignore</variable><variable
        id=\"C,Wl8e|_BY7`@|WNRR)t\">things</variable><variable
        id=\"Qzn]o6PxTzj]cn8bnT2k\">messageText</variable><variable
        id=\"n0X6;%Q,_c=4$=`tHt^@\">ignoreList</variable><variable
        id=\"%x2mOh`(Hd/#(A4O/k(]\">curThing</variable><variable
        id=\"E%{F|_6VUVE]s;x=yceK\">thingId</variable><variable
        id=\"oJm$Rz.ax{=5CWVHpP6o\">thingLabel</variable><variable
        id=\"HrLFiEjH(SOud$zz{9iv\">thingStatus</variable></variables><block
        type=\"oh_log\" id=\"{6Nhb|W/OGmd8@)Fp4@b\" disabled=\"true\" x=\"55\"
        y=\"-618\"><field name=\"severity\">debug</field><value
        name=\"message\"><shadow type=\"text\"
        id=\"`*H5u4}v%f/UR]t5^Aqs\"><field name=\"TEXT\">Enter IDs to ignore in
        next line (comma seperated)</field></shadow></value><next><block
        type=\"variables_set\" id=\"A`J${j$nZ_dOHC]+d`GI\"><field name=\"VAR\"
        id=\"]8Kt_8cXjoij*UWfL]}Z\">ignore</field><value name=\"VALUE\"><block
        type=\"text\" id=\"i:tQdMWT($M2/|(JSPl9\"><field
        name=\"TEXT\">example_ID1,exampleID2</field></block></value><next><block
        type=\"variables_set\" id=\"{~WTIN2e_$f6q3SA6-Yv\"><field name=\"VAR\"
        id=\"C,Wl8e|_BY7`@|WNRR)t\">things</field><value name=\"VALUE\"><block
        type=\"oh_context_attribute\" id=\"|H7Nbd2VaPs{W;)0~AC?\"><value
        name=\"key\"><shadow type=\"text\" id=\"K}t%;y/xn8{VP1ePVqF6\"><field
        name=\"TEXT\">things</field></shadow></value></block></value><next><blo\
        ck type=\"variables_set\" id=\"lI{JZ]zqeFw9P*HeI0t^\"><field
        name=\"VAR\" id=\"Qzn]o6PxTzj]cn8bnT2k\">messageText</field><value
        name=\"VALUE\"><block type=\"text\" id=\"/Let0^K`uHs}:3#7^N2p\"><field
        name=\"TEXT\"></field></block></value><next><block
        type=\"variables_set\" id=\"3Ld5v:`q))oi8DB;w=K?\"><field name=\"VAR\"
        id=\"n0X6;%Q,_c=4$=`tHt^@\">ignoreList</field><value
        name=\"VALUE\"><block type=\"lists_split\"
        id=\"r3}xou^n;{KjY6dl0$RD\"><mutation mode=\"SPLIT\"></mutation><field
        name=\"MODE\">SPLIT</field><value name=\"INPUT\"><block
        type=\"variables_get\" id=\"(4kit^zIqe9-b/b7M4E*\"><field name=\"VAR\"
        id=\"]8Kt_8cXjoij*UWfL]}Z\">ignore</field></block></value><value
        name=\"DELIM\"><shadow type=\"text\" id=\"{CJs^j|p$=Y2x.Friu_p\"><field
        name=\"TEXT\">,</field></shadow></value></block></value><next><block
        type=\"oh_log\" id=\"V67tNp:rbdm+Y}L/x,4o\"><field
        name=\"severity\">debug</field><value name=\"message\"><shadow
        type=\"text\" id=\"i77BH#sZ2AL=2nNh=4Hb\"><field
        name=\"TEXT\">abc</field></shadow><block type=\"oh_context_attribute\"
        id=\"ItP}])9xOd?Z/v6/F)Py\"><value name=\"key\"><shadow type=\"text\"
        id=\"8=q=T@jmhGwC1D2tk#uR\"><field
        name=\"TEXT\">things</field></shadow></value></block></value><next><blo\
        ck type=\"controls_forEach\" id=\"!*_/[dld@qMo~k.%wH8:\"><field
        name=\"VAR\" id=\"%x2mOh`(Hd/#(A4O/k(]\">curThing</field><value
        name=\"LIST\"><block type=\"variables_get\"
        id=\"{;vLL.})j{+K}2-,fUf8\"><field name=\"VAR\"
        id=\"C,Wl8e|_BY7`@|WNRR)t\">things</field></block></value><statement
        name=\"DO\"><block type=\"oh_script_inline\"
        id=\"dad|U8=9dRN@P@6#vrHj\"><field name=\"inlineScript\">thingLabel =
        curThing.getLabel()&amp;#10;thingId =
        curThing.getUID().getId()&amp;#10;thingStatus =
        curThing.getStatus()</field><next><block type=\"controls_if\"
        id=\"mxJbf8]-5BmqywT7W5sf\"><value name=\"IF0\"><block
        type=\"logic_compare\" id=\"DAUiQW`ij2Yv[Dy$Fk.c\"
        inline=\"false\"><field name=\"OP\">EQ</field><value name=\"A\"><block
        type=\"lists_indexOf\" id=\"rv,~hIA.]{~wKDNOjtgR\"><field
        name=\"END\">FIRST</field><value name=\"VALUE\"><block
        type=\"variables_get\" id=\"6=$+}$AWvB)[v-ULY%0.\"><field name=\"VAR\"
        id=\"n0X6;%Q,_c=4$=`tHt^@\">ignoreList</field></block></value><value
        name=\"FIND\"><block type=\"variables_get\"
        id=\"gPiC8%I:1C,ClMm@E+)h\"><field name=\"VAR\"
        id=\"E%{F|_6VUVE]s;x=yceK\">thingId</field></block></value></block></va\
        lue><value name=\"B\"><block type=\"math_number\"
        id=\"/)KV?LC6;7ebjx|rMe#t\"><field
        name=\"NUM\">0</field></block></value></block></value><statement
        name=\"DO0\"><block type=\"text_append\"
        id=\"J;;7GOyh~?h@QmS$GpfU\"><field name=\"VAR\"
        id=\"Qzn]o6PxTzj]cn8bnT2k\">messageText</field><value
        name=\"TEXT\"><shadow type=\"text\"><field
        name=\"TEXT\"></field></shadow><block type=\"text_join\"
        id=\"|4xA3YmbBj_/WySmg;b%\" inline=\"false\"><mutation
        items=\"8\"></mutation><value name=\"ADD0\"><block type=\"oh_text_crlf\"
        id=\"R=RwT3r2c!MldtMCh~7!\"></block></value><value name=\"ADD1\"><block
        type=\"text\" id=\"qRfw,*rM9t]f]-k:QuO~\"><field
        name=\"TEXT\">____________________________</field></block></value><value
        name=\"ADD2\"><block type=\"oh_text_crlf\"
        id=\".6xin}kznn2)oLg+PQHw\"></block></value><value name=\"ADD3\"><block
        type=\"variables_get\" id=\"_~:^T3ya1r+kmE=;[!hA\"><field name=\"VAR\"
        id=\"oJm$Rz.ax{=5CWVHpP6o\">thingLabel</field></block></value><value
        name=\"ADD4\"><block type=\"text\" id=\",hu(4Y3uo%@~MBdyohQx\"><field
        name=\"TEXT\"> (</field></block></value><value name=\"ADD5\"><block
        type=\"variables_get\" id=\"G{1T=[t4mL,J]J|)WHU!\"><field name=\"VAR\"
        id=\"E%{F|_6VUVE]s;x=yceK\">thingId</field></block></value><value
        name=\"ADD6\"><block type=\"text\" id=\"lQ-81UfA^x01nOhBe8tV\"><field
        name=\"TEXT\">): </field></block></value><value name=\"ADD7\"><block
        type=\"variables_get\" id=\"-)va(!nQ2Pe@%?I3@M4V\"><field name=\"VAR\"
        id=\"HrLFiEjH(SOud$zz{9iv\">thingStatus</field></block></value></block>\
        </value></block></statement></block></next></block></statement><next><b\
        lock type=\"controls_if\" id=\"4l=bGRXqL*m#4st3WbIV\"><value
        name=\"IF0\"><block type=\"logic_negate\"
        id=\";TGEaI7pk;r.Wd`XTB%m\"><value name=\"BOOL\"><block
        type=\"text_isEmpty\" id=\"+|%Pzk,zFYtfTEN;/b6.\"><value
        name=\"VALUE\"><shadow type=\"text\" id=\"eP+GnzQ;!+qFSR:f6Z@F\"><field
        name=\"TEXT\"></field></shadow><block type=\"variables_get\"
        id=\"}eT[MPm),_,qbA._-|nz\"><field name=\"VAR\"
        id=\"Qzn]o6PxTzj]cn8bnT2k\">messageText</field></block></value></block>\
        </value></block></value><statement name=\"DO0\"><block
        type=\"telegram_with_chatID_message\" id=\"%?.)2$rZJ)1$`[CGZ9qP\"
        inline=\"false\"><value name=\"MESSAGE\"><shadow type=\"text\"
        id=\"%jIrNi@b+!)=WA-_P8,8\"><field name=\"TEXT\">What's
        up?</field></shadow><block type=\"text_join\"
        id=\"+:B^y;:|mtkaD2WvQ-/;\"><mutation items=\"2\"></mutation><value
        name=\"ADD0\"><block type=\"text\" id=\"Vq%.RspM!Sb]4j|oA!Yq\"><field
        name=\"TEXT\">*Things not online:*</field></block></value><value
        name=\"ADD1\"><block type=\"variables_get\"
        id=\";tdF%Ltj{[p+U`Yxi09~\"><field name=\"VAR\"
        id=\"Qzn]o6PxTzj]cn8bnT2k\">messageText</field></block></value></block>\
        </value><value name=\"BOT\"><shadow type=\"oh_thing\"
        id=\"!e*,mpQ{}pF?C[R{m==`\"><field
        name=\"thingUid\">telegram:telegramBot:Telegram-Bot</field></shadow></v\
        alue><value name=\"CHAT_ID\"><shadow type=\"math_number\"
        id=\"Atq3IW%ItA[[RXD}bR/0\"><field
        name=\"NUM\">-123</field></shadow></value></block></statement></block><\
        /next></block></next></block></next></block></next></block></next></blo\
        ck></next></block></next></block></xml>"
      type: application/javascript
      script: >
        var ignore, things, messageText, ignoreList, curThing, thingId,
        thingLabel, thingStatus;

        var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
        var things2 = Java.type('org.openhab.core.model.script.actions.Things');

        ignore = 'example_ID1,exampleID2';
        things = ctx['things'];
        messageText = '';
        ignoreList = ignore.split(',');
        logger.debug(ctx['things']);
        for (var curThing_index in things) {
          curThing = things[curThing_index];
          thingLabel = curThing.getLabel()
          thingId = curThing.getUID().getId()
          thingStatus = curThing.getStatus()
          if (ignoreList.indexOf(thingId) + 1 == 0) {
            messageText += String(['\r\n','____________________________','\r\n',thingLabel,' (',thingId,'): ',thingStatus].join(''));
          }
        }

        if (!!messageText.length) {
          things2.getActions('telegram', 'telegram:telegramBot:Telegram-Bot').sendTelegram(-123, '*Things not online:*' + String(messageText));
        }
    type: script.ScriptAction

And the screenshot:

Thanks Rich for pointing me in the right direction.

2 Likes

ECMA 2021+ version of @Larsen blocky script.

configuration: {}
triggers: []
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      type: application/javascript;version=ECMAScript-2021
      script: >-
        var ignore, things, messageText, ignoreList, curThing, thingId,
        thingLabel, thingStatus;

        var logger = log('Thing Status Processor');
        ignore = 'ThingID1,ThingID2';
        messageText = '';
        ignoreList = ignore. Split(',');
        logger.debug(things);

        for (var curThing_index in things) {
          curThing = things[curThing_index];
          thingLabel = curThing.label;
          thingId = curThing.UID.id;
          thingStatus = curThing.status;
          if (ignoreList.indexOf(thingId) + 1 == 0) {
            messageText += String(['\r\n','_'.repeat("${thingLabel.length + thingId.lenght + thingStatus.length}".length),'\r\n',thingLabel,' (',thingId,'): ',thingStatus].join(''));
          }
        }

        if (!!messageText.length) {
          actions.NotificationAction.sendNotification('name@domain.com', 'Things not online: ' + String(messageText));
          logger.warn('Things not online: ' + String(messageText));
        }
    type: script.ScriptAction

I am a little lax on the rules but please do keep in mind the rules for marketplace postings. The discussion thread should be strictly about the top post. It’s not a place to post alternative implementations unless they are suggestions to merge with the original post.

The marketplace isn’t like other sections of the forum. These are not tutorials. These are published artifacts visible from and installable from MainUI directly. The threads should remain very on topic to avoid confusion and to keep them from becoming too long to manage.

Finally, as with all of my rule templates, this one has been rewritten in ECMAScript 2021 for OH 4. See Thing Status Reporting [4.0.0.0;4.1.0.0). The capabilities of the rule have also improved.

  • the rule now triggers on all Thing events instead of polling
  • it passes the thing ID, old status, old detail, new status, and new detail, and the actual Thing Object to the called rule instead of a collection of all the Things that are in the given state

I would like to try the 4.0 version of the template but I don’t know how to read the passed parameters from the called script. I didn’t find anything about it…

It would have made more sense to comment on the 4.0 version of the template’s post.

How you access the passed in parameters depends on the language being used in the called script. If JS Scripting you just reference it by name. If Blockly it’s in the context block. Nashorn it’s in context. I don’t know about Rules DSL or jRuby.

Sorry @rlkoshak I missed the other post. Thanks a lot for your suggestion.

Just wondering how to list the Thing status in a custom widget with oh-repeater?
What is the SourceType used for this?
I created a similar script, but I’m stuck how to list the results :frowning:

You’d have to get that information transferred to an Item. Widgets only work with Items, including oh-repeater.

The easiest thing to do would be to add a Switch Item for each Thing you care about. The rule called by this rule templarte would need to somehow maintain a mapping between the Thing and these status Items and command them ON/OFF based on the state of the Thing (Item metadata perhaps?). Then you can use Service Status Standalone Widget to display it.

This is how I do it and I add the status Switch Item to the Equipment also to make accessing the status for a device in rules easier.

I think oh-repeater can also work with JSON data so you could theoretically have just the one String Item that you post a JSON string to that the widget can parse and loop through.

1 Like

Many thanks @rlkoshak - appreciated.
This is the approach I use currently, by adding/removing dummy items for Things not Online dynamically in the script.
I thought an easier way exist in OH4 in the meantime :slight_smile:

Hello everybody,

i tried to install that, but:

2024-01-22 17:02:04.940 [ERROR] [b.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: TypeError: Cannot load CommonJS module: 'openhab_rules_tools'
	at com.oracle.truffle.polyglot.PolyglotMapAndFunction.apply(PolyglotMapAndFunction.java:46) ~[?:?]
	at org.openhab.automation.jsscripting.internal.OpenhabGraalJSScriptEngine.lambda$13(OpenhabGraalJSScriptEngine.java:266) ~[?:?]
	at java.util.Optional.orElseGet(Optional.java:364) ~[?:?]
	at org.openhab.automation.jsscripting.internal.OpenhabGraalJSScriptEngine.lambda$11(OpenhabGraalJSScriptEngine.java:266) ~[?:?]
	at <js>.:program(<eval>:3) ~[?:?]
	at org.graalvm.polyglot.Context.eval(Context.java:399) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426) ~[?:?]
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262) ~[java.scripting:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:52) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:77) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:52) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:77) ~[?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0(ScriptActionHandler.java:71) ~[?:?]
	at java.util.Optional.ifPresent(Optional.java:178) [?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute(ScriptActionHandler.java:68) [bundleFile:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1187) [bundleFile:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.runRule(RuleEngineImpl.java:996) [bundleFile:?]
	at org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData.run(TriggerHandlerCallbackImpl.java:87) [bundleFile:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) [?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) [?:?]
	at java.lang.Thread.run(Thread.java:840) [?:?]
2024-01-22 17:02:04.950 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'things_offline' failed: org.graalvm.polyglot.PolyglotException: TypeError: Cannot load CommonJS module: 'openhab_rules_tools'

Source Code looks as expected:

var {helpers} = require('openhab_rules_tools');

console.loggerName = 'org.openhab.automation.rules_tools.Thing Status';

// osgi.getService('org.apache.karaf.log.core.LogService').setLevel(console.loggerName, 'DEBUG');


helpers.validateLibraries('4.1.0', '2.0.1');


console.debug('ThingStatusInfoChangedEvent:' + event.toString());

var parsed = JSON.parse(event.payload);


var data = {};

data['thingID'] = event.topic.split('/')[2];

data['thing'] = things.getThing(data['thingID']);

data['oldStatus'] = parsed[1].status;

data['oldDetail'] = parsed[1].statusDetail;

data['newStatus'] = parsed[0].status;

data['newDetail'] = parsed[0].statusDetail;


rules.runRule("thingalarm-1", data, true);

Can that come from the called rule generating an error?

You are posting to the 3.x version of this rule template.

The code you posted code is the 4.x version of the rule template. See Thing Status Reporting [4.0.0.0;4.9.9.9].

If you are running OH 3, you installed the wrong version of this template. If you are seeing two versions of this rule template in the list under add-ons, make sure to choose the one with [3.2.0;3.4.9] in the title.

If you are running OH 4, please make sure to post to that version of the template’s thread. But also make sure to look at the list of dependencies.

  • OH 4.0.0 +
  • JS Scripting add-on installed
  • openhab-js 4.1.0 or later
  • openhab_rules_tools 2.0.1 or later

openhab_rules_tools can be installed through openhabian-config or through the following command run from the $OH_CONF/automation/js folder:

npm install openhab_rules_tools

For more info see openHAB Rules Tools Announcements

Sorry for that, will check again and, if necessary, come back in the right thread.
Many thanks for pointing me in the right direction.

I wish there were a better way to manage different versions of these. For now it’s confusing. Good luck and let me know if you run into problems!