Control lights when playing a movie on FireTV / AndroidTV

Introduction

This tutorial shows how to power off lights when playing a movie on a FireTV or AndroidTV with Amazon Prime Video or when launching the Netflix or Youtube app. As soon as the movie has ended or the apps are closed, the light will be powered on again.

Similar topics and inspiration

Prerequisites

  1. Connect to Fire TV Through ADB (Amazon Tutorial)
  2. Install the OpenHAB Exec binding
  3. Have any light in place as OpenHAB item that can be controlled

Setup

Get the FireTV state via ADB

Create the file FireTVState.sh in the scripts folder of the OpenHAB directory (/etc/openhab2/scripts/).

When connected to the FireTV, execute the following command to check if Amazon Prime Video content (movies, series, trailers) is running in foreground.

/usr/bin/adb shell top -n 1 | grep -e com.amazon.avod | grep fg

To also check for the Netflix or Youtube app, execute the following instead.

/usr/bin/adb shell top -n 1 | grep -e com.amazon.avod -e youtube -e netflix | grep fg

FireTVState.sh

This is the complete script.

#!/bin/bash
# grant access for the openhab user to port 5037 (error message: cannot bind 'local:5037')
/bin/chown openhab:openhab /tmp/5037 >> /dev/null

# start adb server if needed
/usr/bin/sudo /usr/bin/adb start-server >> /dev/null

# connect to fire tv stick if needed
CONNECTED=$(/usr/bin/adb connect 192.168.178.59 | grep connected)

if [ -z "$CONNECTED" ]
then
	#echo "device is offline" >> /etc/openhab2/scripts/FireTVState.log
	echo "OFF"
else
	/usr/bin/adb wait-for-device

	# check if Amazon Video (com.amazon.avod), Youtube or netflix is running in foreground (fg)
	STATE=$(/usr/bin/adb shell top -n 1 | grep -e com.amazon.avod -e youtube -e netflix | grep fg)

	if [ -z "$STATE" ]
	then
		#echo "no video process running in foreground" >> /etc/openhab2/scripts/FireTVState.log
		echo "OFF"
	else
		#echo "video is playing" >> /etc/openhab2/scripts/FireTVState.log
		echo "ON"
	fi

	# disconnect
	/usr/bin/adb disconnect
fi

FireTV.items

Create the following item. The configuration is still using the Exec1 binding, but porting shouldn’t be difficult. This item is querying the FireTV every 5 seconds using ADB. Adjust the interval if needed.

Switch FIRETV_State "FireTV State" (gFireTV) {exec="<[/etc/openhab2/scripts/FireTVState.sh:5000:REGEX((.*?))]"}

FireTV.rules

Turn lights on and off as soon as the FireTV state changes.
In addition, turn off all lights as soon as a movie or series is running (if something is playing for more than 5 minutes, so movie trailers are excluded).

rule "FireTV state changed from OFF to ON"
when
	Item FIRETV_State changed from OFF to ON
then
	logInfo("RULES", "FireTV state changed from OFF to ON")
	sendCommand(LightAmbience_LivingroomScreen, OFF)
end

rule "FireTV state changed from ON to OFF"
when
	Item FIRETV_State changed from ON to OFF
then
	logInfo("RULES", "FireTV state changed from ON to OFF")
	sendCommand(LightAmbience_LivingroomScreen, ON)
	sendCommand(LightAmbience_LivingroomSofa, ON)
end

rule "FireTV State"
when
	Item FIRETV_State received command
	or
	Item FIRETV_State received update
then
	if(FIRETV_State.changedSince(now.minusMinutes(6), "mysql") && !FIRETV_State.changedSince(now.minusMinutes(5), "mysql")) {
		if(FIRETV_State.state == ON) {
			logInfo("RULES", "FireTV State is ON for more than 5 minutes")
			sendCommand(LightAmbience_LivingroomSofa, OFF)
		}
		else {
			logInfo("RULES", "FireTV State is OFF for more than 5 minutes")
		}
	}
end

MySQL.persist

Store all updates of the FireTV State, so that the command changedSince can be called on the Item

Strategies {
    default = everyChange
}

Items {
	FIRETV_State : strategy = everyChange
}
3 Likes

Hi,
with my FireTV Stick 4k Max this method is no longer working. I implemented this here, with focus on any running Audio playback which give a clear status back (PlayBack Sensor · Issue #36 · dwaan/homebridge-adb · GitHub).

FireTVState.sh

#!/bin/bash
# grant access for the openhab user to port 5037 (error message: cannot bind 'local:5037')
/bin/chown openhab:openhab /tmp/5037 >> /dev/null

# start adb server if needed
/usr/bin/adb start-server >> /dev/null

# connect to fire tv stick if needed
CONNECTED=$(/usr/bin/adb connect **YOURIP** | grep connected)

if [ -z "$CONNECTED" ]
then
	#echo "device is offline" >> /etc/openhab2/scripts/FireTVState.log
	echo "OFF"
else
	/usr/bin/adb wait-for-device

	# check if Amazon FireTV Stick 4k Max, is playing audio 
	STATE=$(/usr/bin/adb shell "dumpsys audio | grep 'player piid' | tail -1 | grep 'started'") 
	if [ -z "$STATE" ]
	then
		#echo "no video process running in foreground" >> /etc/openhab2/scripts/FireTVState.log
		echo "OFF"
	else
		#echo "video is playing" >> /etc/openhab2/scripts/FireTVState.log
		echo "ON"
	fi

	# disconnect
	/usr/bin/adb disconnect  >> /dev/null
fi

FireTV.items

I used the new Exec Binding

Switch FIRETV_State "FireTV State" (gFireTV)  {channel="exec:command:FireTV:output"}

Exec Thing (i created this one via UI, for having it editable via UI)

/misc/exec.whitelist

Don’t forget to allow the FireTVState.sh in your whitelist

# For security reasons all commands that are used by the exec binding or transformation need to be whitelisted.
# Every command needs to be listed on a separate line below.
/PATHtoYour/FireTVState.sh

Best,
Hagi