An OH3 beginner who wants to write rules

can you change the filename to '/srv/openhab2-logs/HABApp.log' ?
Maybe the path has changed since I last implemented the default.

I don’t have an openhab2-logs directory in my srv …

Strange, I’ve a version of openhabian where this still exists.

The try just HABApp.log, then it’ll build the name relative to the configuration.
If I adjust the filename then it works fine, so I guess it’s really a permission issue.

Ok I changed it to:

filename: 'HABApp.log'

To be honest the whole openhab vs openhabian permissions is confusing … But I adjusted them to be the same:

drwxr-xr-x 2 openhabian openhab 4096 Apr 28 08:02 configs
-rw-rw-r-- 1 openhabian openhab 1610 Apr 28 09:05 config.yml
drwxr-xr-x 2 openhabian openhab 4096 Apr 28 08:03 lib
-rw-rw-r-- 1 openhabian openhab 1540 Apr 28 09:42 logging.yml
drwxr-xr-x 2 openhabian openhab 4096 Apr 28 07:52 params
drwxrwxr-x 2 openhabian openhab 4096 Apr 27 09:56 rules

Still seem to get the same error:

Apr 28 09:46:49 openhabian systemd[1]: Stopped HABApp.
Apr 28 09:46:54 openhabian systemd[1]: Started HABApp.
Apr 28 09:46:54 openhabian habapp[3493]: Error loading logging config: Unable to configure handler 'EventFile'

Can you check the logging folder in the config.yml.
Do you have permissions so that HABApp can create the logging file?

I’ll flash a new openhabian image tomorrow an try to reproduce the issue.

It’s definately a permissions issue (Highly likely because of me fucking around) … I changed the permissions to openhab:openhab and now the Event error is gone.

And now the HAPApp logging is being updated (good!) …

2021-04-28 10:06:22.111 [INFO ] [HABApp.openhab.connection           ] - OpenHAB version 3.0.2 (Release Build)
2021-04-28 10:06:22.224 [INFO ] [HABApp.openhab.items                ] - Updated 42 Items
2021-04-28 10:06:22.290 [INFO ] [HABApp.openhab.items                ] - Updated 37 Things
2021-04-28 10:06:27.120 [INFO ] [HABApp.Rules                        ] - Added rule "MyFirstRule" from helloworld.py

Add now I can see the “That was easy!” message in the syslog, so MyFirstRule ran …

I think I should go back to square one and see why the permissions got foobar’d … thanks for sticking with me Sebastian :slight_smile:

1 Like

I just confirmed that the logfile gets created properly on a fresh openHABian setup.
You must have accidentally changed permissions.

1 Like

Ok … I have got a basic understanding of the DSL Rules and got HABApp working (thanks to Sebastian) … I am going to focus on JSR Python following Rich’s post Journey to JSR223 Python 1 of 9

At this point, staying within OpenHab will probably allow me to learn more before I take a plunge into HABApp.

So far I have found that its hard to follow the documentation since there are so many platforms to support and evolving concepts … one needs to work carefully and have low expectations :slight_smile: for quick progress.

Just be aware that those were written in OH 2.5 so there will be some things in that code that will need to change in order to deal with breaking changes in OH 3. They are also based on use of the Helper Libraries so don’t skip that either. Drop a reply in one of those threads or post a new thread if you run into trouble.

1 Like

Whilst a good understanding of openHAB is always very helpful, creating rules does not rely on the very depth of openHAB.
What you can do is try and create a rule in jsr and then do a rule in HABApp and see what you like best.
HABApp will not get in your way - you can even run a mixture.
Under 3rd party you can post and ask for help creating rules or just ping me. :slight_smile:

PS: Use an IDE, e.g. PyCharm. It helps enormously.

1 Like

Thanks Rich … yes also found this post: Ivan’s Helper Libraries - OH3, Python, JavaScript which is very helpful.

I am making some progress … but I am getting this error:
2021-04-30 10:00:07.479 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/jsr223/python/personal/hello_world.py': ImportError: No module named joda in <script> at line number 6

Line 5/6 are:

from core.rules import rule
from core.triggers import when

I believe this is related to your comment about about:

any time you see “org.eclipse.smarthome” that has been renamed to “org.openhab”`

… obviously my installation is not correct, and of course you dont know what i’ve done, but curious if this rings any bells?

Your installation is likely correct, I just have not done any work on converting examples to work in OH3 yet. As an exercise, see if you can convert the timer scheduling line to work in OH3. You will need to have it use ZonedDateTime instead of Joda.

Thanks Michael - But I’m confused, The hello_world.py example is:

"""
This script uses a decorated cron rule that will generate logs every 10s and can be used to test your initial setup.
"""

from core.rules import rule
from core.triggers import when

@rule("Jython Hello World (cron decorators)", description="This is an example cron triggered rule using decorators", tags=["Test tag", "Hello World"])# description and tags are optional
@when("Time cron 0/10 * * * * ?")
def hello_world_cron_decorators(event):
    hello_world_cron_decorators.log.info("Hello World!")

I’m getting the error as soon as it loads this hello_world.py … so the Joda reference is somewhere in core.triggers … right?

I’m using VS Code … other than personal style is there some advantages I might be missing (FYI - I’m a total novice with VS Code)

@CrazyIvan359 … FYI - I updated to 3.1.0.M3 and get the same results … (I wanted to upgrade for other reasons) … Anyway, sth must be foobar with the python path as, in another case, it cant find the openhab module either:

2021-04-30 13:37:52.210 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/jsr223/python/personal/jython.py': ImportError: No module named openhab in /etc/openhab/automation/jsr223/python/personal/jython.py at line number 5

Yes, it would seem you installed the old Helper Libraries and not my updated version. In the post on this forum it links to the instructions in the repo which have a link to the old repo, the post has a link to my fork with the OH3 updates. Maybe go over the instructions again to make sure you have it installed correctly.

Well that’s embarrassing … I specifically thought I was installing your new Helper libraries :slight_smile: … but when I checked, they were the Dec 20 version Duh! … it works as expected now. (sorry about that!)

One thing I have come to understand in my OH/rule learning adventure is the importance of re-reading the documentation as you become more knowledgeable and have tried things out. At least for me, after I have played around a bit, the documentation makes a lot more sense and I get more insight.

I can now use make rules with both the DSL Rules and JSR223 Jython methods and have played around with some basic concepts … it’s all becoming clearer and I am really excited about all the things I will be able to do! One small advantage I see of the Jython approach vs HabAapp, is that the rules show up in the OpenHab Main UI as they are instantiated. Perhaps this is not that big a deal, but for now, it is helping me see how things are working together. I’m not giving up on HABApp at all, just making the observation.

I also upgraded to 3.1.0M3; which went exceedingly smoothly. (I’m sure there are major issues but for now it is working great) … one immediate benefit, a missing Z-Wave device binding showed up and worked flawlessly.

Of course there are frustrations, but so far, as I learn more, I am getting very encouraged!

I think I am going to start with a fresh install and get the setup back to this point before I move to the next step.

Indeed, the docs are something that all users should return to frequently, especially when having problems of figuring out how to do something. They are constantly being updated with new information and corrections and are essentially a living document. One read through isn’t enough.

I’m really glad you are getting up and running. As you start to create rules, be sure to look at the Tutorials and Solutions section of the forum, especially the Design Pattern posts. Those will give you generic ideas for how to implement a bunch of things that commonly come up. There are Python versions for all of them (look in the replies for those DPs not written by me) but they haven’t been updated for OH 3 yet so they may take a little bit of work to use. But the main point of the DPs are to show an approach, not necessarily provide a working solution.

1 Like

I think that goes for just about all the software documentation I’ve ever read