Example of a simple switch in python oh 3.1

Hello, I’m really trying to understand how to pass some “rule” in python, unfortunately I can’t understand how to start, I have lots of error messages, yet I place my libraries import them but without success, obviously I’m doing everything wrong, I really need help, a step-by-step guide for example to activate or deactivate a switch? help me understand the logic of starting, I tried though … could someone guide me through the setup and creation of a simple rule under Oh3.1 in python? I am stuck…I am depressed :wink:
thank you in advance

The easiest way may be to use HABApp with Python 3. @Spaceman_Spiff has done a good job with that.

1 Like

thanks for your help !

I’ve spent a lot of time getting Python up and running in openHAB 3. Unfortunately, the libraries have to be installed manually. I also noticed that there are few specific Python manuals. You can learn Python itself, but openHAB-specific scripting is a bit difficult in my opinion, as not everything has been documented and you have to reckon with the fact that you won’t always get an answer to your questions and then get stuck, which is of course a bit demotivating . But if you want, i can tell you the installation steps and then have a look at it for yourself. However, i run openHAB in a Docker container, where the installation is a little different.

For those who might not be aware, as I was when I started down this road, there’s kind of a separate site with documentation on the extra languages that can be used in openHAB. I use the “But how do I…?” section all the time

https://openhab-scripters.github.io/openhab-helper-libraries/Guides/But%20How%20Do%20I.html

1 Like

thanks for your attention, there are definitely steps that I don’t understand and yes if you can help me to the setup steps on OH3.1 it would be great!

Here are the steps I followed, I was able to run the “hello world” script without any problem, this is only the first step I know! but we have to start at the beginning … :slight_smile:

here are my steps on OH3.1:

1: from “Setting up VSCode — openHAB Helper Libraries documentation
I created a file for vscode at the root of “oh_conf” named “.env” with in it “PYTHONPATH =” ./ automation / lib / python "that says my vscode does not seem to react?.

2: I downloaded the library for oh3 here: “GitHub - jimtng/openhab-helper-libraries at oh3-patch”.

3: Via Karaf I activated the debug for 2 logs: “log: set DEBUG org.openhab.core.automation” and “log: set DEBUG jsr223”.
4. I copied the contents of “core” folder to automation / jsr223 and automation / lib.

5.Restart server “sudo reboot” via “ssh”.

6.I copied the “hello world” example script: Copy the / Script Examples / Python / hello_world.py script to / automation / jsr223 / python / personal /.

7.Then in the frontail I can see the script running: [INFO] [Jython Hello World (cron decorators)] - Hello World! every 10 sec.

So a know is a very little step, know i try and find how to simply log when a switch light change. hoping that step by step my knowledge will evolve
thank you all

1 Like

here another example to receive in the frontail a log when the value of my item “prodsolaire” changes, in this log I also display its value

from core.rules import rule
from core.triggers import when
#dsl:   rule "log when prodsolaire updated"
@rule("log when prodsolaire updated", description="show log when prodsolaire is updated", tags=["Tag 1", "Tag 2"])

#dsl: when   Item ProdSolaire received update
@when("Item ProdSolaire received update")

#dsl: then logInfo("when prodsolaire updated","ProdSolaire log=>" + ProdSolaire.name + "updated! to:" + ProdSolaire.state + "watts")
def my_rule_function(event):    
    my_rule_function.log.info("ProdSolaire log=> [{}]updated! to: [{}] watts".format(event.itemName, event.itemState))

result in fronttail is :

2021-07-14 09:35:28.864 [INFO ] [.jython.log when prodsolaire updated] - ProdSolaire log=> [ProdSolaire]updated! to: [885] watts

but in visualcode i dont see any use of the “.env” any idea ?

thanks

@isoparme your steps 1 and 2 can be replaced with my updated Helper Libraries. The original libraries have a page that details how to setup VSCode in a little more detail than my now outdated post that you linked. You will also find a link to my Python stubs which should ease development of Jython rules.

You can find my updated libraries in this post:
https://community.openhab.org/t/ivan-s-helper-libraries-oh3-python-javascript/

1 Like

Thank you a thousand times for your support !

1 Like