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
}
2 Likes