JSON request syntax in Domoticz - [Solved]  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
User avatar
rudder66
Posts: 12
Joined: Saturday 07 December 2019 20:21
Target OS: Linux
Domoticz version: 4.11553
Location: Minsk, Belarus
Contact:

JSON request syntax in Domoticz - [Solved]

Post by rudder66 »

Dear friends,

Help deal with JSON in dzvents.

Through the CURL system utility, everything works fine.

Code: Select all

curl -H "Content-Type: application/json" http://192.168.100.32:8081/zeroconf/switch -X POST --data '{"deviceid":"10008fce77","data":{"switch": "off"}}'
Through dzvents - does not work.

Code: Select all

return {
	on = {
		timer = { 'every minute' }
	},
	execute = function(domoticz, timer)
	domoticz.openURL({
    url = 'http://192.168.100.32:8081/zeroconf/switch',
    method = 'POST',
    postData = {
        deviceid = '10008fce77', 
        data = '{"switch": "off"}'}
    })
    end
    }
Debug log

Code: Select all

2019-12-07 21:59:00.273 Status: dzVents: Info: ------ Start internal script: POST:, trigger: every minute
2019-12-07 21:59:00.274 Status: dzVents: Debug: OpenURL: url = http://192.168.100.32:8081/zeroconf/switch
2019-12-07 21:59:00.274 Status: dzVents: Debug: OpenURL: method = POST
2019-12-07 21:59:00.274 Status: dzVents: Debug: OpenURL: post data = {"data":"{\"switch\": \"off\"}","deviceid":"10008fce77"}
2019-12-07 21:59:00.274 Status: dzVents: Debug: OpenURL: headers = {["Content-Type"]="application/json"}
2019-12-07 21:59:00.274 Status: dzVents: Debug: OpenURL: callback = nil
2019-12-07 21:59:00.274 Status: dzVents: Info: ------ Finished POST
2019-12-07 21:59:00.274 Status: dzVents: Debug: Commands sent to Domoticz:
2019-12-07 21:59:00.274 Status: dzVents: Debug: - OpenURL = {["method"]="POST", ["postdata"]="{"data":"{\"switch\": \"off\"}","deviceid":"10008fce77"}", ["URL"]="http://192.168.100.32:8081/zeroconf/switch", ["headers"]={["Content-Type"]="application/json"}}
2019-12-07 21:59:00.274 Status: dzVents: Debug: =====================================================
I use this API protocol to control my SONOFF DIY devices in Domoticz
https://github.com/itead/Sonoff_Devices ... %20v1.4.md
What am I doing wrong, where is the mistake? :(
Thanks
Last edited by rudder66 on Monday 09 December 2019 11:16, edited 2 times in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: JSON request syntax in Domoticz

Post by waaren »

rudder66 wrote: Saturday 07 December 2019 20:30 Help deal with JSON in dzvents.
What am I doing wrong, where is the mistake? :(
Thanks
Can you try this ?

Code: Select all

return 
{

    on = 
    { 
        timer = { 'every minute' } 
    },
	
	logging = 
	{ 
	    level = domoticz.LOG_DEBUG
    },
	
	execute = function(dz)
	    _G.logMarker =  _G.moduleLabel
   
    	dz.openURL(
    	{
            url = 'http://192.168.192.51:8081/zeroconf/switch',
            method = 'POST',
            postData = 
            {
                deviceid = '10008fce77', 
                data = 
                { 
                    switch = 'off', 
                }
            }
        })

    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
rudder66
Posts: 12
Joined: Saturday 07 December 2019 20:21
Target OS: Linux
Domoticz version: 4.11553
Location: Minsk, Belarus
Contact:

Re: JSON request syntax in Domoticz

Post by rudder66 »

waaren wrote: Saturday 07 December 2019 21:31 Can you try this ?
Great, it works! :!:
How can I send this to the topic .... / zeroconf / info:

Code: Select all

{ 
	"deviceid": "100000140e", 
	"data": { } 
 }
If I write like this:
Spoiler: show

Code: Select all

return 
{

    on = 
    { 
        timer = { 'every minute' } 
    },
	
	logging = 
	{ 
	    level = domoticz.LOG_DEBUG
    },
	
	execute = function(dz)
	    _G.logMarker =  _G.moduleLabel
   
    	dz.openURL(
    	{
            url = 'http://192.168.100.32:8081/zeroconf/info',
            method = 'POST',
            postData = 
            {
                deviceid = '10008fce77', 
                data = 
                { 
                     
                }
            }
        })

    end
}
Then it sends me square brackets []:

Code: Select all

2019-12-08 10:02:00.429 Status: dzVents: Info: ------ Start internal script: POST STATUS:, trigger: every minute
2019-12-08 10:02:00.431 Status: dzVents: Debug: POST STATUS: OpenURL: url = http://192.168.100.32:8081/zeroconf/info
2019-12-08 10:02:00.431 Status: dzVents: Debug: POST STATUS: OpenURL: method = POST
2019-12-08 10:02:00.431 Status: dzVents: Debug: POST STATUS: OpenURL: post data = {"data":[],"deviceid":"10008fce77"}
2019-12-08 10:02:00.431 Status: dzVents: Debug: POST STATUS: OpenURL: headers = {["Content-Type"]="application/json"}
2019-12-08 10:02:00.431 Status: dzVents: Debug: POST STATUS: OpenURL: callback = nil
2019-12-08 10:02:00.431 Status: dzVents: Info: POST STATUS: ------ Finished POST STATUS

Ultimately I want get a response of status

Code: Select all

{ 
	"seq": 2, 
	"error": 0,
	"data": {
	"switch": "on",
	"startup": "stay",
	"pulse": "on",
	"pulseWidth": 2000,
	"ssid": "eWeLink",
	"otaUnlock": true
	}
 }
and use the "switch": "on" data to track the actual SONOFF relay status every minute.

Thanks
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: JSON request syntax in Domoticz

Post by waaren »

rudder66 wrote: Sunday 08 December 2019 8:10 How can I send this to the topic .... / zeroconf / info:

Code: Select all

{ 
	"deviceid": "100000140e", 
	"data": { } 
 }
I don't think this is possible. Can't you leave out data completely ?
What would it look like when using curl ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
rudder66
Posts: 12
Joined: Saturday 07 December 2019 20:21
Target OS: Linux
Domoticz version: 4.11553
Location: Minsk, Belarus
Contact:

Re: JSON request syntax in Domoticz

Post by rudder66 »

waaren wrote: Sunday 08 December 2019 10:14 I don't think this is possible. Can't you leave out data completely ?
What would it look like when using curl ?
see below
Снимок экрана от 2019-12-08 14-29-45.png
Снимок экрана от 2019-12-08 14-29-45.png (83.36 KiB) Viewed 2334 times
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: JSON request syntax in Domoticz

Post by waaren »

rudder66 wrote: Sunday 08 December 2019 12:32 see below
You could try this (by making the postdata a string, dzVents will not translate it into a json)

Code: Select all

return 
{

    on = 
    { 
        timer = { 'every minute' } 
    },
    
    logging = 
    { 
        level = domoticz.LOG_DEBUG
    },
    
    execute = function(dz)
        _G.logMarker =  _G.moduleLabel

        dz.openURL(
        {
            url = 'http://192.168.100.32:8081/zeroconf/switch',
            method = 'POST',
            postData = ' { "deviceid" : "10008fce77", "data": {} }'
        })

    end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
rudder66
Posts: 12
Joined: Saturday 07 December 2019 20:21
Target OS: Linux
Domoticz version: 4.11553
Location: Minsk, Belarus
Contact:

Re: JSON request syntax in Domoticz  [Solved]

Post by rudder66 »

Working!
Many thanks! ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest