Scripts are not working

I have many scripts that are not getting called. I’ve picked one as an example. I have a rule as follows:

rule "Cinema Rack"
when
    Item GF_HomeCinema_Rack changed
then
    logInfo("SmartLife command exec", "Running rule Cinema Rack")
    if (GF_HomeCinema_Rack.state == ON) {
        cmdSmartLife_HomeCinema_Rack_Args.sendCommand("ON")
    } else {
        cmdSmartLife_HomeCinema_Rack_Args.sendCommand("OFF")
    }

    while (cmdSmartLife_HomeCinema_Rack.state != OFF) {
        Thread::sleep(500)
    }

    cmdSmartLife_HomeCinema_Rack.sendCommand(ON)

    logInfo("SmartLife command exec", "Result: " + cmdSmartLife_HomeCinema_Rack_Output.state.toString)
end

In my log, I see:

2019-05-09 21:56:51.615 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'test.rules'

But when I change GF_HomeCinema_Rack from ON to OFF or OFF to ON: All I see in the log is:

2019-05-09 21:57:03.360 [ome.event.ItemCommandEvent] - Item 'GF_HomeCinema_Rack' received command OFF

2019-05-09 21:57:03.376 [vent.ItemStateChangedEvent] - GF_HomeCinema_Rack changed from ON to OFF

2019-05-09 21:57:03.387 [GroupItemStateChangedEvent] - gPower changed from ON to OFF through GF_HomeCinema_Rack

I never see my “Running rule Cinema Rack” log entry and in fact, the other two exec bindings never execute either.

What is going on here?

Welcome to the OpenHAB community Ed!
Your rule looks good to me. I’m guessing these rules ran at some time in the past? No clue yet but be sure to let us know what platform you are running OpenHAB on and what version of OpenHAB you are using because it will help us figure out what is wrong

If you mean rules, try something simpler; take just one of your rules and put in a file testxxx.rules on its own, to rule out one error messing with all in one file. You can have multiple .rules files)

Be sure each of your rules have unique text names.

The logInfo should appear in openhab.log, not events.log

I’m running on Raspberry Pi and I’m running openHAB 2.4. This is a first time install.

Yes, I mean rules. I have the rule in a file by itself. Yes, I know the log info should appear in the openhab.log, but nothing appears there. I’m viewing it through the log viewer. I have many rules defined and none of them work reliable; some are in a files by themselves (as this one is) and others are have multiple rules in a file.

I rebooted my Raspberry Pi and tried the rule again. I saw the following in the log:

2019-05-11 00:02:31.584 [ome.event.ItemCommandEvent] - Item 'GF_HomeCinema_Rack' received command ON

2019-05-11 00:02:31.631 [GroupItemStateChangedEvent] - gPower changed from NULL to ON through GF_HomeCinema_Rack

2019-05-11 00:02:31.662 [vent.ItemStateChangedEvent] - GF_HomeCinema_Rack changed from NULL to ON

==> /var/log/openhab2/openhab.log <==

2019-05-11 00:02:33.177 [INFO ] [.model.script.SmartLife command exec] - Running rule Cinema Rack

2019-05-11 00:02:33.177 [INFO ] [se.smarthome.model.script.CinemaRack] - Running Script

2019-05-11 00:02:33.185 [INFO ] [se.smarthome.model.script.CinemaRack] - Turning ON

==> /var/log/openhab2/events.log <==

2019-05-11 00:02:33.188 [ome.event.ItemCommandEvent] - Item 'cmdSmartLife_HomeCinema_Rack_Args' received command ON

2019-05-11 00:02:33.194 [ome.event.ItemCommandEvent] - Item 'GF_HomeCinema_SeatRumblers' received command ON

2019-05-11 00:02:33.196 [ome.event.ItemCommandEvent] - Item 'GF_HomeCinema_Subwoofer' received command ON

2019-05-11 00:02:33.224 [nt.ItemStatePredictedEvent] - cmdSmartLife_HomeCinema_Rack_Args predicted to become NULL

2019-05-11 00:02:33.235 [vent.ItemStateChangedEvent] - GF_HomeCinema_SeatRumblers changed from NULL to ON

2019-05-11 00:02:33.244 [vent.ItemStateChangedEvent] - GF_HomeCinema_Subwoofer changed from NULL to ON

2019-05-11 00:02:36.106 [ome.event.ItemCommandEvent] - Item 'cmdSmartLife_HomeCinema_Subwoofer_Args' received command ON

2019-05-11 00:02:36.114 [ome.event.ItemCommandEvent] - Item 'cmdSmartLife_HomeCinema_SeatRumblers_Args' received command ON

2019-05-11 00:02:36.118 [nt.ItemStatePredictedEvent] - cmdSmartLife_HomeCinema_Subwoofer_Args predicted to become NULL

2019-05-11 00:02:36.120 [nt.ItemStatePredictedEvent] - cmdSmartLife_HomeCinema_SeatRumblers_Args predicted to become NULL

I then turned the switch off again and I only saw (i.e. the rule did not run):

2019-05-11 00:07:45.406 [ome.event.ItemCommandEvent] - Item 'GF_HomeCinema_Rack' received command OFF

2019-05-11 00:07:45.444 [vent.ItemStateChangedEvent] - GF_HomeCinema_Rack changed from ON to OFF

I edited the rule (just changing the title) and tried again:

2019-05-11 00:08:09.272 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'test.rules'

2019-05-11 00:08:09.602 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Cinema Rack': An error occurred during the script execution: null

2019-05-11 00:08:10.054 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Cinema Rack': Script interpreter couldn't be obtain

2019-05-11 00:08:10.079 [INFO ] [se.smarthome.model.script.CinemaRack] - Running Script

2019-05-11 00:08:10.085 [INFO ] [se.smarthome.model.script.CinemaRack] - Turning OFF

==> /var/log/openhab2/events.log <==

2019-05-11 00:08:10.105 [ome.event.ItemCommandEvent] - Item 'GF_HomeCinema_SeatRumblers' received command OFF

2019-05-11 00:08:10.116 [ome.event.ItemCommandEvent] - Item 'GF_HomeCinema_Subwoofer' received command OFF

2019-05-11 00:08:10.152 [vent.ItemStateChangedEvent] - GF_HomeCinema_SeatRumblers changed from ON to OFF

2019-05-11 00:08:10.156 [GroupItemStateChangedEvent] - gPower changed from ON to OFF through GF_HomeCinema_Subwoofer

2019-05-11 00:08:10.159 [vent.ItemStateChangedEvent] - GF_HomeCinema_Subwoofer changed from ON to OFF

2019-05-11 00:08:10.162 [ome.event.ItemCommandEvent] - Item 'cmdSmartLife_HomeCinema_SeatRumblers_Args' received command OFF

2019-05-11 00:08:10.173 [nt.ItemStatePredictedEvent] - cmdSmartLife_HomeCinema_SeatRumblers_Args predicted to become NULL

Unless I touch the file or reboot the server, the rule does nothing after being run once.

This potentially infinite loop is a really bad idea. Is this structure in other rules too? There are only so many rules that can run at the same instant, new rules cannot start if all the threads are taken up looping.

The rule has to wait for the call to return before it can continue. It is an exec binding thing that makes an HTTP call. I guess I can put some kind of loop counter and end after so many times.

How many threads are allow to run simultaneously? I hope it is really high as that could be too limiting.

By default, I believe it’s like 4 or 5.

Then completely rewrite the rule and probably split it into two rules where the second rule is a rule that is triggered when cmdSmartLife_HomeCinema_Rack received a command or changed.

EDIT:
By the way, is cmdSmartLife_HomeCinema_Rack_Args a Switch item or a String item? As you are first using cmdSmartLife_HomeCinema_Rack_Args.sendCommand("ON") and later use cmdSmartLife_HomeCinema_Rack_Args.sendCommand(ON).

Is there a way to increase this even just a little? And what happens if the 6th rule needs to run? Is it just “forgotten” or is it “scheduled” to run when a thread is available?

Duh, of course. Apparently having a senior moment! :blush:

Probably yes, but generally you shouldn’t need to really. And the 6th rule would just be queued, and only be executed if a thread is available again.

Increasing available threads does not really help; you’ll just end up with more threads looping, wasting tesource.

Writing event driven code can be a headache, but that is the way to go.

It is a String. I’m missing where I set it to ON. I set
cmdSmartLife_HomeCinema_Rack.sendCommand(ON) but I missing where cmdSmartLife_HomeCinema_Rack_Args.sendCommand(ON)

OK, so I changed it to

rule "Turn On Cinema Rack"
when
    Item GF_HomeCinema_Rack changed
then
    logInfo("SmartLife command exec", "Running rule Cinema Rack")
    if (GF_HomeCinema_Rack.state == ON) {
        cmdSmartLife_HomeCinema_Rack_Args.sendCommand("ON")
    } else {
        cmdSmartLife_HomeCinema_Rack_Args.sendCommand("OFF")
    }
end
rule "Turn On Cinema Rack Complete"
when
    cmdSmartLife_HomeCinema_Rack changed from ON to OFF
then
    cmdSmartLife_HomeCinema_Rack.sendCommand(ON)
    logInfo("SmartLife command exec", "Result: " + cmdSmartLife_HomeCinema_Rack_Output.state.toString)
end

and I get

2019-05-11 11:58:40.542 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'test.rules' has errors, therefore ignoring it: [14,5]: no viable alternative at input 'cmdSmartLife_HomeCinema_Rack'

The definition for cmdSmartLife_HomeCinema_Rack is

Switch cmdSmartLife_HomeCinema_Rack {channel="exec:command:smartlife:cinema:rack:run"}

What am I missing?

EDIT*
I caught it didn’t have “Item” in the when clause.

Chech the difference between the “when” clauses in your two rules.

It’s something else to learn, but using the optional VSCode as rules editor is good at syntax checks.

Ah yeah you are right, missed that difference, name is just too long to survive my attention span :slight_smile: