when TV is on, run a script. when off, run another script

Post Reply
tung256
Posts: 31
Joined: Thu Mar 01, 2018 10:45 pm
languages_spoken: english
ODROIDs: c2
Contact:

when TV is on, run a script. when off, run another script

Unread post by tung256 » Sun Apr 29, 2018 10:15 am

by default, i dont think ubuntu Mate 16 has HDMI CEC working, so i installed via

Code: Select all

sudo apt-get install cec-utils
check it is working via

Code: Select all

echo scan | cec-client -s -d 1  
and then i see this:
CEC bus information
==================
device #0: TV
address: 0.0.0.0
active source: no
vendor: Toshiba
osd string: TV
CEC version: 1.4
power status: on
language: eng


device #1: Recorder 1
address: 2.0.0.0
active source: no
vendor: Toshiba
osd string: CECTester
CEC version: 1.4
power status: on
language: eng


looks about right. my Odroid C2 is on hdmi port #2.
how do i make ubuntu run a script when TV is turned on?
how do i make ubuntu run another script when TV is turned off?

User avatar
mad_ady
Posts: 5091
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, N1
Location: Bucharest, Romania
Contact:

Re: when TV is on, run a script. when off, run another scrip

Unread post by mad_ady » Sun Apr 29, 2018 1:15 pm

You should check if any relevant messages make it to syslog/dmesg when you turn on/off your tv. If they do you could write a script to do a tail -f on that log and run your commands when appropriate.

tung256
Posts: 31
Joined: Thu Mar 01, 2018 10:45 pm
languages_spoken: english
ODROIDs: c2
Contact:

Re: when TV is on, run a script. when off, run another scrip

Unread post by tung256 » Sun Apr 29, 2018 8:16 pm

mad_ady wrote:You should check if any relevant messages make it to syslog/dmesg when you turn on/off your tv. If they do you could write a script to do a tail -f on that log and run your commands when appropriate.
when i opened /var/log/dmesg, this is all i see:
(Nothing has been logged yet.)

User avatar
mad_ady
Posts: 5091
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, N1
Location: Bucharest, Romania
Contact:

Re: when TV is on, run a script. when off, run another scrip

Unread post by mad_ady » Sun Apr 29, 2018 8:38 pm

Try dmesg -w

tung256
Posts: 31
Joined: Thu Mar 01, 2018 10:45 pm
languages_spoken: english
ODROIDs: c2
Contact:

Re: when TV is on, run a script. when off, run another scrip

Unread post by tung256 » Tue May 01, 2018 5:25 am

mad_ady wrote:Try dmesg -w
that command works!
when TV power button is pressed, i see this in terminal: [105711.824729] hdmitx: system: irq 80000004

when TV is fully booted up in 5 seconds, after the TV logo disappears, i see this in terminal: [105727.973437] hdmitx: system: irq 80000002

but sadly when TV button is pressed for power off, i get nothing.

User avatar
rooted
Posts: 6065
Joined: Fri Dec 19, 2014 9:12 am
languages_spoken: english
Location: Gulf of Mexico, US
Contact:

Re: when TV is on, run a script. when off, run another scrip

Unread post by rooted » Tue May 01, 2018 8:37 am

Check

Code: Select all

power status: xx 
With cec-client with TV off, if it says off you can use it.

tung256
Posts: 31
Joined: Thu Mar 01, 2018 10:45 pm
languages_spoken: english
ODROIDs: c2
Contact:

Re: when TV is on, run a script. when off, run another scrip

Unread post by tung256 » Wed May 02, 2018 10:05 am

rooted wrote:Check

Code: Select all

power status: xx 
With cec-client with TV off, if it says off you can use it.

i get error with that command as seen here:
odroid@odroid64:~$ power status: xx
-bash: power: command not found
odroid@odroid64:~$

User avatar
odroid
Site Admin
Posts: 29330
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID
Contact:

Re: when TV is on, run a script. when off, run another scrip

Unread post by odroid » Wed May 02, 2018 10:29 am

Refer this guide to access the cec-client on the command line.
https://www.endpoint.com/blog/2012/11/0 ... mi-devices

User avatar
rooted
Posts: 6065
Joined: Fri Dec 19, 2014 9:12 am
languages_spoken: english
Location: Gulf of Mexico, US
Contact:

Re: when TV is on, run a script. when off, run another scrip

Unread post by rooted » Wed May 02, 2018 10:38 am

tung256 wrote:
rooted wrote:Check

Code: Select all

power status: xx 
With cec-client with TV off, if it says off you can use it.

i get error with that command as seen here:
odroid@odroid64:~$ power status: xx
-bash: power: command not found
odroid@odroid64:~$
Above you ran it:

Code: Select all

echo scan | cec-client -s -d 1
Do it with the TV off and check the status of the line:

Code: Select all

power status: on 
or

Code: Select all

power status: off 

tung256
Posts: 31
Joined: Thu Mar 01, 2018 10:45 pm
languages_spoken: english
ODROIDs: c2
Contact:

Re: when TV is on, run a script. when off, run another scrip

Unread post by tung256 » Fri May 04, 2018 10:09 pm

yes! when see power status on or off now using this command:
echo scan | cec-client -s -d 1

now, how can i run do_this.sh when TV is on. off course, while TV is on, don't execute do_this.sh again. then when TV is off, execute do_that.sh. while TV is off, don't run do_that.sh

User avatar
mad_ady
Posts: 5091
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, N1
Location: Bucharest, Romania
Contact:

Re: when TV is on, run a script. when off, run another scrip

Unread post by mad_ady » Fri May 04, 2018 10:38 pm

Unfortunately you'll have to use polling - meaning inside an infinite loop check the state of cec. Have a global variable defined before the loop hold the current ON/OFF state. Inside the loop test the following condition:

Code: Select all

state=0
while :
do
   #1 if first cec device in on, 0 otherwise
   currentstate=`echo scan | cec-client -s -d 1 | grep "power status" | head -1 | grep on | wc -l`
   #state changed
   if [ "$currentstate" -ne "$state" ]; then
    if [ "$currentstate" -eq "1" ]; then
       #execute on TV on
    else
       #execute on TV off
    fi
    state=$currentstate
  fi
  sleep 10
done
Not tested, and not sure what effect continuous CEC probing might have on your TV.

tung256
Posts: 31
Joined: Thu Mar 01, 2018 10:45 pm
languages_spoken: english
ODROIDs: c2
Contact:

Re: when TV is on, run a script. when off, run another scrip

Unread post by tung256 » Sat May 05, 2018 5:42 am

i did not know constantly probing CEC can damage TV. in that case, it is not worth the trouble then.
my idea was to have TV auto play a slideshow using FEH when TV is turned on. stop FEH playing jpg from usb drive when TV is off.

for now, i have FEH playing JPG files from usb flash drive 24/7. it is super stable so far

User avatar
mad_ady
Posts: 5091
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, N1
Location: Bucharest, Romania
Contact:

Re: when TV is on, run a script. when off, run another scrip

Unread post by mad_ady » Sat May 05, 2018 4:25 pm

It won't damage your tv by probing cec. The only risk is that bad cec implementation in the tv might have memory leaks which will eventually crash the smart tv software. You will know when it crashes because it won't respond to remote control anymore and you'll have to powercycle the tv to recover.
So give it a try but be prepared to recognize the symptoms of memory starvation.

Post Reply

Return to “Ubuntu”

Who is online

Users browsing this forum: No registered users and 6 guests