[SOLVED] Rules vs Scripts?

Hello friends,
I’ve been using OpenHAB 2.5.x (now updated to 2.5.8) for about 10 months on a Raspberry Pi 4 to control my zwave smart home system.
I created a few rules to automatically control my light, window blinds, fans and hot water boiler.
Even though creating rules was easy for a non-developer like me that doesn’t code, I am still seeing erratic behavior with my rules operations.
Sometimes I press a switch that triggers a rule and nothing happens. Sometimes the rule operation is delayed by a few seconds to a few minutes. And sometimes pressing the switch 2-3 times eventually runs the rules.
This is not a workable solution, since some electric appliances can break from over use (like the water boiler) and the electric bill can sky rocket due to this faulty behavior.
My question is, is there a solution for this erratic Rules behavior?
Will using Scripts instead of a Rules solve this issue?
Is there an easy tutorial for non developers that explains how to create Scripts?
Is there a way to easily convert Rules into Scripts?

Here is an example Rule I created:

rule "Good Night"
when
    Item BedLeft_Switch3 changed from OFF to ON
then
    sendCommand(Entrance_Switch2, OFF)
    sendCommand(Entrance_Switch4, OFF)
    sendCommand(Bathroom_Switch2, OFF)
    sendCommand(Bathroom_Switch3, OFF)
    sendCommand(Bathroom_Switch4, OFF)
    sendCommand(Kitchen_Switch1, OFF)
    sendCommand(Kitchen_Switch2, OFF)
    sendCommand(SalonTV_Switch1, OFF)
    sendCommand(SalonTV_Switch2, OFF)
    sendCommand(SalonTV_Switch4, OFF)
    sendCommand(SalonDesk_Switch1, OFF)
    sendCommand(Balcony_Switch1, OFF)
    sendCommand(Balcony_Switch2, OFF)
    sendCommand(Bedroom_Switch2, OFF)
    sendCommand(Bedroom_Switch4, OFF)

    sendCommand(BedRight_Dimmer3, 100)
    sendCommand(SalonWindow_Dimmer3, 100)
    sendCommand(SalonDesk_Dimmer3, 100)
    sendCommand(SalonBalcony_Dimmer1, 100)
    sendCommand(SalonBalcony_Dimmer2, 100)
    sendCommand(Balcony_Dimmer3, 0)
end 

If possible, I would love to convert it to a script, if it runs in a more consistent way.
Hope you can help :pray:

Write sensible rules.

Looking at the sample presented, it sends a couple of dozen commands to Items in the space of a few milliseconds.
We’ve not been told, but let’s guess these Items are linked to real devices. All the same technology (e.g. zwave), or spread over different types?

Anyway, the most likely outcome here is a traffic jam of getting dozens of commands and responses over a network at the same time. Some technologies are better at handling that than others, maybe it will work eventually, maybe some messages get discarded or trampled on.

Doing the same thing from a different script or rules system will have the same outcome.

1 Like

Ross has a good point, but you tagged this topic with Z-Wave, so I assume this is the technology you are using. I have rules that update dozens of Items and the Z-Wave binding has handled it perfectly. However, you may have some issues in your network that could cause slowness. I suspect this is the cause of your issue.

  • How many devices do you have in your network? Have you looked into the debug log for the binding? Are devices slow to respond when manipulated through the UI?
  • Do you have any ghost nodes (nodes in OH without physical devices assigned to it)?
  • What else is running on your system other than OH? This can significantly slow things down.
  • How often are you using the rules? If not used for a while, they could be getting reloaded, which can cause some slowdown when executing. This would not cause massive slowness though, unless your rules are using a lot of primitives.

That said, the rules DSL is abysmal, and I highly recommend scripted automation. Take a look here for instruction…

… and here for a simplified installation for Jython…

If you wait a few days, the libraries will be updated and the installs will be in Eclipse Marketplace, so 2.5.x users will be able to install them through the UI. There will also be add-ons for Groovy, jRuby, and Kotlin.

1 Like

…but triggers them several times and they might run in parallel threads if they are not finished before the next trigger comes. Be aware of that caveat if you do not take precautions to avoid a situation like that.

2 Likes

I have a 40-50 node Z-Wave set up and use Rules to automate / trigger items. I have a go to sleep rule that turns off 20ish Z-Wave items (one right after the other) every night with no significant delays.

I think Scott has a good list of items/questions to start debugging. One thing I have run into is the ghost nodes in OH / Z-Wave Network will cause the issues you describe. I had this a year or two ago where OH was trying to route through nodes that didn’t exists anymore. I rebuilt my Z-Wave network and am very disciplined to always exclude nodes if I unplug them. Since then the reliability issues went away.

1 Like

Thanks for you reply @rossko57
Most of the time the rule works ok by pressing a physical switch or when using a Siri command.
It’s only sometimes that it hangs or doesn’t work.
These items are all linked to real devices. I have 14 ZWave nodes in total, with 3-4 switches on each node.

Can you please suggest a better rule format that will solve the traffic jam problem?
Many thanks for your help :pray:

Good point. I usually wait a minute or two before I repress the switch that runs the rule.

Thank you @5iver for the detailed answer :slight_smile:

  1. I have 14 zwave nodes on my system. And each node has 3-4 switches.
    I have not checked the debug log. I don’t really know how to do that.

  2. In OpenHAB Inbox, I can see 4 old zwave devices I deleted in the past and are no longer in my house. Are those Ghost nodes? when I removed them from my house I deleted them from the Things list, but I did not run the exclusion command. Is there a way to exclude them now?

  3. my OpenHAB system is installed on Raspberry pi 4 with 4gb of memory running Raspberry Pi OS with the desktop UI. Other than OpenHAB and the web browser, I don’t really use any other apps.

  4. I use rules a few times a day, depending on the rule. For example, I usually run the Good Morning and Good Night rules once a day each. But I have another rule that turns ON a bathroom fan every time I turn on the light in that room. There is only one command in the bathroom fan rule, so it most likely isn’t causing a command traffic jam. Nevertheless, even though the bathroom fan rule only has one command, sometimes there is a 20-30 seconds delay before the fan actually turns ON.

Can you please share an example script so I know how to change my rules to?

Is there post I can subscribe to, to know when the libraries are updated, so I can install them using the Eclipse Marketplace? Installing from openhab ui will make my life much easier, since I’m not a developer.

1 Like

Thanks @Nick_Wilkinson
My OpenHAB system is smaller than yours with about 14 zwave nodes.
In OpenHAB Inbox, I can see 4 old zwave devices I deleted in the past and are no longer in my house. Are those Ghost nodes? when I removed them from my house I deleted them from OpenHAB’s Things list, but I did not run the exclusion command. Is there a way to exclude them now?

The documentation covers this…

Yep! This is very likely the source of your issues.

In Habmin, add the device from the Inbox, then Configuration> Things> select ghost node> Tools (top right corner)> Show advanced settings> Set device as FAILED. Then Remove device from controller. Then delete the Thing. Repeat for all the ghost nodes. Then start discovery for the Z-Wave binding. If they show up again, it did not work, which is what most people report. From there, search the forum for the PC Controller software from SiLabs, which is a very handy tool.

Investigate further after the ghosts are busted. Chances are it will be zippy again.

Share a DSL rule and I will convert it to Jython for you.

The Jython and helper library add-ons are actually already in the marketplace (:shushing_face:)… along with Groovy, jRuby and Kotlin (not working yet). :shushing_face:! I have some polishing up to do, but hope to make an announcement this weekend. Until then, :shushing_face:! The Jython libraries need to be installed before the Jython add-on, or they will not be available in your scripts. Also, the library add-on has older libraries that need to be updated.

I will be posting a new topic about this, but I will ping the Jython beta add-on topic too.

3 Likes

If you still have the device you can plug them in/hook them up and exclude them. For the Aeotec Z-stick you are able to un-plug it from your computer hold the button for a few seconds until it starts to blink and walk around to each Z-Wave Device to trigger the device exclusion.

If you don’t have the devices anymore Scott’s suggestion might work. This hasn’t worked for me in the past.

I’ve downloaded PC control and haven’t had the time to understand it yet but believe there is a way to remove them.

A last resort would be to rebuild your Z-Wave Network. Last time I did this I moved to the the recommended approach of using the .item files https://www.openhab.org/docs/configuration/. This allows for all the items to be recreated quickly by just updating the Node ID in the file.

2 Likes

Ok, just my two cents on that…
I would suggest a fresh installation with the “openhabian-package”


on a recent version. I had some similar issues in the past that I was trying to fix but at the end that fresh installation solved everything and my system is doing a great job ever since!

Good luck!

1 Like

Unfortunately, I don’t have the ghost devices anymore. They didn’t work properly, so I needed to return them to the store and get replacements devices.

Thanks for the detailed explanation :pray:
Unfortunately, I was not successful in removing the Ghost Nodes using the FAIL and Remove device from controller commands.
I’ll try the PC controller software solution.

1 Like

After uninstalling two unnecessary bindings (Sonos binding and Samsung TV binding) I’m now in a big pickle, because all my rules stopped working and Homekit Integration also stopped working :scream:
I tried rebooting my Raspberry Pi, but it didn’t help.
I tried to reinstall the Rule Engine (Experimental) add-on, but that didn’t help.
And I also tried to create completely new rules files, and that didn’t help either.
I don’t know what could have caused the these failures.
I could really use a suggestion how to fix this issue :cry:

It seems my super basic computer developer skills are not sufficient to write proper Jython scripts.
Is it possible to create items Groups to control a few switches by pressing a single switch?
Or is this type of functionality only possible using rules and scripts?

Yes, absolutely!

In short:

  1. Define your Group Item
  2. Add each individual Item to your group using (yourGroupItemName) within the Item configuration.
  3. Add the Group Item to your Sitemap. If it’s a switch, it will switch all the members of that group in one go!
2 Likes

That sounds like a promising rules replacement.
Can Groups be defined in a way so that when the trigger switch is set to ON, all rollershutters in the groups will be opened by X%, and when the trigger switch is set to OFF all rollershutters will be closed?

Is X% the same X every time, or does this X change?

I think this is kind of possible using mappings, but it definitely depends on how you’ve setup your member Items.

1 Like

It will be the same x% everytime.
For example, when I set the trigger switch to ON, I would like all the living room rollershutters to open by 30%, and the bedroom rollershutter to open by 10%.
And when I set the trigger switch to OFF, I would like all the rollershutters in the apartment to close completely.