Yamaha Radio CRX N560D control script

Moderator: leecollings

Post Reply
marcovz
Posts: 5
Joined: Tuesday 10 November 2020 20:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Yamaha Radio CRX N560D control script

Post by marcovz »

I have made a script to control my Yamaha CRX N560D radio which might be useful for others. This radio does not feature the Musiccast protocol but can be controlled over ethernet by http post. For FHEM a Perl module is available with all the commands and status requests. https://github.com/mhop/fhem-mirror/blo ... MAHA_NP.pm. Other models that might work are NP-S2000, CD-N500, CD-N301, R-N500, R-N301. The radio can be controlled by Yahama Network Controller app for IOS and Android.

I only used the commands for the functions I want to control in Domoticz. Power On/Off, select inputs Dig1 and Tuner/DAB, Volume control and select DAB presets. For the basic commands I use a selector switch (idx 678) and another selector switch for the DAB presets (idx 693). The status of the radio is requested every minute by a timer. Power, Volume, Input and preset info are stored in user variables and send to a text sensor (idx 685). The name of levels in the selector switch (678) need to match the name in the script. For the preset selector the first 2 characters are used for the preset number, character 5 and further for the station name and can be set according your preset list. Furthermore I set different volume levels when switching inputs to match the source.

Create variables for:
inputname = domoticz.variables(1).value
volume = domoticz.variables(2).value
presetname = domoticz.variables(4).value
preset = domoticz.variables(6).value
presetinfo = domoticz.variables(7).value
power = domoticz.variables(8).value

and change the variables numbers in the script accordingly. Create 2 (dummy) selector switches

Switch to control with following levels:
- Off
- TV
- DAB
- Mute
- Vol-
- Vol+

Switch for DAB preset levelnames starting with preset number:
- 01
- 02
- 03
and so on. The rest of the levelname can be used for station name is and is discarded in the script.

Create a text sensor which is used as display and change the idx (685 in the code) to your idx numer.

Create a new script in DZvents with following code and change your idx numbers and ip adres of your Yamaha:

Code: Select all


return {
	on = {
		devices = {
			678, 693
		},
		timer = { 'every minute' },
        httpResponses = { 'statusresponse', 'statusplayinfo' }
		    
	},
	logging = {
    level = domoticz.LOG_DEBUG,
    marker = "yamaha_script"
    },
	
	    execute = function(domoticz, device)
	        inputname = domoticz.variables(1).value
	        volume = domoticz.variables(2).value
	        presetname = domoticz.variables(4).value
	        preset = domoticz.variables(6).value
	        presetinfo = domoticz.variables(7).value
	        power = domoticz.variables(8).value
	        
	        yamahaip = '192.168.20.20/YamahaRemoteControl/ctrl'
	        head = {['Content-Type'] = 'application/xml; charset=UTF-8'}
	        
	        xml_get_status = [[<YAMAHA_AV cmd="GET"><System><Basic_Status>GetParam</Basic_Status></System></YAMAHA_AV>]]
	        xml_get_playinfo = [[<YAMAHA_AV cmd="GET"><Tuner><Play_Info>GetParam</Play_Info></Tuner></YAMAHA_AV>]]
	        
	        if device.idx == 678 then
	            xml_put_power_on = [[<YAMAHA_AV cmd="PUT"><System><Power_Control><Power>On</Power></Power_Control></System></YAMAHA_AV>]]
	            xml_put_power_standby = [[<YAMAHA_AV cmd="PUT"><System><Power_Control><Power>Standby</Power></Power_Control></System></YAMAHA_AV>]]
	            xml_put_vol_up = [[<YAMAHA_AV cmd="PUT"><System><Volume><Lvl>Up</Lvl></Volume></System></YAMAHA_AV>]]
	            xml_put_vol_down = [[<YAMAHA_AV cmd="PUT"><System><Volume><Lvl>Down</Lvl></Volume></System></YAMAHA_AV>]]
	            xml_put_vol_mute = [[<YAMAHA_AV cmd="PUT"><System><Volume><Mute>On</Mute></Volume></System></YAMAHA_AV>]]
	            xml_put_vol_level_12 = [[<YAMAHA_AV cmd="PUT"><System><Volume><Lvl>12</Lvl></Volume></System></YAMAHA_AV>]]
                xml_put_vol_level_25 = [[<YAMAHA_AV cmd="PUT"><System><Volume><Lvl>25</Lvl></Volume></System></YAMAHA_AV>]]
                xml_put_input_dig1 = [[<YAMAHA_AV cmd="PUT"><System><Input><Input_Sel>DIGITAL1</Input_Sel></Input></System></YAMAHA_AV>]]
                xml_put_input_tuner = [[<YAMAHA_AV cmd="PUT"><System><Input><Input_Sel>TUNER</Input_Sel></Input></System></YAMAHA_AV>]]
                xml_put_band_dab = [[<YAMAHA_AV cmd="PUT"><Tuner><Play_Control><Band>DAB</Band></Play_Control></Tuner></YAMAHA_AV>]]
                
                if device.levelName == 'Vol +' and power == 'On' then 
	                domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_put_vol_up})
	                volume = volume+1
	            end
	            if device.levelName == 'Vol -' and power == 'On' then 
	                domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_put_vol_down})
	                volume = volume-1
    	        end
	            if device.levelName == 'Mute' and power == 'On' then
	                domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_put_vol_mute})
                end
	           if device.levelName == 'Off' then
	                domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_put_vol_level_12})
	                domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_put_power_standby})
	                volume = 12
	                power = 'Standby'
	           end
	           if device.levelName == 'TV' then
	               if power == 'On' then
	                    domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_put_input_dig1})
	               end
	               if power == 'Standby' then 
	                   domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_put_power_on})
	                   domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_put_input_dig1}).afterSec(1)
	                   power = 'On'
	               end
	               domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_put_vol_level_25}).afterSec(1)
	               inputname = 'Digital 1 - TV'
	               volume = 25
	           end
	           if device.levelName == 'DAB' then
	               if power == 'On' then
	                    domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_put_vol_level_12})
	                    domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_put_input_tuner})
	               end
	               if power == 'Standby' then 
	                   domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_put_power_on})
	                   domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_put_input_tuner}).afterSec(1)
	                   domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_put_vol_level_12}).afterSec(1)
	                   
	                   power = 'On'
	               end
	                 
	               domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_put_band_dab})
	               domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_get_playinfo, callback = 'statusplayinfo'}).afterSec(10)
    	           volume = 12
    	           inputname = 'DAB' 
    	        end
            end
            
            if device.idx == 693 then
                PresetPre = [[<YAMAHA_AV cmd="PUT"><Tuner><Play_Control><Preset><DAB><Preset_Sel>]]
                PresetSuf = [[</Preset_Sel></DAB></Preset></Play_Control></Tuner></YAMAHA_AV>]]

                preset = string.sub(device.levelName,1,2)
                presetname = string.sub(device.levelName,5)
                presetinfo = ' '
                
                xml_put_tuner_preset = PresetPre .. preset .. PresetSuf
	            domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_put_tuner_preset})
	            domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_get_playinfo, callback = 'statusplayinfo'}).afterSec(3)
	            
	        end	
	        
	        if (device.isTimer) then
	                domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_get_status, callback = 'statusresponse'}).afterSec(1)
				    domoticz.openURL({url = yamahaip, method = 'POST', headers =  head, postData = xml_get_playinfo, callback = 'statusplayinfo'}).afterSec(8)
		    end
	        
	        if (device.isHTTPResponse) then 

                responsestatus = string.match(tostring(device.data), "<System><(%a+)_" )
                responseplayinfo = string.match(tostring(device.data), "<Tuner><(%a+)_" )

                if responsestatus == 'Basic' then
                    yamahastatus = tostring(device.data)
                    
                    power = string.match(yamahastatus, "<Power>(%a+)</Power><" )
                    inputname = string.match(yamahastatus, "<Input><Input_Sel>(.*)</Input_Sel>" )
                    volume = string.match (yamahastatus, "<Lvl>(%d+)</Lvl>" )
                    mute = string.match(yamahastatus, "<Mute>(%a+)</Mute><" )
                   
                    if inputname == 'TUNER' then
                        inputname = 'DAB'
                    end
                     
                    if inputname == 'DIGITAL1' then
                        inputname = 'Digital 1 - TV'
                    end
                    
                end
                if responseplayinfo == 'Play' then
                    yamahaplayinfo = tostring(device.data)
                    preset = string.match(yamahaplayinfo, "<DAB><Preset><Preset_Sel>(%d+)</Preset_Sel>" )
                    presetselector = string.sub(domoticz.devices(693).levelName,1,2)
                --    domoticz.log('preset is ' .. tonumber(preset) .. ' presetselector is ' .. tonumber(presetselector) , domoticz.LOG_INFO)
                    if tonumber(preset) ~= tonumber(presetselector) then
                        domoticz.devices(693).switchSelector(preset*10).silent()
                    end
                    
                    if power == 'On' then
                        presetname = string.match(yamahaplayinfo, "<Service_Label>(.*)</Service_Label>" )
                        presetinfo = string.match(yamahaplayinfo, "<DLS>(.*)</DLS>" )
                    end
                end
              
            end 
            if power == 'Standby' then
                inputname = 'Standby'
            end
	        domoticz.variables(1).set(inputname)
	        domoticz.variables(2).set(volume)
	        domoticz.variables(6).set(preset)
	        domoticz.variables(4).set(presetname)
	        domoticz.variables(7).set(presetinfo)
	        domoticz.variables(8).set(power)
	        input2 = inputname
	        
	        if inputname == 'DAB' then
	            input2 = tostring(inputname .. ' ' .. preset .. ' ' .. presetname .. ' | ' .. presetinfo) 
	        end
	            
	        display = domoticz.utils.urlEncode('Vol ' ..  tostring(volume) .. ' | ' .. input2)
	        domoticz.devices(685).setValues('0', display) 


	end
}
Have fun!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest