JSON API addition

Use this forum to discuss possible implementation of a new feature before opening a ticket.
A developer shall edit the topic title with "[xxx]" where xxx is the id of the accompanying tracker id.
Duplicate posts about the same id. +1 posts are not allowed.

Moderators: leecollings, remb0

Post Reply
lost
Posts: 664
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

JSON API addition

Post by lost »

Hello,

I'm quite sure I already done this suggestion a few years ago, but looks there was no addition in the wiki about this:

From HTTP/JSON API, we can change a switch... but we can't retrieve it's current on/of/level position:

In the past, I already had to deal with this and today again. A few years ago, this ended with a device script that copies switch current status to a user var... and as getuservariables is implemented (not only set/command side, as for switches) I was able to get switch status from external python services needing this data.

For API coherence and avoiding such dirty hacks, would be nice to add an API for this: A "switchget" on top of "switchcmd" for instance.

If there is already a way to get this easily (at least easier than current hack), please let me know...
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: JSON API addition

Post by boum »

Have you tried:

Code: Select all

http://DZ/json.htm?type=devices&rid=IDX
?
lost
Posts: 664
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: JSON API addition

Post by lost »

boum wrote: Wednesday 14 October 2020 15:15 Have you tried:

Code: Select all

http://DZ/json.htm?type=devices&rid=IDX
?
I just did: This may do the job, must agree, but needs quite heavy JSON parsing as this returns full switch data (mostly linked to Domoticz internals) and even more (you also get sunset/sunrise... as well).

This does not really compare with user variables counterpart that just returns useful data:

Code: Select all

/json.htm?type=command&param=getuservariable&idx=IDX
If there is no other solution that may not have made it's way in the wiki, I'll use your suggestion indeed. Just hope JSON response will not change in the future. This may be made easier IMO but at least removes external dirty dependencies difficult to maintain over time!
lost
Posts: 664
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: JSON API addition

Post by lost »

Looks this works, added a get switch to a module that interacts with Domoticz:

Code: Select all

dmtJsonGetSwitch = {"type":"devices", "rid":999}

# Get domoticz switch status, returns sys.maxint if error
def dmtGetSwitch(url, idx, name, logger):
    """
    Get Domoticz switch status, returns -1 if idx/name mismatch, -2 if connection fails...
    """
    ret = -1

    dmtJsonGetSwitch['rid'] = idx

    try:
        # Connect to Domoticz via JSON API and send data
        dmtRget = requests.get(url, params=dmtJsonGetSwitch)
    except requests.exceptions.RequestException as dmtErr:
        logger.log(logging.ERROR, "Unable to connect with URL=%s \nGet requests error %s" % (url, dmtErr))
        ret = -2
        return ret

    #logger.log(logging.DEBUG, "Sent data: [%s]" % (dmtRget.url))
    #logger.log(logging.DEBUG, "dmtGetSwitch(idx=%d <=> %s) server response :\n%s" % (idx, name, dmtRget.text))

    # Check var/idx coherence to validate domoticz response
    jsonRget = json.loads(dmtRget.text)
    if jsonRget["result"][0]["Name"] != name:
        logger.log(logging.INFO, "dmtGetSwitch(idx=%d <=> %s != %s) MISMATCH" % (idx, name, jsonRget["result"][0]["Name"]))
        return sys.maxint

    ret = jsonRget["result"][0]["Data"]
    #logger.log(logging.DEBUG, "dmtJsonGetSwitch(%s) = %s" % (name, ret))

    # Looks OK, return domoticz switch data...
    return ret
With a few imports (logging/json/requests...) this may be called like this from some code that already setup logger after setting right IDX/SWITCH_NAME (coherency checked by get):

Code: Select all

domoticzJurl='http://127.0.0.1:8080/json.htm?'

print "switch IDX/SWITCH_NAME status:"
status = dmtGetSwitch(domoticzJurl, IDX, 'SWITCH_NAME', logger)
print status
Thanks for the suggestion, I did not noticed this one when I 1st checked for something appropriate!
User avatar
kiddigital
Posts: 438
Joined: Thursday 10 August 2017 6:52
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: JSON API addition

Post by kiddigital »

Don't forget to check the 'status' field in the JSON payload. It should state 'OK' to confirm that everything went ok. (So field '["status"]' in your code)

Maybe also even check that you received a valid HTTP response 200 code and not something else.

These checks should be done to see if the request/response went well. Than you can check for '["result"][0]["Name"]' :)
One RPi with Domoticz, RFX433e, aeon labs z-wave plus stick GEN5, ha-bridge 5.4.0 for Alexa, Philips Hue Bridge, Pimoroni Automation Hat
One RPi with Pi foundation standard touch screen to display Dashticz
lost
Posts: 664
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: JSON API addition

Post by lost »

kiddigital wrote: Wednesday 14 October 2020 21:55 Don't forget to check the 'status' field in the JSON payload. It should state 'OK' to confirm that everything went ok. (So field '["status"]' in your code)

Maybe also even check that you received a valid HTTP response 200 code and not something else.

These checks should be done to see if the request/response went well. Than you can check for '["result"][0]["Name"]' :)
OK, I'll add these checks after pure connection issue errors and before name/idx coherence check (that was more targeted for a dummy read at services using these functions init, to check for obvious errors in the yaml configuration file)... In this function and others using the JSON API as well. Some verify security status, read/write user variables linked to alarm system... Better being reliable!

For now, when connection was OK (domoticz http server sometimes hanged in the past with domoticz core still running so no systemd auto restart: This can last for long) I did not noticed issues with HTTP API or external signs of misbehaving. But future may show others issues.
Hirthe
Posts: 1
Joined: Monday 14 December 2020 10:19
Target OS: Windows
Domoticz version:
Contact:

Re: JSON API addition

Post by Hirthe »

Hi, someone can tell me how can i get a JSON from a subreddit?

I made it, its working with /top.json

But can someone tell me, how can i use it with like all.json or something?

i used &limit=100, but this is the maximum. i want more than 100






mygroundbiz
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest