--- Yamonos 1.0 beta ---
-- Automatically turns on a Yamaha Receiver (and switches it to the appropriate input) when a Sonos Connect starts playing.
-- It requires a Yamaha Virtual Device that stores the correct power state and input in 2 separate Global Variables
-- Known issue: If there are multiple grouped devices, Sonos will keep setting the status to "Playing", even if the devices are paused.
-- Feel free to improve it and share it
-- adjust the variables section below
--[[
%% 526 properties
%% 525 properties
%% 359 properties
%% autostart
%% events
%% globals
--]]
local YamahaAVR = 359 -- Yamaha VD ID
local sonos = 526 -- Sonos Connect ID
local pwrBtn = 1 -- the Yamaha VD button ID to turn on the device
local offBtn = 2 -- the Yamaha VD button ID to turn off the device
local srcBtn = 8 -- the Yamaha VD source button ID
local sleepTime = 5000 -- how often should Fibaro check the AV status - in miliseconds
local globalVarName = "YAMAHA" -- global variable name created for Yamaha VD
local globalVarInputName = "YAMAHA_SET" -- global variable name for Yamaha VD to set the input
local avInput = "AV2" -- The Global variable set by the Yamaha VD
local globalVarValueOn = "ON" -- the value of the global variable when ON, set by Yamaha VD
local globalVarValueOff = "Standby" -- the value of the global variable when OFF, set by Yamaha VD
-------- no input is necessary from here ---------------
local executed = false
function turnOnYamaha()
fibaro:call(YamahaAVR, "pressButton",pwrBtn) --POWER ON
fibaro:debug("yamaha on")
fibaro:sleep(2000) --wait 2 sec until it turns on
fibaro:debug("yamaha av2")
fibaro:call(YamahaAVR, "pressButton",srcBtn) --AV2
executed = true
end
function turnOffYamaha()
fibaro:call(YamahaAVR, "pressButton",offBtn) --POWER OFF
fibaro:debug("yamaha off")
end
while true do
local sonosPlaying = fibaro:getValue(sonos, "state")
local yamahaStatus = fibaro:getGlobalValue(globalVarName)
if yamahaStatus == globalVarValueOff and sonosPlaying == "PLAYING" then
fibaro:debug("Sonos Playing, Yamaha Off - Turning On Yamaha")
if not executed then
turnOnYamaha()
end
executed = false
sonosPlaying = fibaro:getValue(sonos, "state")
yamahaStatus = fibaro:getGlobalValue(globalVarName)
elseif yamahaStatus == globalVarValueOn and sonosPlaying == "PLAYING" then
fibaro:debug("Sonos Playing, Yamaha On - Nothing here for me")
fibaro:debug(sonosPlaying)
sonosPlaying = fibaro:getValue(sonos, "state")
yamahaStatus = fibaro:getGlobalValue(globalVarName)
elseif sonosPlaying == "PAUSED_PLAYBACK" and yamahaStatus == globalVarValueOff then
fibaro:debug(yamahaStatus)
fibaro:debug("Sonos Off, Yamaha Off - nothing to do")
sonosPlaying = fibaro:getValue(sonos, "state")
yamahaStatus = fibaro:getGlobalValue(globalVarName)
elseif yamahaStatus == globalVarValueOn and sonosPlaying == "PAUSED_PLAYBACK" then
fibaro:debug("Sonos Off, Yamaha On - Turning Off Yamaha")
local otherInput = fibaro:getGlobalValue(globalVarInputName)
if otherInput ~= (avInput) then
fibaro:debug("Yamaha is on another input, do not touch")
else
turnOffYamaha()
end
sonosPlaying = fibaro:getValue(sonos, "state")
yamahaStatus = fibaro:getGlobalValue(globalVarName)
end
fibaro:sleep(sleepTime)
end
Hey Folks,
I wrote this small script to help me out with my Sonos Connect and Yamaha AV Receiver.
It turns on the Yamaha AV and switches to the correct input as soon as Sonos Connect is playing.
It's not perfect, but it works pretty well. Please feel free to improve it, if it's needed.
/Seb