Rules not triggering when item receives command

I’m new to OH but I’m very excited by the prospects. The first major hurdle I’m having is getting my triggers to work. I have a WOL script that I’d like to trigger with a switch, I’m using the UI editor. The script works correctly when executed in the script editor, but I cannot get the trigger to run the script either from a page or a siteMap via iOS.

triggers:
  - id: "1"
    configuration:
      itemName: wol_entertainment
      state: ON
      previousState: OFF
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: |-
        val actions = getActions("network", "network:pingdevice:cea6b998bb")
        if (actions === null) {
            logInfo("actions", "Actions not found, check thing ID")
            return
        } else {
            // Send via MAC address
            //actions.sendWakeOnLanPacketViaMac()
          logInfo("triggered", "WOL action triggered")
            actions.sendWakeOnLanPacket() // deprecated
            
            // Send via IP address
            //actions.sendWakeOnLanPacketViaIp()
        }
    type: script.ScriptAction

The event generated by the switch item is shown in the log:

[TRACE] [openhab.event.ItemCommandEvent       ] - Received event of type 'ItemCommandEvent' under the topic 'openhab/items/wol_entertainment/command' with payload: '{"type":"OnOff","value":"ON"}'
  • Platform information:
    • Hardware: BSD Jail
    • OS: freeBSD 12.3
    • Java Runtime Environment: openjdk 11.0.12 2021-07-20
    • openHAB version: 3.1.0

Your screenshot and your code do not match.
In your script you have to use the command trigger as shown in the screenshot.

type: core.ItemCommandTrigger

In your GUI simply send ON command to wol_entertainment

No, that’s the event sent from your UI.
The Item command event should be found in your events.log, to make sure the chain is complete.

That’s a state rule trigger, not a command trigger. These are not the same thing.
You should look into why your code and screenshot are different, some confusion?

@rossko57 , @Oliver2 – thanks for the reply, I guess I had been messing about with the ItemStateChangeTrigger and hadn’t saved. I’ve not set it back to the ItemCommandTrigger to no avail. Are there API references somewhere that I’m missing? I’m a software dev so I’d much rather read through a ref than try to figure this out from the tutorials/UI.

triggers:
  - id: "1"
    configuration:
      command: ON
      itemName: wol_entertainment
    type: core.ItemCommandTrigger

@rossko57 , the log lines in events.log look similar to those in the console:

 [TRACE] [openhab.event.ItemCommandEvent      ] - Received event of type 'ItemCommandEvent' under the topic 'openhab/items/wol_entertainment/command' with payload: '{"type":"OnOff","value":"ON"}'
[TRACE] [openhab.event.ItemStateChangedEvent ] - Received event of type 'ItemStateChangedEvent' under the topic 'openhab/items/wol_entertainment/statechanged' with payload: '{"type":"OnOff","value":"ON","oldType":"OnOff","oldValue":"OFF"}'

make sure that your trigger section looks like this:

    configuration:
      itemName: wol_entertainment
      command: ON
    type: core.ItemCommandTrigger

better do it again with the trigger builder of MainUI.

if (actions === null) {

should be “NULL”

If it still does not work, add in every other line logInfo("Test","...") to see where and why the problem occurs

It Does:

triggers:
  - id: "1"
    configuration:
      itemName: wol_entertainment
      command: ON
    type: core.ItemCommandTrigger
if (actions === null) {

Okay, well changed to NULL and added those lines, but again - the script is not running. I can run it from the UI using the “play” button and see my logInfo lines and WOL packet sent. The problem here is that the trigger is not executing.

Just so we’re clear, here is exactly what I did: Created an Item of type Switch named wol_entertainment and added it to my “LivingRoom”. I then added widgets in a page and added the item to my sitemap.

Then, I used the UI to build the rule. This should work, correct? Am I missing something?

triggers:
  - id: "1"
    configuration:
      itemName: wol_entertainment
      command: ON
    type: core.ItemCommandTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: |-
        logInfo("getting Actions", "...")
        val actions = getActions("network", "network:pingdevice:cea6b998bb")
        logInfo("Got Actions", "...")
        if (actions === NULL) {
            logInfo("actions", "Actions not found, check thing ID")
            return
        } else {
            logInfo("triggered", "WOL action triggered")
            //actions.sendWakeOnLanPacket() // deprecated

        }
    type: script.ScriptAction

Those look weird, but then nobody runs with TRACE on events. :wink:
But it looks like commands do happen.

Make a new rule, simple as possible. Trigger from ANY command to your Item, have the script just be a logInfo announcement.
I’m speculating this rule has a corrupt trigger somewhere.

FWIW, I had to elevate the log level to TRACE in order to see ANY events in the logs.

Created a new Switch point, and a new Rule posted below, this one exhibit the same behavior. Events are logged as above, but the logInfo is not.

triggers:
  - id: "1"
    configuration:
      itemName: test_switch
      command: ON
    type: core.ItemCommandTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: logInfo("triggered", "Test action triggered")
    type: script.ScriptAction

LOG:

[TRACE] [openhab.event.ItemCommandEvent      ] - Received event of type 'ItemCommandEvent' under the topic 'openhab/items/test_switch/command' with payload: '{"type":"OnOff","value":"ON"}'
[TRACE] [openhab.event.ItemStateChangedEvent ] - Received event of type 'ItemStateChangedEvent' under the topic 'openhab/items/test_switch/statechanged' with payload: '{"type":"OnOff","value":"ON","oldType":"OnOff","oldValue":"OFF"}'

Ah, some progresss. I set the log level of org.openhab and openhab.event to DEBUG and received:

[DEBUG] [re.automation.internal.RuleEngineImpl] - The trigger '1' of rule 'test_rule' is triggered.
[DEBUG] [re.automation.internal.RuleEngineImpl] - Rule engine not yet started - not executing rule 'test_rule',

I’ll look into restarting/enabling the rule engine.

could you replace your code just with a logInfo?

Not sure what you mean there, the only line in my script is logInfo("triggered", "Test action triggered")

sorry - my fault. Didn’t realize you already did :+1:

There’s a problem worth fixing before attempting diagnosis of other things. There are a number of posts about similar missing logs, so far as I can make out happens sometimes after OH2->OH3 upgrade, to do with logfile locations perhaps.

See also

Interesting, I did install v2 before realizing my mistake and upgrading. Perhaps it is worth a clean install in a new jail.

Yes, I saw that, but that issue went unresolved. I may try to run this in an Ubuntu VM or something but I’d really like to get it working in a jail.

FWIW, this was solved by installing misc/openhab (v3.2.0) via bsd ports.
I assume the same could be assumed for pkg installs using the misc/openhab package.