Running Python scripts importing xknx

I have a KNX installation (well, currently still just a testboard) and use HA for visualization and logic. For more advanced logic stuff I want to write Python scripts using xknx running in the Background and are either event or time-driven - in the style of the xknx examples: https://github.com/XKNX/xknx/tree/main/examples. The question is how to do that.
So far I figured out that I can’t use “Python Scripts” for that, because it doesn’t allow imports, so I can’t import xknx. Instead I should use “pyscript” or “AppDeamon”. “AppDeamon” seems to be overkill to me and I can’t find a convenient way to install “pyscript”. Convenient means to me that I can install it once and can trigger all updates via the HA frontend. But it seems to me that by installing it via HACS I would need to update HACS manually and by installing it manually without HACS I would need to update “pyscript” manually. Is that correct?

Hi :wave:!

You can update HACS via frontend.

I’d recommend to use HA entities and their services, Knx interface device triggers or events and knx.send service, instead of using xknx directly. You may even use Nodered for advanced logics.
Or use Appdeamon, pyscript or what have you with HA primitives, instead of interfacing with xknx directly.

Using xknx would mean you’d have to coerce to the version used in HA core and have your scripts updated for eventual changes of it. You’d also need 2 connections (tunnels).

1 Like

Hi and thanks a lot so far!

That sounds great.

Ok, I thought I could use knx.send and knx.read only in automations and not in Python code. At least I wasn’t able to find any examples. Could you give me a hint where I can find some? That would be great. All my searches ended up with using xknx directly so far.

Having to stick with the HA core xknx version is a good point. The tunnels itself wouldn’t be a problem, I have a KNX IP Gateway, which provides me 8 tunnels. I just haven’t checked by now how I could map multiple of them to one HA instance.

I have never done any scripting for that. But I guess you can interface with HA entities, events and services with those mentioned integrations - independently of knx specific stuff.
See eg. Writing AppDaemon Apps — AppDaemon 4.4.2 documentation