Send play state and content type from Emby to openHAB, Light control

Tags: #<Tag:0x00007efec035b8d8> #<Tag:0x00007efec035b7e8> #<Tag:0x00007efec035b6d0>

Below you’ll find a solution on how to connect Emby media server to openHAB2.
This is accomplished by using Emby Scripter-X plugin, which you have
to provide on your Emby server.

1; Platform:

  • Synology DS218+
  • DSM version: 6.2.3-25426 Update 2

2; Software prerequisite:

3; Goal:
Light control according the Emby play state. If I play off a movie then turn off my lights.
Or is it a serie? Then dim the lights to half or whatever.If I finished my Emby & Chill session?
Set back all the lights.

4; Problem:
I couldn’t run scripts under Emby server when it has installed from the package center. Even if I gave admin rights. Is it limitation or somethings else? I don’t know. Maybe smarter people can tell the truth.

5; How to:
5.1 openHAB site:

Create two items. You can do it on Paper UI or textual, doesn’t matter. One for the playstate and one for the type of content. Type content can be here later: Movie, Episode or Audio.
I recommend to replace “myPlayer” with the Emby device name to avoid confusion, like Chrome or you chose to name your device.

emby.items

Switch	Emby_myPlayer_Stop		"<myPlayer> Stop"				{ autoupdate="true" }
String	Emby_myPlayer_MediaType	"<myPlayer> Mediatype [%s]"

Now go to openhab on localhost:8080/start/index and find REST API. You find “items” below. Click on it and look for POST /items/{itemname}. There are two fields with the text (required). The “itemname” gonna be “myPlayer” and to the body write ON in. Then click on “Try it out!”. Do not change anything else! Parameter content type must be text/plain. If everything fine, you’ll see the message Response Body with “no content” and Response Code “200”. That’s mean, we’re good. Now, create a text file with notepad++ and copy the curl command into it. Then repeat the procedure with the other item too, just to be all fine. If you are done, rename the file to “play.sh”.

file: play.sh - as template

curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "ON" "http://localhost:8080/rest/items/Emby_myPlayer_Stop"
curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "ON" "http://localhost:8080/rest/items/Emby_myPlayer_Mediatype"

5.2 Emby site:

If the plugin isn’t installed yet, go to Emby dashboard and on the left side click on Plugins. Choose Catalogue above and install the Emby Scripter-X plugin from the general category. After install, restart the Emby server. On synology then create a folder under Docker volume and name it “scripts”. Like “/volume1/docker/emby/scripts”. Place your play.sh file here and make a copy and rename it to “stop.sh”. From now on, we need to use putty or other ssh client to connect to NAS. Do it and find your files. First, open the file play.sh with file editor(vim, nano) and alter the content. After -d (–data) in first row change ON to OFF and in 2nd row be “”$1"" .

New play.sh file:

curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "OFF" "localhost:8080/rest/items/Emby_myPlayer_Stop"
curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d ""$1"" "localhost:8080/rest/items/Emby_myPlayer_Mediatype" 

Save and close it. Then open stop.sh with file editor. And only in the 2nd row after the -d sign change it to “UNDEF”.

New stop.sh file:

curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "ON" "http://localhost:8080/rest/items/Emby_myPlayer_Stop"
curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "UNDEF" "http://localhost:8080/rest/items/Emby_myPlayer_Mediatype"

save and close this file, too. Don’t forget to make them executable!

sudo chmod +x play.sh stop.sh

Go back to Emby’s dashboard and you’ll see Scripter-X -> Action on the left side. Click on and find “onPlaybackStart”.
Add a new rule with the big + sign in the top left corner. Click on pen and type the following to:

Run:

/config/scripts/play.sh

using:

/bin/sh

and into parameter box:

%item.type%

Save it.
Do the same with “onPlaybackStopped”, but point the run path to stop.sh file and left parameter box empty.

Almost there. Now you can try it out from browser or any other client. But right now it will command openHAB any time and from any client if you play content off. It has to be limited to one device. How’d we do that? Easy. You see those little colored boxes. They work like drag and drop item in Windows. Go edit the settings.
Hoover your mouse over %device.name%, grab it and drop it into the “where” box. Also do it with “Equals” and “Text” below. Then click on text and write there your emby device name. Repeat on the “onPlaybackStopped”.

Whenever you want to add a new device, it is necessary to add new rule to the onPlaybackStart and onPlaybackStopped box with the new device name.

6; Example.rule

rule "Emby Server Lights"
when
    Item Emby_myPlayer_MediaType changed
then
	var String strEmbyState = Emby_myPlayer_MediaType.state.toString
	switch strEmbyState {
	case "Movie" : {
		//Switch off lights
	}
	case "Episode" : {
		//Dim lights
	}
	case "Audio" : {
		//Turn on smoke machine
	}
	case "UNDEF" : {
		//Switch back lights
	}
}
end

I’m not using the stop item in the example rule, but think of it as a proxy item. You can prevent other light control rules to run during a playback session.

Enjoy the magic!