waaren wrote:jake wrote: ↑Sunday 30 August 2020 23:54
When I flip my testButton, the response in de domoticz log is: attempt to index a nil value (field 'result')
What do you see with this code snippet?
Code: Select all
if (device == testButton) then
domoticz.openURL({
url = 'http://user:[email protected]:8080/jsonrpc?request={jsonrpc:2.0,method:XBMC.GetInfoBooleans,params:{booleans:[Player.HasAudio]},id:1}',
method = 'GET',
callback = 'kodiIdleTimer'
})
elseif (device.isHTTPResponse and device.ok) then
-- we know it is json but dzVents cannot detect this
-- convert to Lua
local json = domoticz.utils.fromJSON(device.data)
-- json is now a Lua table
domoticz.log(device.data,domoticz.LOG_FORCE)
domoticz.utils.dumpTable(json)
print("The result true or false from the request is ".. json[1].result['Player.HasAudio'])
elseif (device.isHTTPResponse and device.ok == false) then
print("Response was not OK)
end
The force log learned me stuff:
Code: Select all
2020-08-31 18:14:57.112 Status: dzVents: !Info: {"error":{"code":-32700,"message":"Parse error."},"id":null,"jsonrpc":"2.0"}
2020-08-31 18:14:57.112 Status: dzVents: > jsonrpc: 2.0
2020-08-31 18:14:57.112 Status: dzVents: > error:
2020-08-31 18:14:57.112 Status: dzVents: > code: -32700
2020-08-31 18:14:57.112 Status: dzVents: > message: Parse error.
The result showed id:"null", while in the browser it is displayed as id: 1
This triggered me to put the " " back in the url: {"jsonrpc":"2.0","method":"XBMC.GetInfoBooleans","params":{"booleans":["Player.HasAudio"]},"id":1}
This improved the results in the log:
Code: Select all
2020-08-31 18:56:51.571 Status: dzVents: !Info: {"id":1,"jsonrpc":"2.0","result":{"Player.HasAudio":false}}
2020-08-31 18:56:51.571 Status: dzVents: > jsonrpc: 2.0
2020-08-31 18:56:51.571 Status: dzVents: > id: 1
2020-08-31 18:56:51.571 Status: dzVents: > result:
2020-08-31 18:56:51.571 Status: dzVents: > Player.HasAudio: false
2020-08-31 18:56:51.980 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-08-31 18:56:51.571 Error: dzVents: Error: (2.5.7) An error occurred when calling event handler kodi3
2020-08-31 18:56:51.571 Error: dzVents: Error: (2.5.7) /home/pi/domoticz/scripts/dzVents/scripts/kodi3.lua:192: attempt to index a nil value (field '?')
Line 192 is:
Code: Select all
print("The result true or false from the request is ".. json[1].result['Player.HasAudio'])
UPDATE: well, well, I finally got it: I first 'tried' to change line 192 to json.result['Player.HasAudio']. This gave me the clue in the log:
Code: Select all
Error: (2.5.7) /home/pi/domoticz/scripts/dzVents/scripts/kodi3.lua:192: attempt to concatenate a boolean value (field 'Player.HasAudio
So i tried a different line of code on line 192 and finally with success:
if json.result['Player.HasAudio'] == false then print ("No audio is playing") end:
Code: Select all
2020-08-31 19:14:54.734 Status: dzVents: No audio is playing
2020-08-31 19:14:54.734 Status: dzVents: !Info: {"id":1,"jsonrpc":"2.0","result":{"Player.HasAudio":false}}
2020-08-31 19:14:54.734 Status: dzVents: > jsonrpc: 2.0
2020-08-31 19:14:54.734 Status: dzVents: > id: 1
2020-08-31 19:14:54.734 Status: dzVents: > result:
2020-08-31 19:14:54.734 Status: dzVents: > Player.HasAudio: false
Thanks for pointing me in the right direction
Although I can now continue my script, I would like to know out of curiosity how I could still show the 'false' in a log line, instead of domoticz failing with 'attempt to concatenate a boolean value'