How to get value from sensors Topic is solved

Python and python framework

Moderator: leecollings

Post Reply
mattek
Posts: 3
Joined: Saturday 02 January 2021 20:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

How to get value from sensors

Post by mattek »

I want to display sensor values ​​but cannot access these values. I have created a script (python) for connecting Raspberry with Telegram and this is where I'm going to display the data, but first I need to download it somehow. So I'm using code https://www.domoticz.com/wiki/Python_-_ ... python3.29 but it doesn't work for me.

Code of file "test1.py":

Code: Select all

import json
import urllib.request, urllib.error, urllib.parse

domoticzurl = 'http://192.168.1.210:8080/json.htm?type=devices&rid=20'

def domoticzrequest (url):
  request = urllib.request.Request(url)
  response = urllib.request.urlopen(request)
  return response.read()

json_object = json.loads(domoticzrequest(domoticzurl))
if json_object["status"] == "OK":
	temp = json_object["result"][0]['Temp']
print(temp)
IP address is correct. When I check the URL in the browser than I get a result.

When i run the code i get something like that:
Spoiler: show

Code: Select all

pi@raspberrypi:~ $ python3 test1.py
Traceback (most recent call last):
  File "/usr/lib/python3.7/urllib/request.py", line 1324, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/usr/lib/python3.7/http/client.py", line 1244, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.7/http/client.py", line 1290, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.7/http/client.py", line 1239, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.7/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/lib/python3.7/http/client.py", line 966, in send
    self.connect()
  File "/usr/lib/python3.7/http/client.py", line 938, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "/usr/lib/python3.7/socket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.7/socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test1.py", line 38, in <module>
    json_object = json.loads(domoticzrequest(domoticzurl))
  File "test1.py", line 34, in domoticzrequest
    response = urllib.request.urlopen(request)
  File "/usr/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.7/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/usr/lib/python3.7/urllib/request.py", line 543, in _open
    '_open', req)
  File "/usr/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.7/urllib/request.py", line 1352, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "/usr/lib/python3.7/urllib/request.py", line 1326, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno -2] Name or service not known>
Does anybody have an idea what is wrong ?

Some details:
RaspberryPi 4B
Python3 version 3.7.3
Domoticz version 2020.2
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to get value from sensors

Post by waaren »

mattek wrote: Sunday 03 January 2021 12:36 So I'm using code https://www.domoticz.com/wiki/Python_-_ ... python3.29 but it doesn't work for me.
Does anybody have an idea what is wrong ?
Below script works for me (after changing IP / Port to mine)

Code: Select all

import json
import urllib.request, urllib.error, urllib.parse

domoticzurl = 'http://192.168.1.210:8080/json.htm?type=command&param=getversion'

def domoticzrequest (url):
    request = urllib.request.Request(url)
    response = urllib.request.urlopen(request)
    return response.read()

json_object = json.loads(domoticzrequest(domoticzurl))
if json_object["status"] == "OK":
    print(json_object["version"])


sudo python3 testConnection.py

Code: Select all

2020.2 (build 12815)
Did you double check IP / Port and if domoticz is set to allow access from local / local network without password?

[setup][settings][system]

Local Networks (no username/password):
127.0.0.1;192.168.1.*;::1
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
mattek
Posts: 3
Joined: Saturday 02 January 2021 20:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to get value from sensors

Post by mattek »

Great, It works !

My fault because I didn't read the steps:
Make sure that in the Security section in the settings (Setup > Settings > System > Local Networks (no username/password) you allow 127.0.0.1 (and / or ::1 when using IPv6 ) to not need a password.
Is there any option for this address to be set to login to Domoticz with a password and for this script to also work? I mean safety, of course.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to get value from sensors

Post by waaren »

mattek wrote: Monday 04 January 2021 22:53 Is there any option for this address to be set to login to Domoticz with a password and for this script to also work? I mean safety, of course.
Only when you include username / password in your script. Not very secure in my book... :)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waltervl
Posts: 5910
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: How to get value from sensors

Post by waltervl »

You will have to modify the Python script to put the authorisation parameters in the JSON command
.
See section format of the documentation https://www.domoticz.com/wiki/Domoticz_ ... 27s#Format
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
mattek
Posts: 3
Joined: Saturday 02 January 2021 20:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to get value from sensors

Post by mattek »

I want to use authorization with username and password in script. When I try to use format with url "http://192.168.1.210:8080/json.htm?type=devices&rid=20" (without username/password) I get:
Traceback (most recent call last):
File "test2.py", line 31, in <module>
json_object = json.loads(domoticzrequest(domoticzurl)) # retrieve sensor data
File "test2.py", line 26, in domoticzrequest
response = urllib.request.urlopen(request)
File "/usr/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.7/urllib/request.py", line 531, in open
response = meth(req, response)
File "/usr/lib/python3.7/urllib/request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.7/urllib/request.py", line 569, in error
return self._call_chain(*args)
File "/usr/lib/python3.7/urllib/request.py", line 503, in _call_chain
result = func(*args)
File "/usr/lib/python3.7/urllib/request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Unauthorized
But when I use url "http://test:[email protected]:8080/jso ... ces&rid=20" I get the same error like in my first post. The user with name "test" not working with"user", "admin" or "viewer" rights. I set devices for the user, but It still not working. Anything else I could try ?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest