Sunday, November 2, 2014

Turn off Hue light bulb when TV is turned on

I had been enjoying Hue remote controlling feature with RaspBMC add-on: http://meethue.wordpress.com But since our family started Netflix, I don't have change to use RaspBMC as much as before.

This script is to do the similar thing with little difference. When I am in my room with TV turned on, I want to turn off the light always. So I made a script that checks if the TV is turned on and if it is, it will turn off the light.

This cannot be done without hardware help on TV side. My Samsung TV is not a smart TV but it has many advanced features. Some of features are related to network. It can upgrade firmware or stream movies from DLNA server. It has Wired lan port but I cannot use it since the TV is in my room. I had to buy a Wifi adapter that specifically made for the TV by Samsung. The Wifi adapter was almost $100 for long time but it dropped to $40-wish after people found some of cheaper Wifi adapters are compatible. I got one at $34.99 last year. It seems that the price didn't change much; I can see it around $37~$39 from Amazon.

But the point is that if your TV is able to access network, you can use this script, because this script uses only basic aspect of network adapters. It doesn't have to be Samsung TV and most of Smart TV will work with this script.

The script works in three steps:
  1. Figure out the IP address of TV and Hue brigde
  2. Ping on the TV IP
  3. Turn off Hue if TV is turned on

There is one assumption that came from my TV. It shouldn't respond to PING if the TV is turned off. If your TV respond to PING request even when it is turned off, I will be surprised.

#!/bin/sh
username=newdeveloper
MAC_TV=f4:7b:5e:41:91:00
MAC_HUE=00:17:88:09:ef:18
ipHue=192.168.0.50

CURL=/usr/bin/curl
GREP=/bin/grep
ARP=/usr/sbin/arp
CUT=/usr/bin/cut
PING=/bin/ping
[ ! -f "$CURL" ] && echo "$CURL not found" && exit 1
[ ! -f "$GREP" ] && echo "$GREP not found" && exit 2
[ ! -f "$ARP"  ] && echo "$ARP  not found" && exit 3
[ ! -f "$CUT"  ] && echo "$CUT  not found" && exit 4
[ ! -f "$PING" ] && echo "$PING not found" && exit 5

$PING -c 1 $ipHue > /dev/null
arpList=`$ARP -n | $GREP 192`
ipTv=`echo "$arpList"  | $GREP $MAC_TV  | $CUT -d' ' -f1`
ipHue=`echo "$arpList" | $GREP $MAC_HUE | $CUT -d' ' -f1`
echo "TV  ip: $ipTv"
echo "Hue ip: $ipHue"
[ "x$ipTv"  = "x" ] && echo "TV  IP is not found" && exit 6
[ "x$ipHue" = "x" ] && echo "Hue IP is not found" && exit 7

gotPing=`$PING -c 1 $ipTv | $GREP "1 received"`
[ "x$gotPing" = "x" ] && exit 0
echo "Ping from TV IP responded and probably it is turned on"

data='{"on":false}'
lightIndex=2
URL=http://$ipHue/api/$username/lights/$lightIndex/state

echo "Turning off Hue # $lightIndex ..."
gotState=`$CURL -s -X PUT --data $data $URL`
echo $gotState
If the IP address is fixed manually, you can skip the slow step 1. But it is still good idea to check the kernel cache first with "arp", because it will avoid PING test when TV was turned off. With "arp", PING test will occur only when the TV is turned on. Once PING test failed, meaning TV is turned off, the kernel will update the ARP table; without PING test the cache data may last even when the TV was turned off long time ago. If you decided to use dynamic IP address, you need to find out what MAC address the TV has.

For the controlling Hue lights with a script, I already explained it a few month ago, so I am skipping it here.

Once you got the script running, you can keep running it with crontab.

1 comment:

  1. It seems that I should just use a fixed IP address for Hue. Arp cache doesn't seem to keep the data long enough. Probably it is because Hue bridge doesn't send out anything to the network.

    ReplyDelete

About Me

My photo
Tomorrow may not come, so I want to do my best now.