[SOLVED] My first rule

Platform information:

  • Hardware: Laptop 8GB ram
  • OS: Windows 7

Hi
I am trying to write my first rule to work.
I went to the rules folder and created new file called “MyFIrstRule.rules”.
Inside the rule I wrote:

rule “MyFirstRule”
when
Item zwave:device:fe6f2434:node12:sensor_door changed to OPEN
then
logDebug(“kitchen”, “Door is open”)
end

I set the following log:

log:set DEBUG org.eclipse.smarthome.model.script.kitchen

I Opened the door and look at the logs and did not see any printing lines.

Maybe I am using the Item name wrong?
Do I need to restart the HA to load the new rule ? (didnt help).

Thanks for the help

Ok
First things first, please use 0codes fences](How to use code fences) when posting code.
Second, have you read: https://www.openhab.org/docs/concepts/#things-channels-bindings-items-and-links

Is a channel not an item.
Have you limked a contact item to this channel?
If yes, what is it called?

Then do:

rule "My First Rule"
when
    Item MyContactItem changed to OPEN //Or whatever you called YOUR item
then
    logInfo("kitchen", "Door is open")
end

No need, just do a logInfo in the rules and it will appear in your openhab.log file

Make sure you don’t have Simple Mode turned on in PaperUI. I hate that mode as it hides all your Items. Assuming you have Simple Mode turned on, then the name of your Item will be zwave_device_fe6f2434_node12_sensor_door.

If you don’t have Simple Mode turned on, you need to create an Item and link it to that Channel.

Thank you for your detailed answer.
I learned a lot from it.
But still doesnt work :frowning:
I created an item and link it to the channel as in here:
image

I created a new rule as in here

rule “My First Rule”
when
Item MyDoorSensorItem changed to OPEN
then
logInfo(“kitchen”, “Door is open”)
end

I saved the file and restart the OH.
Still cant see anything in the openHab.log.
I can only see in the events.log the basic line that the door is open.

What can be wrong?
How can I know if the rule is even loaded?
What is the trigger that the rule is reload? saving the file?

The rule is in this folder:
image

Thanks in advance.

Something that might help new folk, I like to use logWarn; when you’re using log:tail in console it shows up as purple and does not get lost in the “noise” of events on a busy system.
When you’re happy, either comment it out or change it to Info

Please, don’t. There are four log Levels in question of DSL rules:

logDebug
logInfo
logWarn
logError

Each one is for messages associated with the name, Error -> there was a serious error which prevents the rule from being executed; might cause problems…
Warn -> There is something going on, which might be wrong, but maybe it’s ok - for example a device is offline, therefor a rule can’t succeed, but maybe the device is offline on purpose.
Info -> just to inform about what’s going on.
Debug -> like Info, but lot more Information.

The point is, you can set the log Level while running openHAB, it will take effect immediately.

If you’re interested in some special messages, just filter the output. You can do this in frontail, in bash (tail -f /var/log/openhab2/openhab.log | grep something) but not in karaf, but you can use the logger to cut it down to the interesting stuff (log:tail logger).

How did you create the rule1.rules file? Which Codepage used (should be UTF-8)? Which sort of line feed (should be only LF)? Are there some messages in openhab.log, such as

2019-10-15 23:25:22.692 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'rule1.rules'

and no errors around this message?

1 Like

Hi
I can see this error:

2019-10-17 11:33:04.165 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'rule1.rules' is either empty or cannot be parsed correctly!

I created the rule with notepad and saved it as UTF8.

Attached the rule as TXT file
rule1.txt (124 Bytes)

You are using the wrong kind of double quotes
You need these:""

I can see it’s UTF-8 with BOM, and it’s CRLF, not LF.
grafik

Nope, that’s discourse auto changing when code not marked as code :wink:

No, in post 4 his rule is marked as code but there are wonky quotes

So how can I edit rule files? Without visual studio

You don’t need Visual Studio but Visual Studio Code (that’s another software) Get it from here:


It’s free. There’s a plugin for openHAB (can be installed directly from Visual Studio Code builtin market place).
afaik you can change the code page by save as dialogue (at least from Notepad)
grafik
I’m not sure about CRLF, though.

So how do you avoid CR in your rules?

I’m not sure if this is necessary in windows, maybe it only has to be consistent :wink: so try it out.
As I’m using VSCode, it’s one click to change the behavior :wink:

Another tool worth mentioning in Notepad++, which also provides a switch to toggle between CRLF and LF only.

Nothing is going easy form me :frowning:
I installed notepad++
Changed it to LF, Utf-8 without BOM.
image

Still same error :frowning:

But now the file is named rule3.rules, not rule1.rules. Is the error about rule3.rules?

1 Like

I use Notepad++ as well. I have never seen this LF in my files.

Perhaps create a whole new file starting all over could be the fix.

It works!!!
I had to restart the OH and problem solved. Thank you very much for your help!