Links to beginners documents on Rules for OpenHab2

I keep finding documentation for rules for openhab, these do not work for openhab2 as I am getting errors like “the use of wildcards is depreciated” and “validation issues found”.

for example …

import org.openhab,core.library.types.* throws an error.

I simply want when “FRONT_DOOR_STATE” changes to “OPEN” to write in the log that it opened. I can not get it to fire no matter how I try to trigger it.

I tried even adding

when
item FRONT_DOOR_STATE received update
then

and it still will not trigger even if I try to write to the log right after the then. I assume my thing named “FRONT_DOOR_STATE” is what I use there?

Is there a “openhab rules for really big dummies that need to be spoon fed” document anywhere? as I just can not wrap my head around where I call things from what angle and what expected response I should be looking for?

Thanks so much for your help

You should use the Item name there. Careful about syntax (use Item with capital I). You can try ESHD to verify syntax.
It should be:

rule	"My_Rule"
when
	Item <Item_Name> received update
then
	logInfo("My_Rule_Log", "Item got some kind of update...")
end

Have you created the Items using PaperUI (simple linking mode) or using flat config files (*.items) ?

An intro to rules can be found here: http://docs.openhab.org/configuration/rules-dsl.html

In most of the cases, you don’t need to import stuff in OH2 rules.
(note: you have a comma after org.openhab)

Thanks for that help, I found that document and it is really lacking in any details. I’m looking for something very detailed.

For example… I have the following rule

rule “FrontDoor”

when
item FRONT_DOOR_STATE received update
then
logDebug("door,“door state changed”)
end

It follows all conventions that the documents say are perfect for a rule. but it refuses to run. I get in the log file…

Configuration door.rules has errors : no viable alternative at input ‘item’

FRONT_DOOR_STATE is the name of my item as defined in paper under items. it shows up on the control panel and switches from OPEN to CLOSED as I move the door. so it is working. but the rule refuses to even run.

what does it mean my an alternative input? none of the documents for rules talk about adding alternative inputs to items.

A good german guide you can find here:
https://klenzel.de/3125

1 Like

Did you try with Item ? (with capital I) I am not sure if this is the problem but it’s worth a try (I don’t have access atm to my OH2 to check it out)

Tried that and the error goes away, but still nothing happening. nothing is being written to openhab.log
Although the documentation does not say a capitol I is needed, Maybe that is required in OpenHab2?

try logInfo to see if it works (instead of logDebug)

Do you have any kind of entries in your openhab.log?

Perfect, Thank you! google translate is making it very readable for me.

1 Like

you are missing a closing quote after "door
use ESHD :slight_smile:

It shows that it reloaded the rules file but nothing from the logdebug or logInfo. I just tried to change it to logInfo and it also does not work.

checked the quote and it’s there in the rules file. the transfer to the internet must have lost it.

I tried ESHD and it throws warnings on all my item names because I am running openHab2 as there is nothing in the items folder.

try to recreate the *.rule file from scratch
create a new file, type up the rule, delete the old file
maybe there is some kind of strange character or new line feed in the existing file that is stopping the rule from being processed (not sure… worth a try)

by the way: Are you sure that this is the Item name (and not the label?). You need to use the name. Can you post a screenshot of PaperUI showing the Item?
Usually OH2 gives other kind of Item names when they are auto-created (was this one auto-created or you defined it manually in PaperUI?)
If you defined it manually: Did you link it to a channel of a Thing?

1 Like

Wait the name and label are different? Nothing I have read over the past 2 days mentinoed that.

FRONT_DOOR_STATE
DoorWindowsSensor_BinarySensor
Contact

That means I have to use that obscure "DoorWindowSensor_BinarySensor? when I have 10 of them that will get very confusing as to what sensor is on what door.

I know :slight_smile:
That’s why I use *.items files
You are right… the docs entry is not very clear regarding the Item name but this is what you need to use for the trigger condition. This is what gets status updates from OH2.

You can see the state of your items using either the console or the REST API (you need to have the REST Docs installed under Addons->Misc)

with console ssh openhab@localhost -p 8101 (with password: habopen)

items list |grep -i Sensor

with REST API:

http://<OH2_IP>:8080/rest/items?recursive=false

Ok, so I am in the same boat. Why can’t the docs be clear on this?

Anyways, I am trying to get the syntax of an Item correctly.

The Item is either: omnilink:zone:4515ad84:3:contact OR omnilink_zone_4515ad84_3_contact . The item iself in PaperUI is: omnilink:zone:4515ad84:3 and under the channel for contact is: omnilink:zone:4515ad84:3:contact.

Neither work. The common label is: Zones_Den_Door_Current. Doesn’t work either.

Tried quotes around the 'omni…". Didn’t work either.

Why does this have to be #1 so difficult and #2 so poorly documented?

Here’s an example of the log of contact 1. Contact 3 works fine live in PaperUI, etc; state changes immediately when door open and closed. So its not the binding. Every error log has issues with the syntax of the WHEN argument. Specifically, it can’t figure out that the ‘item’ actually is because the syntax is wrong. Do I need quotes? Do I need blahblah_1_contact.state? Help!

2018-02-25 20:56:25.278 [ItemStateChangedEvent     ] - omnilink_zone_4515ad84_1_current_condition changed from 0 to 1
2018-02-25 20:56:27.578 [ItemStateChangedEvent     ] - omnilink_zone_4515ad84_1_contact changed from OPEN to CLOSED
2018-02-25 20:56:27.579 [ItemStateChangedEvent     ] - omnilink_zone_4515ad84_1_current_condition changed from 1 to 0
2018-02-25 21:02:14.590 [ItemStateChangedEvent     ] - omnilink_zone_4515ad84_1_contact changed from CLOSED to OPEN
2018-02-25 21:02:14.592 [ItemStateChangedEvent     ] - omnilink_zone_4515ad84_1_current_condition changed from 0 to 1
2018-02-25 21:02:25.590 [ItemStateChangedEvent     ] - omnilink_zone_4515ad84_1_contact changed from OPEN to CLOSED
2018-02-25 21:02:25.590 [ItemStateChangedEvent     ] - omnilink_zone_4515ad84_1_current_condition changed from 1 to 0
2018-02-25 21:03:10.291 [ItemStateChangedEvent     ] - omnilink_zone_4515ad84_1_contact changed from CLOSED to OPEN
2018-02-25 21:03:10.293 [ItemStateChangedEvent     ] - omnilink_zone_4515ad84_1_current_condition changed from 0 to 1
2018-02-25 21:03:21.891 [ItemStateChangedEvent     ] - omnilink_zone_4515ad84_1_contact changed from OPEN to CLOSED
2018-02-25 21:03:21.892 [ItemStateChangedEvent     ] - omnilink_zone_4515ad84_1_current_condition changed from 1 to 0
2018-02-25 21:03:23.291 [ItemStateChangedEvent     ] - omnilink_zone_4515ad84_1_contact changed from CLOSED to OPEN
2018-02-25 21:03:23.293 [ItemStateChangedEvent     ] - omnilink_zone_4515ad84_1_current_condition changed from 0 to 1
2018-02-25 21:03:25.592 [ItemStateChangedEvent     ] - omnilink_zone_4515ad84_1_contact changed from OPEN to CLOSED

import java.net.URLEncoder
import java.net.URI

rule "Den Door Contact Open"
when
    Item omnilink_zone_4515ad84_3_current_condition changed from 0 TO 1
then
    {
    logDebug("DenDoorContact","CLOSED TO OPEN")
    sendHttpGetRequest("http://172.16.1.136:3480/data_request?id=lu_action&output_format=json&DeviceNum=165&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1")
    }
end

rule "Den Door Contact Closed"
when
    Item omnilink_zone_4515ad84_3_current_condition changed from 1 TO 0
then
    {
    logDebug("DenDoorContact","OPEN TO CLOSED")
    sendHttpGetRequest("http://172.16.1.136:3480/data_request?id=lu_action&output_format=json&DeviceNum=165&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0")
    }

end

Ignore the syntax for the item above; it doesn’t work. If tried it with and without the quotes, and also as omnilink_zone_4515ad84_3_contact and OPEN TO CLOSE

Any help would be greatly appreciated. I’ve got 17 of these to do. Thanks!

The docs aren’t that bad :rofl:

Take a look at
https://docs.openhab.org/configuration/rules-dsl.html#event-based-triggers
and note this trigger:
Item <item> changed [from <state>] [to <state>]
Instead you are using capital letter for to
I never tried capital letters but I guess that is your problem.

Your are looking at the channels, not the items.

Here you go, THERE is your items name.

Try out the VS Code editor with the openhab extension.

This will help you to find errors.

Try this rule.

rule "Den Door Contact Open"
when
    Item omnilink_zone_4515ad84_3_current_condition changed 
then
    logInfo("My Rule Test", "Item:\"" + triggeringItem.name + "\" state:\"" +  triggeringItem.state + "\" previousState:\"" +  previousState + "\"" )
end
2018-02-26 22:34:34.043 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'security.rules'
2018-02-26 22:34:44.486 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Den Door Contact Open': An error occurred during the script execution: The name 'triggeringItem' cannot be resolved to an item $
2018-02-26 22:34:46.754 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Den Door Contact Open': An error occurred during the script execution: The name 'triggeringItem' cannot be resolved to an item $
2018-02-26 22:34:48.039 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Den Door Contact Open': An error occurred during the script execution: The name 'triggeringItem' cannot be resolved to an item $
2018-02-26 22:34:50.446 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Den Door Contact Open': An error occurred during the script execution: The name 'triggeringItem' cannot be resolved to an item $

Didn’t work/ Copied exactly as stated.

This doesn’t work either, but don’t get an error…

rule "Den Door Contact Open"
when
    Item omnilink_zone_4515ad84_3_current_condition changed


then
    if (omnilink_zone_4515ad84_3_current_condition.state==OPEN) {

    debugInfo ("Condition","open")

    sendHttpGetRequest("http://172.16.1.136:3480/data_request?id=lu_action&output_format=json&DeviceNum=165&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1")

    debugInfo ("Condition","open sent commandd")


    } else if (omnilink_zone_4515ad84_3_current_condition.state==CLOSED) {

    sendHttpGetRequest("http://172.16.1.136:3480/data_request?id=lu_action&output_format=json&DeviceNum=165&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0")

    }
end

Any help? (if then examples taken from multiple openhab2 articles with examples on this site)…

Thanks…