24.07.2025, 09:02
(22.02.2020, 09:29)arnocl Wrote: I cleaned up your code a little to be tailored more to my needs. Also included a status object checker, convenient to get a feedback of the actual settings of the TV.
Common Functions:
Code:function ipControlSonyTV(service, command) local http = require("socket.http") local ltn12 = require("ltn12") require("json") local ip = "192.168.1.135" local response_body = { } local res, code, response_headers, status = http.request { url = "http://" .. ip .. "/sony/" .. service, method = "POST", headers = { ["HOST"] = ip, ["Accept"] = "*/*", ["Content-Type"] = "application/x-www-form-urlencoded", ["X-Auth-PSK"] = "************", ["Content-Length"] = command:len() }, source = ltn12.source.string(command), sink = ltn12.sink.table(response_body) } response = json.decode(response_body[1]) return response["result"][1] end
Resident Script (every 2 seconds or as much as needed):
Code:command = [[ {"method": "getPowerStatus","id": 50,"params": [],"version": "1.0"} ]] powerStatus = ipControlSonyTV("system", command) powerStatus = powerStatus["status"] if (powerStatus == "active") then grp.checkwrite('5/0/2', 1, 1) elseif (powerStatus == "standby") then grp.checkwrite('5/0/2', 0, 1) end if (powerStatus == "active") then command = [[ {"method": "getVolumeInformation", "id": 33, "params": [], "version": "1.0"} ]] volumeInfo = ipControlSonyTV("audio", command) volumeStatus = volumeInfo[1]["volume"] muteStatus = volumeInfo[1]["mute"] grp.checkwrite('5/0/5', volumeStatus, 1) grp.checkwrite('5/0/7', muteStatus, 1) command = [[ {"method": "getPlayingContentInfo","id": 103,"params": [],"version": "1.0"} ]] sourceStatus = ipControlSonyTV("avContent", command) sourceStatus = sourceStatus["uri"] sourceStatus = tonumber(string.sub(sourceStatus, -1)) grp.checkwrite('5/0/9', sourceStatus, 1) end
Every control object has it's own event script
Power:
Code:power = event.getvalue() if (power) then command = [[ {"method": "setPowerStatus","id": 55,"params": [{"status": true}],"version": "1.0"} ]] else command = [[ {"method": "setPowerStatus","id": 55,"params": [{"status": false}],"version": "1.0"} ]] end ipControlSonyTV("system", command)
Volume Step:
Code:volumeDown = event.getvalue() -- down = 1 | up = 0 if (volumeDown) then command = [[ {"method": "setAudioVolume","id": 98,"params": [{"volume": "-1","ui": "on","target": "speaker"}],"version": "1.2"} ]] else command = [[ {"method": "setAudioVolume","id": 98,"params": [{"volume": "+1","ui": "on","target": "speaker"}],"version": "1.2"} ]] end ipControlSonyTV("audio", command)
Volume:
Code:volume = event.getvalue() command = [[ {"method": "setAudioVolume","id": 98,"params": [{"volume": "]] .. volume .. [[","ui": "on","target": "speaker"}],"version": "1.2"} ]] ipControlSonyTV("audio", command)
Mute:
Code:mute = event.getvalue() if (mute) then command = [[ {"method": "setAudioMute","id": 601,"params": [{"status": true}],"version": "1.0"} ]] else command = [[ {"method": "setAudioMute","id": 601,"params": [{"status": false}],"version": "1.0"} ]] end ipControlSonyTV("audio", command)
Source Select:
Code:source = event.getvalue() power = grp.getvalue('5/0/2') if (not power) then grp.write('5/0/1', 1) os.sleep(1) end command = [[ {"method": "setPlayContent","id": 101,"params": [{"uri": "extInput:hdmi?port=]] .. source .. [["}],"version": "1.0"} ]] ipControlSonyTV("avContent", command)
Hi guys,
i try this Scripts and got error
Code:
Common functions:144: bad argument #1 to 'decode' (string expected, got nil)
stack traceback:
[C]: in function 'decode'
Common functions:144: in function 'ipControlSonyTV'I dont know much about scripting.
Just a little less than basics.