Page 1 of 1

Problem with Python based Events

Posted: Thursday 10 August 2017 13:48
by Maxovitsj
Hi y'all!

So I've recently started using Domoticz, I'm loving it so far!
My post is in regards to the Event system, mainly the Python one.
I, for the live of me, can not get it to work.

My 'Test' event is as follows:

Code: Select all

import domoticz

domoticz.log("Hi Domoticz!")
That's it... can someone tell me what I'm doing wrong?
Also, where are these events stored on the file system?

Thanks in advance!

-Max

Re: Problem with Python based Events

Posted: Thursday 10 August 2017 15:52
by niceandeasy
An 'Event' is something that happens.
The script you made is just a short script. If this script was made using the GUI, then it is stored in Domoticz's database. You can also put scripts in the domoticz/scripts/python directory.

Python support is fairly new. LUA support has been there for a long time, so my experiences are just based on LUA.

I wrote: "An 'Event' is something that happens."
So, for example, when you push a button on a remote control, or in the GUI, you cause an event and your script gets executed. But in in your script, there's no code that checks which event 'happened'. This probably causes this script not to be executed at all, no matter what events happen.

You should read up on 'Events' at http://www.domoticz.com/wiki/Events and on the python example script in the GUI.
And:
In the GUI, setup, more options, events, just create a new Python script, do not change anything to the example script, Set it active and save it.
Then go to setup, log
Then cause some Events, for example, push some buttons on a remote control for your lights, or, whatever, and see what appears in the log. After that, start changing things in the example script.

Re: Problem with Python based Events

Posted: Thursday 10 August 2017 18:36
by Maxovitsj
Thanks for getting back to me.

I understand the concept of Events. I've fiddled around with the LUA one and that one works fine.
I did what you said, used the default Event for Python but that doesn't do anything either.

Trying to 'import domoticz' in my test.py in the /scripts/python directory throws me the error:

Code: Select all

  File "/home/pi/domoticz/scripts/python/domoticz.py", line 30, in <module>
    import DomoticzEvents as domoticz_ #
ImportError: No module named 'DomoticzEvents'

Re: Problem with Python based Events

Posted: Thursday 10 August 2017 21:33
by moroen
Maxovitsj wrote: Thursday 10 August 2017 18:36 Thanks for getting back to me.

I understand the concept of Events. I've fiddled around with the LUA one and that one works fine.
I did what you said, used the default Event for Python but that doesn't do anything either.

Trying to 'import domoticz' in my test.py in the /scripts/python directory throws me the error:

Code: Select all

  File "/home/pi/domoticz/scripts/python/domoticz.py", line 30, in <module>
    import DomoticzEvents as domoticz_ #
ImportError: No module named 'DomoticzEvents'
Look's like you're trying to execute the python-script from a shell? The DomoticzEvents module is only available in the instance of python running embedded in domoticz. To execute a script when something changes, it needs to follow the correct naming-convention, for example:

domoticz/scripts/python/script_device_test.py

this will be executed every time a device changes. The name after script_device_ doesn't matter, as long as it doesn't end with demo.py, as such scripts will be ignored.

In the script it's vital to check what device has changed, especially if using the script to change the state of the same or another device, to avoid a race condition. In the default distribution, you'll find a script_device_test_demo.py, it shows some of the basic functionality!

BTW: script_time_*.py gets executed once every minute...

Regards,
M

Re: Problem with Python based Events

Posted: Thursday 10 August 2017 22:39
by Maxovitsj
Moroen, thanks for the reply.

Makes total sense.
I've made a script in the /scripts/python directory called: "script_time_test.py" with the contents being the same as the "script_time_demo.py" but that doesn't work either. I'm supposed to see those messages in the Log console right?

Re: Problem with Python based Events

Posted: Friday 11 August 2017 0:36
by moroen
Maxovitsj wrote: Thursday 10 August 2017 22:39 Moroen, thanks for the reply.

Makes total sense.
I've made a script in the /scripts/python directory called: "script_time_test.py" with the contents being the same as the "script_time_demo.py" but that doesn't work either. I'm supposed to see those messages in the Log console right?
Yes, that's correct, you should see a log entry every minute, or if there's an error in the script, an error-message every minute... If you see nothing, it might mean that event-scripts aren't firing as they should.

What version of domoticz are you using? The latest stable binary should have python events enabled by default, as far as I know!

Regards,
M

Re: Problem with Python based Events

Posted: Friday 11 August 2017 0:37
by moroen
Maxovitsj wrote: Thursday 10 August 2017 22:39 Moroen, thanks for the reply.

Makes total sense.
I've made a script in the /scripts/python directory called: "script_time_test.py" with the contents being the same as the "script_time_demo.py" but that doesn't work either. I'm supposed to see those messages in the Log console right?
Yes, that's correct, you should see a log entry every minute, or if there's an error in the script, an error-message every minute... If you see nothing, it might mean that event-scripts aren't firing as they should.

What version of domoticz are you using? The latest stable binary should have python events enabled by default, as far as I know! You should also make sure that domoticz actually finds python and enables it. If the correct python libs can't be found at startup, all of the python subsystems are just ignored. You'll see this upon startup:

Code: Select all

2017-08-11 00:47:32.459  Domoticz V3.8153 (c)2012-2017 GizMoCuz
2017-08-11 00:47:32.459  Build Hash: 494fff7, Date: 2017-07-30 12:19:41
2017-08-11 00:47:32.459  Startup Path: /opt/domoticz/
2017-08-11 00:47:32.474  Sunrise: 05:31:00 SunSet:21:18:00
2017-08-11 00:47:32.474  EventSystem: reset all events...
2017-08-11 00:47:32.495  PluginSystem: Started, Python version '3.4.2'.
opposed to:

Code: Select all

2017-08-11 00:52:45.544  Domoticz V3.8005 (c)2012-2017 GizMoCuz
2017-08-11 00:52:45.544  Build Hash: b9cc58e6, Date: 2017-06-22 10:43:50
2017-08-11 00:52:45.544  Startup Path: /Users/mak/Prosjekter/Domoticz/dev-domoticz/
2017-08-11 00:52:45.552  Sunrise: 05:31:00 SunSet:21:18:00
2017-08-11 00:52:45.552  EventSystem: reset all events...
2017-08-11 00:52:45.552  PluginSystem: Failed dynamic library load, install the latest libpython3.x library that is available for your platform.
Regards,
M

Re: Problem with Python based Events

Posted: Saturday 12 August 2017 12:49
by Maxovitsj
I get this message:

Code: Select all

PluginSystem: Failed dynamic library load, install the latest libpython3.x library that is available for your platform.
I installed 'libpython3.4' but it still gives me that message.
I'm running the latest stable version on a fresh Raspberry Pi installation.

Re: Problem with Python based Events

Posted: Saturday 12 August 2017 19:43
by Tinmanz
Hi guys,

I'm having the same issue :?

My setup

* Raspberry Pi 3B
* Raspbian Jessie Lite, from 2016-11-25-raspbian-jessie-lite.img (with Python 2.7.9) and followed https://www.domoticz.com/wiki/Initial_R ... y_Pi_Setup for the initial setup
* Domoticz V3.8278 (Beta Release Channel)

Now I installed Python 3.4 next to 2.7.9, followed by an install of libpython3.4, but also after a full restart of the Pi in the Error tab of the Domoticz Log I still see:

Code: Select all

PluginSystem: Failed dynamic library load, install the latest libpython3.x library that is available for your platform.
Could it be that Domoticz is using the older Python version?

Re: Problem with Python based Events

Posted: Sunday 13 August 2017 20:09
by Maxovitsj
Bumping

Re: Problem with Python based Events

Posted: Sunday 13 August 2017 20:19
by SweetPants
Maxovitsj wrote: Sunday 13 August 2017 20:09Bumping
Search the forum. This has been handled numerous times

Re: Problem with Python based Events

Posted: Sunday 13 August 2017 20:28
by Maxovitsj
Fixed it by running: 'sudo apt-get install python3-dev -y' :lol:

Re: Problem with Python based Events

Posted: Sunday 13 August 2017 22:09
by Tinmanz
Solved it for me too, thanks :D