get_gas_values(domoticzurl, domoticzdeviceid_gas) stopped working

Python and python framework

Moderator: leecollings

Post Reply
prutsky
Posts: 3
Joined: Saturday 14 June 2025 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

get_gas_values(domoticzurl, domoticzdeviceid_gas) stopped working

Post by prutsky »

Hi,
I'm running two scripts which uses "get_gas_values(domoticzurl, domoticzdeviceid_gas)". These scripts worked fine until May 6th 2025.

Now I get the following error:

Code: Select all

/domoticz/scripts $ python3 gasstanden.py 
Traceback (most recent call last):
  File "/home/pi/domoticz/scripts/gasstanden.py", line 52, in <module>
    GasMeterReading = get_gas_values(domoticzurl, domoticzdeviceid_gas)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/domoticz/scripts/gasstanden.py", line 39, in get_gas_values
    device_data = Domoticz(url).get_device(device_id)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/domoticz/scripts/gasstanden.py", line 31, in get_device
    data = json.load(self.__execute__(url))
                     ^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/domoticz/scripts/gasstanden.py", line 24, in __execute__
    return urllib.request.urlopen(req, timeout=5)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/urllib/request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/urllib/request.py", line 525, in open
    response = meth(req, response)
               ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/urllib/request.py", line 634, in http_response
    response = self.parent.error(
               ^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/urllib/request.py", line 563, in error
    return self._call_chain(*args)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/urllib/request.py", line 496, in _call_chain
    result = func(*args)
             ^^^^^^^^^^^
  File "/usr/lib/python3.11/urllib/request.py", line 643, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
The Python code which worked fine before is:

Code: Select all

import sys
import json
import urllib.request, urllib.error, urllib.parse
import re
import time
import datetime
import http.client, urllib.request, urllib.parse, urllib.error

def open_port():
    pass

def close_port():
    pass

class Domoticz():
    
    def __init__(self, url):
        
        self.baseurl = url
        
    def __execute__(self, url):

        req = urllib.request.Request(url)
        return urllib.request.urlopen(req, timeout=5)
       
    def get_device(self, xid):
        """
        Get the Domoticz device information.
        """
        url = "%s/json.htm?type=devices&rid=%s" % (self.baseurl, xid)
        data = json.load(self.__execute__(url))
        return data

def get_gas_values(url, device_id):
    """
    Get gasmeter reading.
    """
    
    device_data = Domoticz(url).get_device(device_id)
    data = device_data['result'][0]['Data']

    ex = re.compile('^([0-9\.]+)$')

    groups = ex.match(data).group
    gasstand = float(groups(1)) #/ 1000

    return gasstand
# example usage

domoticzurl = "http://192.168.2.44:8084"
domoticzdeviceid_gas = 10
GasMeterReading = get_gas_values(domoticzurl, domoticzdeviceid_gas)

print ("Gasmeterstand:\t\t\t\t"+str(GasMeterReading).replace('.',',')+"m3")

# creeer een bestand met de waarde / create a file with the readings
name = '/home/pi/domoticz/scripts/gasverbruik.csv'  # Naam tekstbestand / Name of text file 
now = datetime.datetime.now()
 
try: 
    file = open(name,'a')   # create file / creeer bestand
    file.write(str(now.strftime("%Y-%m-%d %H:%M")) + ';' + str(GasMeterReading).replace('.', ',') +'\n')
except: # something went wrong / in het vorige blok is iets niet goed gegaan (error)
    print('Something went wrong!')
 
What am I missing that this ain't working anymore?
User avatar
waltervl
Posts: 5852
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: get_gas_values(domoticzurl, domoticzdeviceid_gas) stopped working

Post by waltervl »

You are using deprecated API calls in the script.
For example
/json.htm?type=devices
will be
/json.htm?type=command&param=getdevices

See the wiki how to solve it:
https://wiki.domoticz.com/Domoticz_API/ ... and_newer)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
prutsky
Posts: 3
Joined: Saturday 14 June 2025 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: get_gas_values(domoticzurl, domoticzdeviceid_gas) stopped working

Post by prutsky »

Thanks for clearing that out! I got this working:

Code: Select all

        url = f"{self.baseurl}/json.htm?type=command&param=getdevices"
        data = json.load(self.__execute__(url))
        return data

def get_gas_values(url, device_id):
    """
    Get gasmeter reading.
    """
    
    device_data = Domoticz(url).get_device(device_id)
    data = device_data['result'][0]['Data']
    print (data) 
    ex = re.compile('^([0-9\.]+)$')

    groups = ex.match(data).group
    gasstand = float(groups(1)) #/ 1000
The output is changed also it seams. I now get the next error:

Code: Select all

python3 gasstanden.py 
10281666;9519876;2372162;5242921;365;0
Traceback (most recent call last):
  File "/home/pi/domoticz/scripts/gasstanden.py", line 51, in <module>
    GasMeterReading = get_gas_values(domoticzurl, domoticzdeviceid_gas)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/domoticz/scripts/gasstanden.py", line 43, in get_gas_values
    groups = ex.match(data).group
             ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'group'
Sorry my Python skills are limited...
prutsky
Posts: 3
Joined: Saturday 14 June 2025 9:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: get_gas_values(domoticzurl, domoticzdeviceid_gas) stopped working

Post by prutsky »

I found out my get_devices call was not correct (not using the xid).

The correct one is: url = "%s/json.htm?type=command&param=getdevices&rid=%s" % (self.baseurl, xid)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest