I want to share with you my LUA scripts for turning on/off a Sony Bravia TV. I created and tested this with a KDL-50W815B model, but i'm sure this will work with lots of other network connected Sony Bravia models.
My solution consists of two scripts. The first one is a time script that updates the status in Domoticz when the TV was turned on or off via another remote. The second script is a device script that can actually turn the television on and off.
The scripts are made for Domoticz on the Raspberry Pi, but may also work on other Linux/Unix distros.
Prerequisites:
- Create a virtual switch (On/Off) named "TV"
- Create a user variable (integer) named "TVOnlyStatusUpdate", with value 0
- Install etherwake (sudo apt-get install etherwake)
- Enable remote start on your TV: [Settings] → [Network] → [Home Network Setup] → [Remote Start] → [On]
- Enable pre-shared key on your TV: [Settings] → [Network] → [Home Network Setup] → [IP Control] → [Authentication] → [Normal and Pre-Shared Key]
- Set pre-shared key on your TV: [Settings] → [Network] → [Home Network Setup] → [IP Control] → [Pre-Shared Key] → [sony]
- Give your TV a static IP address, or make a DHCP reservation for a specific IP address in your router.
- Determine the MAC address of your TV: [Settings] → [Network] → [Network Setup] → [View Network Status]
What you need to adjust in the scripts:
- In script 2, set the MAC address of your TV. Update the following to match the MAC address: 'etherwake ab:12:34:5678'.
- In both scripts set the url to match the IP address of your TV.
- I used 'sony' as pre-shared key in this example, but you can define your own pre-shared key. When you do so, make sure to update this in both scripts. (headervalue = 'sony')
Code: Select all
function getPowerStatus()
local url = 'http://10.101.0.50/sony/system'
local headername = 'X-Auth-PSK'
local headervalue = 'sony'
local jsonbodygetstatus = '{\\"id\\":2,\\"method\\":\\"getPowerStatus\\",\\"version\\":\\"1.0\\",\\"params\\":[]}'
local runcommand = 'curl -v -H \"Content-Type:application/json\" -H \"' .. headername .. ':' .. headervalue .. '\" -d \"' .. jsonbodygetstatus .. '\" ' .. url .. ''
local h=io.popen(runcommand)
local response=h:read("*a")
h:close()
if string.find(response, '{"status":"active"}') then
return 'active'
elseif string.find(response, '{"status":"standby"}') then
return 'standby'
else
return 'unkown'
end
end
commandArray = {}
ping_success=os.execute('ping -c1 10.101.0.50')
if (ping_success) then
print('TV ping success')
powerstatus=getPowerStatus()
print('TV says it is ' .. powerstatus)
if(powerstatus == 'active') then
if(otherdevices['TV'] == 'Off') then
print('TV active, but status is OFF, so update to ON.')
commandArray['Variable:TVOnlyStatusUpdate'] = '1'
commandArray['TV'] = 'On'
else
print('TV active, status is ON, so do nothing.')
end
elseif(powerstatus == 'standby') then
if(otherdevices['TV'] == 'On') then
print('TV standby, but status is ON, so update to OFF.')
commandArray['Variable:TVOnlyStatusUpdate'] = '1'
commandArray['TV'] = 'Off'
else
print('TV standby, status is OFF, so do nothing.')
end
else
print('TV in unknown power status, doing nothing...')
end
else
print("TV ping fail")
if(otherdevices['TV'] == 'On') then
print('TV current status here is ON, so update status to OFF.')
commandArray['Variable:TVOnlyStatusUpdate'] = '1'
commandArray['TV']='Off'
else
print('TV current status here is OFF, so do nothing.')
end
end
return commandArray
Code: Select all
commandArray = {}
if(devicechanged['TV']) then
url = 'http://10.101.0.50/sony/system'
headername = 'X-Auth-PSK'
headervalue = 'sony'
jsonbodypoweroff = '{\\"id\\":2,\\"method\\":\\"setPowerStatus\\",\\"version\\":\\"1.0\\",\\"params\\":[{ \\"status\\" : false}]}'
if (devicechanged['TV'] == 'On') then
if(uservariables['TVOnlyStatusUpdate'] == 1) then
print('TV status update received (ON), resetting user variable.')
commandArray['Variable:TVOnlyStatusUpdate'] = '0'
else
os.execute('etherwake ab:12:34:56:cd:78')
print('TV got wake up call')
end
end
if (devicechanged['TV'] == 'Off') then
if(uservariables['TVOnlyStatusUpdate'] == 1) then
print('TV status update received (OFF), resetting user variable.')
commandArray['Variable:TVOnlyStatusUpdate'] = '0'
else
runcommand = 'curl -v -H \"Content-Type:application/json\" -H \"' .. headername .. ':' .. headervalue .. '\" -d \"' .. jsonbodypoweroff .. '\" ' .. url .. ''
response = os.execute(runcommand)
if response then
print('TV goes sleepy sleep')
else
print('TV not in the mood for sleepy sleep')
end
end
end
end
return commandArray