Emby and Nvidia Shield device states

I read the device state of the shield as a trigger, use after sunset as a condition, and then call a service to change the light level.

- alias: Dim lights when Shield is playing
  trigger:
  - entity_id: media_player.shield_2
    platform: state
    to: playing
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - data:
      brightness: 55
      transition: 2
    entity_id: light.lamp, light.other_lamp
    service: light.turn_on

- alias: Brighten lights when Shield is idle
  trigger:
  - entity_id: media_player.shield_2
    platform: state
    to: standby
  - entity_id: media_player.shield_2
    platform: state
    to: paused
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - data:
      brightness: 155
      transition: 2
    entity_id: light.lamp, light.other_lamp
    service: light.turn_on

Iā€™m still pretty new with home assistant so maybe thereā€™s a better way. These were some of my first automations.

Ahh I see. Yeah I have the same automation basically but I wouldnā€™t want it to auto dim lights if things are playing on the shield, only if itā€™s Jellyfin/Emby playing.

Otherwise I donā€™t want it to dim for YouTubeTV, YouTube, etc.

Iā€™m guessing Iā€™ll just have to stick with the emby media player as the entity ID thenā€¦ just a pain in the butt because the start/stop triggers are instant but the pause/unpause information that is relayed takes a solid 15-20 seconds sometimes before it reports back to HA and therefore making the pause/unpause automations for the lights very slow.

Might be a little hacky, but I think you could use custom states to override the apps play states when you donā€™t want the lights to change.

Yeah maybe. I was also just noticing that when I start something on Android TV, it isnā€™t updating to ā€˜playingā€™ for a solid 6-8 seconds, same goes for paused/stopping too.

I previously used Plex with Tautulli with MQTT and it was instant and so nice but wanted to abandon that platform due to trust issued and move to an open-source solution in Jellyfin.

The start/stop work perfectly so maybe Iā€™ll just have to deal with the slow response when pausing.

I believe the androidtv package polls the device every 10 seconds. So if youā€™re unlucky and pause directly after a poll, it could take up to 10 seconds before the new state is read. Typically for me, itā€™s much shorter.

1 Like

This will help. I will just add this to my triggers:

  - entity_id: media_player.nvidia_shield
    from: playing
    platform: state
    to: standby

And through some quick testing itā€™s triggered within that time frame you mentioned, which is much better than the Emby/Jellyfin would report back when paused. Itā€™s not perfect like MQTT was with Plex but MUCH better than the solid 15-20 seconds it was before.

Much appreciated!

I updated my automations last night because of a new one I made where I donā€™t want the lights to dim. Itā€™s pretty easy to add a condition so the automation only happens when emby is playing. In my particular instance I didnā€™t want the dimming automations to happen when google cast is playing so I added this.

  - condition: template
    value_template: '{{ states.media_player.shield_2.attributes.app_name != "Google Cast" }}'

in your case you could use something like this so the automation only happens when emby is playing.

  - condition: template
    value_template: '{{ states.media_player.shield_2.attributes.app_name == "Emby" }}'

ofcourse updating media_player.shield_2 to the name of your shield device.

1 Like

Nice! I didnā€™t know that, thanks! :+1:

Do you find yourself having random ā€˜playingā€™ to ā€˜pausedā€™ moments with it?

I thought it ok but noticed that it goes from ā€˜playingā€™ to ā€˜pausedā€™ very randomly when Iā€™m using Jellyfin (itā€™s an open-source player forked from Emby). I have no idea what is causing it but you can see the random blips of paused on this chart. Anything that looks like a sliver of teal is one of those random cases. The other teal lines that look longer than a sliver were legitimate pauses.

image

It lasts about 1 polling session and it goes back to ā€˜playedā€™ and dims the lights again.

Just wondering if you have run into that at all. I tried to do the ADB GET_PROPERTIES and I believe I have all it all set properly.

    state_detection_rules:
      'org.jellyfin.androidtv':
        - 'playing':
            'wake_lock_size': 3
        - 'paused':
            'wake_lock_size': 1

Itā€™s very possible I just have the properties set wrong but I canā€™t figure out why itā€™s going into the ā€˜pausedā€™ state because itā€™ll be sometimes 20 minutes into a show/movie and havenā€™t changed or even touched the remote and it goes into that ā€˜pausedā€™ state and the lights brighten up and 8-10 seconds later they dim back down because itā€™s reporting ā€˜playingā€™.

Just wondering I guess if youā€™ve run into this at all or what your solution has been.

Iā€™ve had some movies that play the whole way through without issue, other times Iā€™ve had the lights brighten and dim 2-3 times during a single movie. It basically comes down to jellyfin/emby not setting a media_session_state like every other app does so playback is determined solely by wake_lock_size. Really the only solution is for JF/emby to add media_session_state to their apps. For me, it seems to happen rarely enough that Iā€™m fine waiting for them to fix their apps. I did make a post outlining this issue.

1 Like

I think Iā€™ll probably just use the Emby integration since it works for Jellyfin.

The only problem is pausing/unpausing delayed of 15 seconds or so. Otherwise itā€™s instant when you start and when you stop it.

I was having the lights brighten/dim too much unfortunately. Wish it had that other state so it could be more accurate. Oh well.

Appreciate all the feedback on this!

Update: Emby has implemented media_session_state as of Emby for Android TV v1.8.05 (currently in beta). Testing was as follows:

Emby idle
'audio_state': 'paused', 'wake_lock_size': 0, 'current_app': 'tv.emby.embyatv', 'media_session_state': 0

Emby paused
'audio_state': 'paused', 'wake_lock_size': 1, 'current_app': 'tv.emby.embyatv', 'media_session_state': 2

Emby playing
'audio_state': 'paused', 'wake_lock_size': 2, 'current_app': 'tv.emby.embyatv', 'media_session_state': 3

Seems to be working great with updated custom state detection rules, and also without any custom state detection rules. :slight_smile:

1 Like

Nice! I know you posted it on Jellyfinā€™s github but just in case others stumble across this in the future and have the same issue/request, I thought Iā€™d link your github enhancement request so people know that it is in the pipeline for a future Jellyfin release.

Thanks for bringing this all to light and keeping it updated!

Thank you so much for this. Had some issues getting the per app play state. How did you figure this out?

In developer tools > services, use androidtv.adb_command service with command: GET_PROPERTIES for the desired entity. Then, check the adb_response in developer tools > states for that same entity.

for emby androidTV
tv.emby.embyatv
I had to input,
[{"playing": {"wake_lock_size": 3, "wake_lock_size": 4}}, {"paused": {"wake_lock_size": 2, "wake_lock_size": 1}}]
it works.
if i reopen the rule it says
[{"playing": {"wake_lock_size": 4}}, {"paused": {"wake_lock_size": 1}}]
either way, its working now.
this [POST] had a bunch of configs
THIS DOC. was helpful for figuring out syntax, the ADB commands, reponses to get it working.

THIS POST, has code that allows me to turn on Hue Sync via Hue Essentials when Emby starts playing. (just fixed.)