If TV is turned on then change input receiver

I have a Sony Bravia TV and I tried to install the Sony binding, but this didn’t work (the TV was not detected). Therefor, I’ve created a python script that returns 0 or 1 in case the TV is off or on (I can easily change the script to return “ON” or “OFF” if that would be easier). The ultimate goal is to change the input channel of the receiver to “HDMI3” or “AUDIO3” in case the TV is switched on or off.

I had the following rule in mind …

As long as there is movement in the room (to avoid that I execute the python script day and night), I have to execute the python script every second or so to be sure that the TV is not turned on. When the TV is on, I have to execute the python script every second or so to be sure that the TV is not turned off (in that case I have to switch the receiver to “AUDIO3”).

I have this already configured.

Thing exec:command:bravia-status [command="/etc/openhab2/scripts/bravia_status.py"]

Switch stat_Bravia { channel="exec:command:bravia-status:run" }

Motion in the room can be detected by the item “mot_bePod_MeetingRoom”. The input channel of the receiver can be switched with eg. yamahareceiver_yamahaAV_468a2766_input.sendCommand(“AUDIO3”).

Can you help this newbie by defining this rule?

Tx – Ivan

I’ve figured it out …

String stat_Bravia { channel="exec:command:bravia-status:output" }
Thing exec:command:bravia-status [ command="/etc/openhab2/scripts/bravia_status.py", interval=3 ]

rule "Receiver to HDMI3 input when TV is turned on"
when
        Item stat_Bravia changed to 1
then
        yamahareceiver_yamahaAV_468a2766_power.sendCommand(ON)
        Thread::sleep(10000)
        yamahareceiver_yamahaAV_468a2766_input.sendCommand("HDMI3")
        dim_BePod_Dim1_TV.sendCommand(OFF)
end

rule "Receiver to HDMI3 input when TV is turned off"
when
        Item stat_Bravia changed to 0
then
        yamahareceiver_yamahaAV_468a2766_power.sendCommand(ON)
        Thread::sleep(10000)
        yamahareceiver_yamahaAV_468a2766_input.sendCommand("AUDIO3")
        dim_BePod_Dim1_TV.sendCommand(ON)
end
2 Likes

I’m happy you figured it out.

My only critique is that long sleeps are discouraged. It is better to use Timers. You have a few choices there as well but in this case, let’s use Timers instead of the Expire Binding.

rule "Receiver to HDMI3 input when TV is turned on"
when
        Item stat_Bravia changed to 1
then
        yamahareceiver_yamahaAV_468a2766_power.sendCommand(ON)
        createTimer(now.plusSeconds(10), [|
            yamahareceiver_yamahaAV_468a2766_input.sendCommand("HDMI3")
            dim_BePod_Dim1_TV.sendCommand(OFF)
        ])
end

This will schedule the code between the [| ] to run in ten seconds from now, freeing up the execution thread to do other work during the wait.

Finally, this is just a style thing but I would change what your script returns to ON and OFF so the code makes clear what is ON and what is OFF. When you do this though you will probably have to merge your rules, which is probably a good idea anyway because of DRY (Don’t Repeat Yourself).

rule "TV changed state"
when
        Item stat_Bravia changed
then
        yamahareceiver_yamahaAV_468a2766_power.sendCommand(ON)

        // these lines are using the trinary operator which lets you set the state of a val based on a condition
        val cmd = if(stat_Bravia.state == ON) "HDMI3" else "AUDIO3"
        val dim1 = if(stat_Bravia.state == ON) OFF else ON

        createTimer(now.plusSeconds(10), [|
            yamahareceiver_yamahaAV_468a2766_input.sendCommand(cmd)
            dim_BePod_Dim1_TV.sendCommand(dim1)
        ])
end
3 Likes