Automation with Firetv/Emby

at what level explanation are you looking?

You need to generate an API key for your emby server.

For each FireTV or FireTV Stick, you need to create a command line sensor that includes the device ID in the curl call to the Emby API.

I use Postman to find this information. The GET call for my setup looks like this:

http://tv.domain.com:8096/emby/Devices

and headers:
X-Emby-Token=xxxxxxx
Content-Type=application/json


The Response should look like this:
{
“Items”: [
{
“Name”: “SMG965U”,
“Id”: “63e06b564b84b016”,
“LastUserName”: “Rob”,
“AppName”: “Emby for Android Mobile”,
“AppVersion”: “3.0.48”,
“LastUserId”: “70ed5f4b409f4dcd83526a04bc25e0a8”,
“DateLastActivity”: “2019-05-19T21:49:35.0000000Z”,
“IconUrl”: “https://github.com/MediaBrowser/Emby.Resources/raw/master/images/devices/android.png
},
{
“Name”: “Hedrick Living Room”,
“Id”: “4a31382947bbd1a2”,
“LastUserName”: “Family”,
“AppName”: “AndroidTv”,
“AppVersion”: “1.6.94a”,
“LastUserId”: “0588ff12bc3e45fdbaeee9fc15d27a09”,
“DateLastActivity”: “2019-05-19T21:47:06.0000000Z”,
“IconUrl”: “https://mediabrowser.github.io/Emby.AndroidTv/appicon.png
},
{
“Name”: “Playroom”,
“Id”: “66a3382525ce0893”,
“LastUserName”: “Olivia”,
“AppName”: “AndroidTv”,
“AppVersion”: “1.6.94a”,
“LastUserId”: “81839facd92b4c86991c3088275a859b”,
“DateLastActivity”: “2019-05-19T21:26:23.0000000Z”,
“IconUrl”: “https://mediabrowser.github.io/Emby.AndroidTv/appicon.png
},

You want the Id from this information for each device you want to control.

  - platform: command_line
    name: embysessionplayroom
    command: "curl -H \"Content-Type: application/json\" -H \"X-Emby-Token: xxxxxxxxxxxxxxxxxxx\" -X GET \"http://tv.domain.com:8096/emby/Sessions\" | jq -r '.[] | select(.DeviceId==\"66a3382525ce0893\") | .Id'"
    scan_interval: 4

Make sure you escape all quotes inside the command with a backslash as I have

This sensor will report back the unique session id later.

You need to also get the Id for the user you want to login as.

Use the same method as for devices, but your GET is
http://tv.domain.com:8096/emby/Users

Finally, you need a list of Movies and their ID’s that the user you want to control is allow to watch.
Same headers as above
http://tv.chucklehead.lol:8096/emby/Users/USERIDGUID/Items?Recursive=true&IncludeItemTypes=Movie

Create an input_select for the movies. Make sure you remove illegal characters.

  firetv_movies:
    name: firetv_movies
    options:
      - Cinderella
      - Despicable Me
      - Despicable Me 2
      - Despicable Me 3
      - Finding Nemo
      - Frosty the Snowman
      - Frozen
      - Harry Potter and the Philosophers Stone
      - Lady and the Tramp
      - Madagascar
      - Madagascar 3 Europes Most Wanted
      - Madagascar Escape 2 Africa
      - Monsters, Inc.
      - One Hundred and One Dalmatians
      - Pinocchio
      - Pocahontas
      - Rudolph the Red-Nosed Reindeer
      - Snow White and the Seven Dwarfs
      - The Lion King
      - The Little Mermaid
      - Willy Wonka & the Chocolate Factory
    initial: The Little Mermaid
    icon: mdi:movie

Create an input_datetime

  liv_trigger_time:
    name: "turn on liv show"
    has_date: false
    has_time: true
    initial: '07:00'

In your configuration.yaml make sure you can control the firestick thru the androidtv platform

  - platform: androidtv
    name: Olivias Room
    host: 172.17.17.5
    adbkey: !secret adbkey_olivia

There are several automations that need to be created. Before I did this I connected to all my firesticks via adb and changed the configuration so that they never go to sleep or turn off… This is important.

This is what my final card looks like:
image

Create the rest_command:

  emby_start_show:
    url: http://tv.domain.com:8096/emby/Sessions/{{ session }}/Playing?ItemIds={{ show }}&PlayCommand=PlayNow&api_key={{ token }}
    method: POST
    headers:
      accept: '*/*'
      Content-Type: 'application/json'
    payload: '{"ControllingUserId":"{{ userid }}","SubtitleStreamIndex":0,"AudioStreamIndex":0,"StartIndex":0}'

Create the automation’s:

- id: '155648556587'
  alias: call automations for olivia device at time
  initial_state: true
  trigger:
  - platform: template
    value_template: '{{ states.sensor.time.state == (states.input_datetime.liv_trigger_time.attributes.timestamp
      | int | timestamp_custom("%H:%M", False)) }}'
  action:
  - service: automation.trigger
    entity_id: automation.launch_emby_on_olivia_device
  - service: automation.trigger
    entity_id: automation.play_emby_show_on_olivia_device
  - service: automation.trigger
    entity_id: automation.turn_olivias_tv_on

The TV turn on action is for my harmony IR device to turn on the TV

- id: '15855573698'
  alias: launch emby on olivia device
  trigger: []
  condition:
  - condition: state
    entity_id: automation.call_automations_for_olivia_device_at_time
    state: 'on'
  action:
  - service: script.adb_start_emby_on_device
    data_template:
      device: '{% if is_state(''input_select.firetv_device'',''Olivias Room'') %}
        media_player.olivias_room {% elif is_state(''input_select.firetv_device'',''Playroom'') %}
        media_player.play_room {% elif is_state(''input_select.firetv_device'',''Bathroom'') %}
        media_player.bathroom {% else %} media_player.olivias_room {% endif %}

- id: '15857596587'
  alias: play emby show on olivia device
  trigger: []
  condition:
  - condition: state
    entity_id: automation.call_automations_for_olivia_device_at_time
    state: 'on'
  action:
  - wait_template: "{{ not is_state('sensor.embysessionolivia', '') }}"
  - service: rest_command.emby_start_show
    data_template:
      show: '{% if is_state(''input_select.firetv_movies'',''Cinderella'') %} 24959
        {% elif is_state(''input_select.firetv_movies'',''Despicable Me'') %} 60872
        {% elif is_state(''input_select.firetv_movies'',''Despicable Me 2'') %} 67305
        {% elif is_state(''input_select.firetv_movies'',''Despicable Me 3'') %} 67175
        {% elif is_state(''input_select.firetv_movies'',''Finding Nemo'') %} 60819
        {% elif is_state(''input_select.firetv_movies'',''Frosty the Snowman'') %}
        36271 {% elif is_state(''input_select.firetv_movies'',''Frozen'') %} 24954
        {% elif is_state(''input_select.firetv_movies'',''Harry Potter and the Philosophers
        Stone'') %} 67224 {% elif is_state(''input_select.firetv_movies'',''Lady and
        the Tramp'') %} 24957 {% elif is_state(''input_select.firetv_movies'',''Madagascar'')
        %} 60841 {% elif is_state(''input_select.firetv_movies'',''Madagascar 3 Europes
        Most Wanted'') %} 67174 {% elif is_state(''input_select.firetv_movies'',''Madagascar
        Escape 2 Africa'') %} 67155 {% elif is_state(''input_select.firetv_movies'',''Monsters,
        Inc.'') %} 24958 {% elif is_state(''input_select.firetv_movies'',''One Hundred
        and One Dalmatians'') %} 50077 {% elif is_state(''input_select.firetv_movies'',''Pinocchio'')
        %} 24950 {% elif is_state(''input_select.firetv_movies'',''Pocahontas'') %}
        24953 {% elif is_state(''input_select.firetv_movies'',''Rudolph the Red-Nosed
        Reindeer'') %} 36272 {% elif is_state(''input_select.firetv_movies'',''Snow
        White and the Seven Dwarfs'') %} 24952 {% elif is_state(''input_select.firetv_movies'',''The
        Lion King'') %} 57496 {% elif is_state(''input_select.firetv_movies'',''The
        Little Mermaid'') %} 24956 {% elif is_state(''input_select.firetv_movies'',''Willy
        Wonka & the Chocolate Factory'') %} 92962 {% else %} 60819 {% endif %}

        '
      token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
      userid: 81839facd92b4c86991c3088275a859b
      session: '{{ states.sensor.embysessionolivia.state }}'

Tie it all together with a UI-Lovelace card like this:
- title: Automation
icon: ‘mdi:home-automation’
panel: false
path: automation
theme: default
type: vertical-stack
cards:
- type: custom:compact-custom-header
main_config: false
disable: false
header: true
menu: show
background_image: true
notifications: show
voice: hide
options: clock
clock_format: 12
exceptions:
- conditions:
user: dashboard
config:
show_tabs: 12
- type: entities
title: Olivia Automation
show_header_toggle: false
entities:
- entity: automation.call_automations_for_olivia_device_at_time
name: Enable/Disable

          - entity: input_datetime.liv_trigger_time
            name: Time

          - entity: input_select.firetv_device
            name: Device

          - entity: input_select.firetv_movies
            name: Movie
4 Likes