JSON call does not work in new version Topic is solved

Moderator: leecollings

Post Reply
RonEN
Posts: 11
Joined: Tuesday 07 June 2016 10:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

JSON call does not work in new version

Post by RonEN »

After updating to the latest version and installing Python3, the JSON call below no longer works.

httpresponse = urllib.urlopen("http://"+ domoticzserver +"/json.htm?type=command&param=udevice&idx=27&nvalue=" + str (Ingang_17_1) + "&svalue=" + str (Text_17) )

I've tried many things but I can't get it to work anymore. I often get the syntax error.
The urllib.urlopen changed to urllib.request.urlopen (for python3) also gives no result
I use this call to update virtual sensors visible in the device tab.
Can someone tell me where I'm going wrong. It's probably something simple that I'm overlooking. :geek:

Ronald.
willemd
Posts: 630
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: JSON call does not work in new version

Post by willemd »

username and password required? If it is a call from outside domoticz.
https://www.domoticz.com/wiki/Security
User avatar
habahabahaba
Posts: 215
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: JSON call does not work in new version

Post by habahabahaba »

Logs?
User avatar
waltervl
Posts: 5397
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: JSON call does not work in new version

Post by waltervl »

Check Python2 to Python3 changes. You cannot always use python2 programs in Python3
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
RonEN
Posts: 11
Joined: Tuesday 07 June 2016 10:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: JSON call does not work in new version

Post by RonEN »

Thanks for the responses.

@willemd : I have already set the trusted networks settings for the IP address. I don't get an access denied message
@waltervl : I have seen that there is a conversion tool from Python2 to Python3. I think I'll give that a try. Individual changes I tried from P2 to P3 did not work.
@habahabahaba : If I have used the conversion tool and it still goes wrong, I will provide logs because I really don't know anymore. :(

Perhaps someone sees this and knows the correct code for Python3 to upload the 2 variables to Domoticz.
User avatar
habahabahaba
Posts: 215
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: JSON call does not work in new version

Post by habahabahaba »

Can you catch "domoticzserver " ?
Adress? port number ?
May be you are using https not http?
Kedi
Posts: 564
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: JSON call does not work in new version

Post by Kedi »

str (Text_17) should be between quotes e.g. ""
Logic will get you from A to B. Imagination will take you everywhere.
willemd
Posts: 630
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: JSON call does not work in new version

Post by willemd »

The url string looks to be composed correctly, but you can double check by printing it.

What is the exact error message you get? Just syntax error? Syntax error on composing the string or on calling the urlopen?

Did you include the other compulsory parameters of the urlopen command ?
https://docs.python.org/3/library/urlli ... ib.request

If you call the command in a browser it works without error messages?

If you run another urlopen command, it works?
User avatar
waltervl
Posts: 5397
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: JSON call does not work in new version

Post by waltervl »

Here is an example of a Python3 script filling the IP address of the Domoticz host into a Domoticz text device.
The wiki has 2 versions, Python2 and Python3

https://www.domoticz.com/wiki/Python_-_ ... python3.29
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
lost
Posts: 643
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: JSON call does not work in new version

Post by lost »

As waltervi pointed out, you IMO now need 2 lines. Here is a code snippet extracted from a script that was migrated from python 2.7 to 3.x, with "urilopen1" that was the previous 2.7 import and "urlopen2" and "Request" new python 3 imports as:

Code: Select all

from urllib.request import urlopen as urlopen2, Request


I first named urlopen1 and urlopen2 to be able to switch from one to another while testing...
So, hereunder, you have the (commented) python 2.7/urlib1 version and the (now used) python3/Request+urlopen2().read versions:

Code: Select all

    try:
        #check_page = urlopen1(CHECK_URL+caller_id)
        r = Request(CHECK_URL+caller_id, headers={'User-Agent': 'Mozilla/5.0'})
        check_page = urlopen2(r).read()
    except: # pylint: disable=bare-except
        logger.log(logging.INFO, "Connection failed : "+CHECK_URL)
        return ret
This should make needed changes on your side more straightforward.
RonEN
Posts: 11
Joined: Tuesday 07 June 2016 10:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: JSON call does not work in new version

Post by RonEN »

Gents,

Thanks for your thoughts. I've tried some tips and I think it's in the Python call.
If I copy the code from HTTP to the browser, it works and I get the text in the device and the coloring of the symbol in Domoticz.
When I run the Python code in a terminal I get the error "SyntaxError: Invalid syntax".

Code: Select all

#!/usr/bin/python3
import urllib
from urllib.request import urlopen as urlopen2, Request

r = Request(http://192.168.151.23:8080/json.htm?type=command&param=udevice&idx=7&nvalue=5+&svalue=Put vol)
check_page = urlopen2(r).read()
any suggestions?
User avatar
habahabahaba
Posts: 215
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: JSON call does not work in new version

Post by habahabahaba »

willemd wrote: Monday 20 November 2023 21:12 username and password required? If it is a call from outside domoticz.
https://www.domoticz.com/wiki/Security
Just try it :)
lost
Posts: 643
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: JSON call does not work in new version

Post by lost »

RonEN wrote: Wednesday 22 November 2023 20:32 any suggestions?
Use quotes, as written your URL is not a string...
willemd
Posts: 630
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: JSON call does not work in new version

Post by willemd »

What if you abandon urllib and just do the following:

import requests, json
URLstring="the url string you are building in your example"
response=requests.get(URLstring)
responseResult=str(response.json())
if reponseResult=="ERR":
etc.
etc.

I get good results when I do this in my python3 programs using the domoticz URL strings.
RonEN
Posts: 11
Joined: Tuesday 07 June 2016 10:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: JSON call does not work in new version

Post by RonEN »

A breakthrough!

With the above help I now indeed get the text in my device and the symbol changes color. :)

Code: Select all

URLstring="http://192.168.51.23:8080/json.htm?type=command&param=udevice&idx=7&nvalue=5+&svalue=Put vol"
However, if I want to use the variables in the string of my first post in the current string, things go wrong again. I get the name string '(Text_17)' in my drvice or I get "SyntaxError: invalid syntax" when I try a different format I have been trying to get this right for a while now, but without any results. :roll:

I would like one last tip to get it working with the 2 variables.
User avatar
waltervl
Posts: 5397
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: JSON call does not work in new version

Post by waltervl »

I think we already gave enough hints how to troubleshoot your issue.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
willemd
Posts: 630
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: JSON call does not work in new version

Post by willemd »

Show us the exact piece of code that your are using
RonEN
Posts: 11
Joined: Tuesday 07 June 2016 10:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: JSON call does not work in new version

Post by RonEN »

Gentlemen,

Success. :D
After many hours of fussing and trying, I finally got it working the way I want it to. The call is slightly different than what you suggested, but it works for me.
Below is my test program.

Code: Select all

#!/usr/bin/python3
#imports
import json
import requests

# Setup variables
Domoticz_Server = "192.168.151.23:8080"
Devices_IDX = 7
Ingang_17_1 = 3
Text_17 = "Put vol"

# Update values in Domoticz
request = requests.get('http://' + Domoticz_Server + '/json.htm?type=command&param=udevice&idx=' + str(Devices_IDX) + '&nvalue=' + str(Ingang_17_1) + '&svalue=' + str(Text_17))
I struggled enormously with implementing the variables, but now it finally works and I can start applying it.
Many thanks for your thoughts and suggestions. It helped me enormously.

Cheers,
Ronald.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest