JSON in a CustomEvent JSON URL  [Solved]

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

Moderator: leecollings

Post Reply
peterbos
Posts: 93
Joined: Saturday 07 November 2020 21:41
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

JSON in a CustomEvent JSON URL

Post by peterbos »

Hi,

I want to send JSON data in a custom event JSON URL as an easy way of sending data from a Windows program to Domoticz. One of the several URLs I tried was

Code: Select all

http://192.168.1.162:8080/json.htm?type=command&param=customevent&event=testTrigger&data={"field1":"This is field 1","field2":"This is field 2"}
I made a simple script to recive the data:

Code: Select all

return {
	on = {
		customEvents = {
			'testTrigger' -- event triggered by emitEvent
		}
	},
	data = {},
	logging = {},
	execute = function(dz, triggeredItem)
		if (triggeredItem.isCustomEvent) then
		    if (triggeredItem.isJSON) then
		        local myData = triggeredItem.json
			dz.log('Test succeeded, data = ' .. myData.field1 , dz.LOG_ERROR)
		    end
		end
	end
}
I also tried it with "", '', extra {} and/or [] but I keep getting error messages of all kind. What is the right way to pass a JSON with this custom event JSON?

Peter
peterbos
Posts: 93
Joined: Saturday 07 November 2020 21:41
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: JSON in a CustomEvent JSON URL  [Solved]

Post by peterbos »

Hi,

I solved it.

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

Re: JSON in a CustomEvent JSON URL

Post by waaren »

peterbos wrote: Thursday 31 December 2020 0:35 I want to send JSON data in a custom event JSON URL as an easy way of sending data from a Windows program to Domoticz.
[Edit] Good that you solved it. For other forum members facing the same..

Below curl and script should do it if you allowed your local network to access domoticz without password.

[Setup][Settings][System]

Local Networks (no username/password):
Networks:
127.0.0.*;192.168.1.*;::1

Code: Select all

curl -d '{"field1":"This is field 1","field2":"This is field 2"}' 'http://192.168.1.162:8080/json.htm?type=command&param=customevent&event=testTrigger'

Code: Select all

return 
{
    on = 
    {
        customEvents = 
        {
            'testTrigger',
        },
    },
    
    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = ' testCiustomEvent',
    },
    
    execute = function(dz, item)
        if item.isJSON then
            dz.log('Test succeeded, json received = ' , dz.LOG_FORCE)
            dz.utils.dumpTable(item.json)
        else
            dz.log('Test partial succeeded, item = ', dz.LOG_FORCE)
            dz.utils.dumpTable(item)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
peterbos
Posts: 93
Joined: Saturday 07 November 2020 21:41
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: JSON in a CustomEvent JSON URL

Post by peterbos »

Hi Waaren,
Maybe your solution works but I can't trigger the custom event from a browser with the simple command. I found out that the problem was not in the URL but in the script. The check if the data is a JSON fails but the data is treated as a JSON correctly.

This script works but only without either of the two commands I commented out:

Code: Select all

return {
	on = {
		customEvents = {
			'testTrigger' -- event triggered by emitEvent
		}
	},
	data = {},
	logging = {},
	execute = function(dz, triggeredItem)
		--if (triggeredItem.isJSON) then        <--- doesn't work
		--if (triggeredItem.data.isJSON) then      <--- doesn't work also
		    dz.log(triggeredItem.data , dz.LOG_ERROR)
		    dz.log('Test succeeded, data = ' .. triggeredItem.data.field1 , dz.LOG_ERROR)
		--end
	end
}
Peter
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: JSON in a CustomEvent JSON URL

Post by waaren »

peterbos wrote: Friday 01 January 2021 17:27 Hi Waaren,
Maybe your solution works but I can't trigger the custom event from a browser with the simple command. I found out that the problem was not in the URL but in the script. The check if the data is a JSON fails but the data is treated as a JSON correctly.
I cannot replicate your issue so I can only guess you are on a version before build 12019
In this version I made the check for valid JSON strings a bit more forgiving. According the standard a JSON string cannot start with a curly bracket but because the Lua JSON decoder does allow it, I changed dzVents behavior in versions > 12019 to accept them as valid JSON

What do you see when you repeat my tests?

Code: Select all

--[[

    (my test system runs on a system named nuc and I use port 8084 for my test / development domotcicz)

    test in Browser window
    http://nuc:8084/json.htm?type=command&param=customevent&event=testTrigger&data={"field1":"This is field 1","field2":"This is field 2"}

2021-01-01 18:30:39.902  Status: dzVents: Info:  testCustomEvent: ------ Start internal script: dz 20201231 customSensor: Custom event: "testTrigger"
2021-01-01 18:30:39.903  Status: dzVents: Info:  testCustomEvent: dzVents version: 3.1.0
2021-01-01 18:30:39.904  Status: dzVents: Info:  testCustomEvent: domoticz version: 2020.2 (build 12803)
2021-01-01 18:30:39.904  Status: dzVents: Info:  testCustomEvent: isJSON: true ; ==>> {["field2"]="This is field 2", ["field1"]="This is field 1"}
2021-01-01 18:30:39.904  Status: dzVents: Info:  testCustomEvent: data ==>> {"field1":"This is field 1","field2":"This is field 2"}
2021-01-01 18:30:39.904  Status: dzVents: Info:  testCustomEvent: data.field1 =>> nil
2021-01-01 18:30:39.906  Status: dzVents: Info:  testCustomEvent: ------ Finished dz 20201231 customSensor

    test from commandLine
    curl -d '{"field1":"This is field 1","field2":"This is field 2"}' 'nuc:8084/json.htm?type=command&param=customevent&event=testTrigger'

2021-01-01 18:32:59.834  Status: dzVents: Info:  testCustomEvent: ------ Start internal script: dz 20201231 customSensor: Custom event: "testTrigger"
2021-01-01 18:32:59.836  Status: dzVents: Info:  testCustomEvent: dzVents version: 3.1.0
2021-01-01 18:32:59.836  Status: dzVents: Info:  testCustomEvent: domoticz version: 2020.2 (build 12803)
2021-01-01 18:32:59.836  Status: dzVents: Info:  testCustomEvent: isJSON: true ; ==>> {["field1"]="This is field 1", ["field2"]="This is field 2"}
2021-01-01 18:32:59.836  Status: dzVents: Info:  testCustomEvent: data ==>> {"field1":"This is field 1","field2":"This is field 2"}
2021-01-01 18:32:59.836  Status: dzVents: Info:  testCustomEvent: data.field1 =>> nil
2021-01-01 18:32:59.838  Status: dzVents: Info:  testCustomEvent: ------ Finished dz 20201231 customSensor

]]--


return
{
    on =
    {
        customEvents =
        {
            'testTrigger',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = ' testCustomEvent',
    },

    execute = function(dz, item)
        dz.log('dzVents version: ' .. dz.settings.dzVentsVersion)
        dz.log('domoticz version: ' .. dz.settings.domoticzVersion)
        if (item.isJSON) then
                dz.log('isJSON: true ; ==>> '.. dz.utils.toStr(item.json) )
        end
        dz.log('data ==>> ' .. dz.utils.toStr(item.data) )
        dz.log('data.field1 =>> ' .. dz.utils.toStr(item.data.field1) )
    end
}


Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
peterbos
Posts: 93
Joined: Saturday 07 November 2020 21:41
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: JSON in a CustomEvent JSON URL

Post by peterbos »

Hi Waaren,

I have Domoticz 2020.2 with dzvents 3.0.2. The difference between our approaches is that I send the data as part of the URL and you use two lines from a commandLine (which?) in which the data is not part of the URL.

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

Re: JSON in a CustomEvent JSON URL

Post by waaren »

peterbos wrote: Friday 01 January 2021 23:04 Hi Waaren,

I have Domoticz 2020.2 with dzvents 3.0.2. The difference between our approaches is that I send the data as part of the URL and you use two lines from a commandLine (which?) in which the data is not part of the URL.
If you look closely to my last post you will see that I tested two different methods. One from the browser address bar with data as part of the url and one using curl with data separated from the url (command line Debian) but this will not work for your version either because it is using post and that was not available for dzVents 3.0.2

If you intend to keep using the stable version with dzVents 3.0.2, you will have to keep using item.data when sending from a browser. If you update to the beta channel you will have the fixed version (and a lot more)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
peterbos
Posts: 93
Joined: Saturday 07 November 2020 21:41
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: JSON in a CustomEvent JSON URL

Post by peterbos »

Hi Waaren,
I downloaded Domoticz and dzVents only two months ago, so it surprised me that I have an old version of dzVents. I see that the current stable version is 3.1.0. Is that the version you're pointing to? I prefer not to use a beta version.

I used the Check for updates button in Domoticz but get the message No updates available. How do I install dzVents 3.1.0?

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

Re: JSON in a CustomEvent JSON URL

Post by waaren »

peterbos wrote: Sunday 03 January 2021 0:39 I used the Check for updates button in Domoticz but get the message No updates available. How do I install dzVents 3.1.0?
You downloaded the release (aka) stable version. Hotfixing and development of domoticz takes place in the Beta channel. Releases are not hotfixed. If you want the latest version you nee to switch to Beta.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest