Accessing User Variables using internal Python Script

Python and python framework

Moderator: leecollings

Post Reply
CPtje
Posts: 6
Joined: Sunday 11 November 2018 17:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Accessing User Variables using internal Python Script

Post by CPtje »

Really thankful that I encountered the internal Python(3) scripting possibility with the (new) DomoticsEvents library.
Currently I am rewriting my LUA scripts to internal Python3 scipts but now I'm stuck because I cannot find how to acces the Domoticz (global) user variables using the internal Python scripting.

There does not seem to be much help info (WIKI?) on the current possibilities of the DomoticzEvents library for Python3 so is there anybody who can help me in how to access my Domoticz User variables or any other way for using global variables with internal Python scripting?

Carlo
Xorfor

Re: Accessing User Variables using internal Python Script

Post by Xorfor »

You can use the json/API from Domoticz: https://www.domoticz.com/wiki/Domoticz_ ... _variables with curl or urllib.request.

I use these calls in a Python Domoticz API. Look here for more ideas: https://github.com/Xorfor/Domoticz-API/ ... erVariable
CPtje
Posts: 6
Joined: Sunday 11 November 2018 17:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Accessing User Variables using internal Python Script

Post by CPtje »

Thanks Xorfor, gonna have a look at this. Never used these requests before so this is going to be exciting.
Tonio16
Posts: 45
Joined: Friday 23 February 2018 20:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: France
Contact:

Re: Accessing User Variables using internal Python Script

Post by Tonio16 »

Xorfor

Would you have an example or several on how to implement user variable api in a script.?
I tried but with no success.

TX

Antoine
Tonio16
Posts: 45
Joined: Friday 23 February 2018 20:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: France
Contact:

Re: Accessing User Variables using internal Python Script

Post by Tonio16 »

Me again,

In my script I have these error messages:

Code: Select all

 2019-04-29 16:54:00.177 Error: (VMC-W2) 'onStart' failed 'AttributeError':''NoneType' object has no attribute 'api''.
2019-04-29 16:54:00.177 Error: (VMC-W2) ----> Line 310 in /home/pi/domoticz/plugins/DrexelRead/plugin.py, function onStart
2019-04-29 16:54:00.177 Error: (VMC-W2) ----> Line 103 in /home/pi/domoticz/plugins/DrexelRead/plugin.py, function onStart
2019-04-29 16:54:00.177 Error: (VMC-W2) ----> Line 50 in /usr/lib/python3.5/DomoticzAPI/uservariable.py, function __init__ 
Xorfor

Re: Accessing User Variables using internal Python Script

Post by Xorfor »

@Tonio16

An example of a script can be found in tests directory: https://github.com/Xorfor/Domoticz-API/ ... ariable.py

If you still have problems, you can send me the plugin.py, and I will take a look.

Meanwhile I will try to make a plugin.py example for uservariables :)
Xorfor

Re: Accessing User Variables using internal Python Script

Post by Xorfor »

I have added an example of a plugin using Uservariables from https://github.com/Xorfor/Domoticz-API. Please look at https://github.com/Xorfor/Domoticz-API/ ... riables.py. What I added was:

1. 21: import DomoticzAPI as dom
2. 36: __init__ - initialize self.__server
2. 60-70: onStart - initialize and create Uservariable
3. 88-92: onHeartbeat - Some debugging information and set the value of the Uservariable

If you look at Setup -> More Options -> User variables, you will find the uservariable `API Test Uservariable` with a value which will change every 10 seconds.

Please be aware that DomoticzAPI is still a beta version! I am still looking for more functionality and optimization.
Tonio16
Posts: 45
Joined: Friday 23 February 2018 20:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: France
Contact:

Re: Accessing User Variables using internal Python Script

Post by Tonio16 »

TX, I will try tomorrow.

Antoine
Tonio16
Posts: 45
Joined: Friday 23 February 2018 20:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: France
Contact:

Re: Accessing User Variables using internal Python Script

Post by Tonio16 »

Ok, I launched your script without anymodification and I have these errors:

Code: Select all

 2019-04-30 17:28:30.439 Status: (test) Initialized version 1.0.0, author 'Xorfor'
2019-04-30 17:28:30.492 Error: (test) 'onStart' failed 'AttributeError':''NoneType' object has no attribute 'api''.
2019-04-30 17:28:30.492 Error: (test) ----> Line 152 in /home/pi/domoticz/plugins/UserVarRW/plugin.py, function onStart
2019-04-30 17:28:30.492 Error: (test) ----> Line 65 in /home/pi/domoticz/plugins/UserVarRW/plugin.py, function onStart
2019-04-30 17:28:30.492 Error: (test) ----> Line 50 in /usr/lib/python3.5/DomoticzAPI/uservariable.py, function __init__
2019-04-30 17:28:39.962 Error: (test) 'onHeartbeat' failed 'AttributeError':''BasePlugin' object has no attribute '_BasePlugin__var''.
2019-04-30 17:28:39.962 Error: (test) ----> Line 136 in /home/pi/domoticz/plugins/UserVarRW/plugin.py, function onHeartbeat
2019-04-30 17:28:39.962 Error: (test) ----> Line 89 in /home/pi/domoticz/plugins/UserVarRW/plugin.py, function onHeartbeat 
I am using the last stable of Domotciz, 4.9700. Should I switch to a beta one?
Do I have to change or take care of something special in my parameters?

I have the same error line in my tries: 2019-04-30 17:28:30.492 Error: (test) ----> Line 50 in /usr/lib/python3.5/DomoticzAPI/uservariable.py, function __init__

Regards
Antoine
Xorfor

Re: Accessing User Variables using internal Python Script

Post by Xorfor »

@Tonio16,

Yes, please update to the latest beta version.

If you work with Python plugins, it is better to stay on the latest beta. Since the latest stable version, some important improvements are made.
Tonio16
Posts: 45
Joined: Friday 23 February 2018 20:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: France
Contact:

Re: Accessing User Variables using internal Python Script

Post by Tonio16 »

Ok, I update to beta, to the lastest Domoticz-API but I have still the same error:

Code: Select all

 2019-04-30 20:26:29.451 Status: Python EventSystem: Initalizing event module.
2019-04-30 20:26:29.451 Status: EventSystem: Started
2019-04-30 20:26:29.451 Status: EventSystem: Queue thread started...
2019-04-30 20:26:29.354 Error: (test) 'onStart' failed 'AttributeError':''NoneType' object has no attribute 'api''.
2019-04-30 20:26:29.354 Error: (test) ----> Line 152 in '/home/pi/domoticz/plugins/UserVarRW/plugin.py', function onStart
2019-04-30 20:26:29.355 Error: (test) ----> Line 65 in '/home/pi/domoticz/plugins/UserVarRW/plugin.py', function onStart
2019-04-30 20:26:29.355 Error: (test) ----> Line 50 in '/usr/lib/python3.5/DomoticzAPI/uservariable.py', function __init__
2019-04-30 20:26:38.867 Error: (test) 'onHeartbeat' failed 'AttributeError':''BasePlugin' object has no attribute '_BasePlugin__var''.
2019-04-30 20:26:38.867 Error: (test) ----> Line 136 in '/home/pi/domoticz/plugins/UserVarRW/plugin.py', function onHeartbeat
2019-04-30 20:26:38.868 Error: (test) ----> Line 89 in '/home/pi/domoticz/plugins/UserVarRW/plugin.py', function onHeartbeat
2019-04-30 20:26:50.264 Error: (test) 'onHeartbeat' failed 'AttributeError':''BasePlugin' object has no attribute '_BasePlugin__var''.
2019-04-30 20:26:50.266 Error: (test) ----> Line 136 in '/home/pi/domoticz/plugins/UserVarRW/plugin.py', function onHeartbeat
2019-04-30 20:26:50.266 Error: (test) ----> Line 89 in '/home/pi/domoticz/plugins/UserVarRW/plugin.py', function onHeartbeat
With my poorly understanding, it looks so that the uservariable.py is not dealing the api.py.

Do I have to install other API than yours?


By installing some other python plugins, I discovered something strange on my installation of librairies, for example with requests:
$ sudo pip3 show requests
Name: requests
Version: 2.21.0
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: [email protected]
License: Apache 2.0
Location: /usr/local/lib/python3.5/dist-packages/requests-2.21.0-py3.5.egg
Requires: certifi, chardet, idna, urllib3

After uninstall and install it looks normal: Location: /usr/local/lib/python3.5/dist-packages

For your Domoticz-API, I hav:
sudo pip3 show DomoticzAPI
Name: DomoticzAPI
Version: 0.13.1b0
Summary: Domoticz API for Python
Home-page: https://github.com/Xorfor/Domoticz-API/
Author: Xorfor
Author-email: [email protected]
License: MIT License
Location: /usr/lib/python3.5
Requires:


Antoine
Tonio16
Posts: 45
Joined: Friday 23 February 2018 20:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: France
Contact:

Re: Accessing User Variables using internal Python Script

Post by Tonio16 »

last point today.
I tried to install the iCal plugin and I had this message:
2019-04-30 22:12:10.128 Error: (test) Error sending command to domoticz : type=command&param=getuservariables
2019-04-30 22:12:10.128 Error: (test) Erreur to execute get user variables

I tried json links directly in Firefoy and it worked: create, modify, read.

Antoine
Xorfor

Re: Accessing User Variables using internal Python Script

Post by Xorfor »

So probably something with your installation? It looks not related to Python or the plugins.

On which platform/version is Domoticz installed?
Did you a default Domoticz install?
Can you ping to localhost?
Are you using the default port 8080?
Etc...
Tonio16
Posts: 45
Joined: Friday 23 February 2018 20:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: France
Contact:

Re: Accessing User Variables using internal Python Script

Post by Tonio16 »

Xorfor wrote: Tuesday 30 April 2019 22:33 So probably something with your installation? It looks not related to Python or the plugins.

On which platform/version is Domoticz installed?
Did you a default Domoticz install?
Can you ping to localhost?
Are you using the default port 8080?
Etc...
Hello

Just to be sure. Is it normal that your plugin is using this path: Location: /usr/lib/python3.5
and not Location: /usr/lib/python3.5/dist-packages like other ?

I missed your answer Xorfor.
I am on a Raspberry Pi 3+. I am regularly doing some apt-get update upgrade, so it should be up to date.
Domoticz is a default install.
Ping localhost seems ok.
Spoiler: show
ping localhost
PING localhost(localhost (::1)) 56 data bytes
64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.187 ms
64 bytes from localhost (::1): icmp_seq=2 ttl=64 time=0.096 ms
64 bytes from localhost (::1): icmp_seq=3 ttl=64 time=0.121 ms
64 bytes from localhost (::1): icmp_seq=4 ttl=64 time=0.145 ms
64 bytes from localhost (::1): icmp_seq=5 ttl=64 time=0.152 ms
64 bytes from localhost (::1): icmp_seq=6 ttl=64 time=0.145 ms
64 bytes from localhost (::1): icmp_seq=7 ttl=64 time=0.148 ms
64 bytes from localhost (::1): icmp_seq=8 ttl=64 time=0.149 ms
64 bytes from localhost (::1): icmp_seq=9 ttl=64 time=0.160 ms
64 bytes from localhost (::1): icmp_seq=10 ttl=64 time=0.157 ms
64 bytes from localhost (::1): icmp_seq=11 ttl=64 time=0.156 ms
64 bytes from localhost (::1): icmp_seq=12 ttl=64 time=0.089 ms
64 bytes from localhost (::1): icmp_seq=13 ttl=64 time=0.147 ms
64 bytes from localhost (::1): icmp_seq=14 ttl=64 time=0.149 ms
64 bytes from localhost (::1): icmp_seq=15 ttl=64 time=0.201 ms
64 bytes from localhost (::1): icmp_seq=16 ttl=64 time=0.149 ms
64 bytes from localhost (::1): icmp_seq=17 ttl=64 time=0.167 ms
64 bytes from localhost (::1): icmp_seq=18 ttl=64 time=0.166 ms
64 bytes from localhost (::1): icmp_seq=19 ttl=64 time=0.165 ms
64 bytes from localhost (::1): icmp_seq=20 ttl=64 time=0.150 ms
64 bytes from localhost (::1): icmp_seq=21 ttl=64 time=0.175 ms
64 bytes from localhost (::1): icmp_seq=22 ttl=64 time=0.197 ms
64 bytes from localhost (::1): icmp_seq=23 ttl=64 time=0.153 ms
ping localhost
PING localhost(localhost (::1)) 56 data bytes
64 bytes from local
host (::1): icmp_seq=1 ttl=64 time=0.187 ms
64 bytes from localhost (::1): icmp_seq=2 ttl=64 time=0.096 ms
64 bytes from localhost (::1): icmp_seq=3 ttl=64 time=0.121 ms
64 bytes from localhost (::1): icmp_seq=4 ttl=64 time=0.145 ms
64 bytes from localhost (::1): icmp_seq=5 ttl=64 time=0.152 ms
64 bytes from localhost (::1): icmp_seq=6 ttl=64 time=0.145 ms
64 bytes from localhost (::1): icmp_seq=7 ttl=64 time=0.148 ms
64 bytes from localhost (::1): icmp_seq=8 ttl=64 time=0.149 ms
64 bytes from localhost (::1): icmp_seq=9 ttl=64 time=0.160 ms
64 bytes from localhost (::1): icmp_seq=10 ttl=64 time=0.157 ms
64 bytes from localhost (::1): icmp_seq=11 ttl=64 time=0.156 ms
64 bytes from localhost (::1): icmp_seq=24 ttl=64 time=0.150 ms
64 bytes from localhost (::1): icmp_seq=25 ttl=64 time=0.155 ms
64 bytes from localhost (::1): icmp_seq=26 ttl=64 time=0.144 ms
^C
--- localhost ping statistics ---
26 packets transmitted, 26 received, 0% packet loss, time 26020ms
rtt min/avg/max/mdev = 0.089/0.152/0.201/0.028 ms
Default port of Domoticz is 8080.

Here the debug:

Code: Select all

 2019-05-05 14:11:10.325 Status: (test) Entering work loop.
2019-05-05 14:11:10.325 Status: (test) Initialized version 1.0.0, author 'Xorfor'
2019-05-05 14:11:10.327 Error: (test) 'onStart' failed 'AttributeError':''NoneType' object has no attribute 'api''.
2019-05-05 14:11:10.327 Error: (test) ----> Line 156 in '/home/pi/domoticz/plugins/UserVarRW/plugin.py', function onStart
2019-05-05 14:11:10.327 Error: (test) ----> Line 69 in '/home/pi/domoticz/plugins/UserVarRW/plugin.py', function onStart
2019-05-05 14:11:10.327 Error: (test) ----> Line 50 in '/usr/lib/python3.5/DomoticzAPI/uservariable.py', function __init__
2019-05-05 14:11:20.327 (test) Pushing 'onHeartbeatCallback' on to queue
2019-05-05 14:11:20.348 (test) Processing 'onHeartbeatCallback' message
2019-05-05 14:11:20.348 (test) Calling message handler 'onHeartbeat'.
2019-05-05 14:11:20.349 (test) onHeartbeat called
2019-05-05 14:11:20.349 (test) self.__server: Server("localhost", "8080"): False
2019-05-05 14:11:20.349 Error: (test) 'onHeartbeat' failed 'AttributeError':''BasePlugin' object has no attribute '_BasePlugin__var''.
2019-05-05 14:11:20.349 Error: (test) ----> Line 140 in '/home/pi/domoticz/plugins/UserVarRW/plugin.py', function onHeartbeat
2019-05-05 14:11:20.349 Error: (test) ----> Line 93 in '/home/pi/domoticz/plugins/UserVarRW/plugin.py', function onHeartbeat
2019-05-05 14:11:30.328 (test) Pushing 'onHeartbeatCallback' on to queue
2019-05-05 14:11:30.367 (test) Processing 'onHeartbeatCallback' message
2019-05-05 14:11:30.367 (test) Calling message handler 'onHeartbeat'.
2019-05-05 14:11:30.367 (test) onHeartbeat called
2019-05-05 14:11:30.367 (test) self.__server: Server("localhost", "8080"): False
2019-05-05 14:11:30.368 Error: (test) 'onHeartbeat' failed 'AttributeError':''BasePlugin' object has no attribute '_BasePlugin__var''.
2019-05-05 14:11:30.368 Error: (test) ----> Line 140 in '/home/pi/domoticz/plugins/UserVarRW/plugin.py', function onHeartbeat
2019-05-05 14:11:30.368 Error: (test) ----> Line 93 in '/home/pi/domoticz/plugins/UserVarRW/plugin.py', function onHeartbeat 
Tx

Antoine
Xorfor

Re: Accessing User Variables using internal Python Script

Post by Xorfor »

Hmmm. /usr/lib/python3.5 is one of the directories in the path for Python. You can try:

Code: Select all

sudo pip3 install git+git://github.com/Xorfor/Domoticz-API.git -t /usr/lib/python3.5/dist-packages
or try on your RPi:

Code: Select all

 curl 'http://localhost:8080/json.htm?type=command&param=getSunRiseSet'
Tonio16
Posts: 45
Joined: Friday 23 February 2018 20:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: France
Contact:

Re: Accessing User Variables using internal Python Script

Post by Tonio16 »

Hello Xorfor

Tx a lot to help me.

If I do the curl, I have this message:
<html><head><title>Unauthorized</title></head><body><h1>401 Unauthorized</h1></body></html>
With sudo, it is the same.

With the IP address, 192.168.x.x, instead of localhost or 127.0.0.1, it works.

I am running Domoticz on a SSD and not a SD-card, could it be the reason ?

Regards

Antoine
Xorfor

Re: Accessing User Variables using internal Python Script

Post by Xorfor »

Antoine,

Please check your setting Local Networks (no username/password). I guess that you have the value 192.168.*.*, or something like this. Please add 128.0.0.*, so that you get 192.168.*.*;128.0.0.*.

What you also can do is to change /home/pi/domoticz/plugins/UserVarRW/plugin.py:

Code: Select all

...

    def __init__(self):
        self.__runAgain = 0
        self.__server = dom.Server()
        
        ...
to

Code: Select all

...

    def __init__(self):
        self.__runAgain = 0
        self.__server = dom.Server("<ip adress Domoticz server>", "8080")
        
        ...
Tonio16
Posts: 45
Joined: Friday 23 February 2018 20:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: France
Contact:

Re: Accessing User Variables using internal Python Script

Post by Tonio16 »

Hello Xorfor

I looked to my parameters and the IP address range was false, a "," instead of a ";" between 192.168.*.* and 127.0.0.*
After my correction, the json command is working but not the plugins.

Code: Select all

 curl 'http://127.0.0.1:8080/json.htm?type=command&param=getSunRiseSet'
With this command, the json is also not working.

Code: Select all

 curl 'http://localhost:8080/json.htm?type=command&param=getSunRiseSet'
I understand well that localhost and 127.0.0.1 shoud behave the same here?

With this setting in the plugin, it's working.

Code: Select all

    def __init__(self):
        self.__runAgain = 0
        self.__server = dom.Server("192.168.x.z","8080")
Thanks a lot for your support. I hope that it will somehow help you for the développement of this plugin.

Antoine
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests