Page 1 of 1

JSON call does not work in new version

Posted: Monday 20 November 2023 20:37
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.

Re: JSON call does not work in new version

Posted: Monday 20 November 2023 21:12
by willemd
username and password required? If it is a call from outside domoticz.
https://www.domoticz.com/wiki/Security

Re: JSON call does not work in new version

Posted: Tuesday 21 November 2023 8:13
by habahabahaba
Logs?

Re: JSON call does not work in new version

Posted: Tuesday 21 November 2023 10:22
by waltervl
Check Python2 to Python3 changes. You cannot always use python2 programs in Python3

Re: JSON call does not work in new version

Posted: Tuesday 21 November 2023 10:54
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.

Re: JSON call does not work in new version

Posted: Tuesday 21 November 2023 12:11
by habahabahaba
Can you catch "domoticzserver " ?
Adress? port number ?
May be you are using https not http?

Re: JSON call does not work in new version

Posted: Tuesday 21 November 2023 12:30
by Kedi
str (Text_17) should be between quotes e.g. ""

Re: JSON call does not work in new version

Posted: Tuesday 21 November 2023 19:52
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?

Re: JSON call does not work in new version

Posted: Wednesday 22 November 2023 14:42
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

Re: JSON call does not work in new version

Posted: Wednesday 22 November 2023 16:31
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.

Re: JSON call does not work in new version

Posted: Wednesday 22 November 2023 20:32
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?

Re: JSON call does not work in new version

Posted: Thursday 23 November 2023 4:57
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 :)

Re: JSON call does not work in new version

Posted: Thursday 23 November 2023 11:22
by lost
RonEN wrote: Wednesday 22 November 2023 20:32 any suggestions?
Use quotes, as written your URL is not a string...

Re: JSON call does not work in new version

Posted: Thursday 23 November 2023 13:05
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.

Re: JSON call does not work in new version

Posted: Thursday 23 November 2023 23:04
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.

Re: JSON call does not work in new version

Posted: Thursday 23 November 2023 23:37
by waltervl
I think we already gave enough hints how to troubleshoot your issue.

Re: JSON call does not work in new version

Posted: Friday 24 November 2023 11:50
by willemd
Show us the exact piece of code that your are using

Re: JSON call does not work in new version

Posted: Friday 24 November 2023 16:17
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.