Android tv and automations based on streaming service

Hi
I’m woundering if anyone can help me figure out how to make an automation based on what “program” is playing on the TV.

E.g. I have an android tv, integrated through the ADB integration, and I can see that my kids are watching youtube, so after 20 min I want to pause it and tell them through a speaker that they should change to something else.

I can do the last steps but i cant figure out how to trigger based on the android tv status.

You will need to set up a template sensor for the attribute, then use that sensor as your trigger.

template:
  - sensor:
      - name: Android TV Source
        state: "{{ state_attr('media_player.example', 'source') }}"

Automation trigger:

trigger:
  - platform: state
    entity_id: sensor.android_tv_source
    to: YouTube
    for: "00:20:00"

The state produced may be slightly different based on your TV, for example on my Amazon FireTV it would be YouTube (FireTV). Make sure the value for to matches what you see in the States tool for your media player entity.

Actually, you can trigger on an attribute directly

trigger:
  - platform: state
    entity_id: sensor.android_tv
    attribute: source
    to: YouTube
    for: "00:20:00"
1 Like

Unless it’s been changed recently, for is not reliable for attributes because it is based on the last_updated property which changes whenever any attribute or the state changes. This is especially an issue for OP’s question, since many YouTube videos are shorter than 20 minutes and the entity_picture attribute changes with each video, resetting the last_updated value.

EDIT: The behavior of for with attributes no longer works like that. :man_facepalming:


Instead of starting the “countdown” the moment YouTube is opened, maybe a “fairer” version might be something like:

template:
  - trigger:
      - platform: state
        attribute: source
        to: "YouTube (FireTV)"
        entity_id: media_player.fire_tv_10_0_0_64
    action:
      - alias: Buffer in case someone left a video queued and YouTube starts playing it or ads
        delay: 15
      - alias: Wait for a video to be chosen or 2 minutes of browsing
        wait_template: "{{ is_state('media_player.example', 'playing')}}"
        timeout: "00:02:00"
    binary_sensor:
      - name: YouTube 20 minutes
        state: "on"
        auto_off: "00:20:00"

Then OP could just trigger based on the binary sensor going from on to off.

1 Like

Well, I don’t have first hand experience with this. The documentation explicitly give an “attribute” + “for” example, but you might be right. To be tested, I guess.

automation:
  trigger:
    - platform: state
      entity_id: climate.living_room
      attribute: hvac_action
      to: "heating"
      for: "00:10:00"
1 Like

I tried this one, but I changed to netflix and some times just to see how it responds but it is still “unknown”

template:
  - trigger:
      - platform: state
        attribute: source
        to: "Netflix"
        entity_id: media_player.android_tv
    action:
      - alias: Buffer in case someone left a video queued and YouTube starts playing it or ads
        delay: 15
      - alias: Wait for a video to be chosen or 2 minutes of browsing
        wait_template: "{{ is_state('media_player.android_tv', 'playing')}}"
        timeout: "00:00:30"
    binary_sensor:
      - name: YouTube 20 minutes
        state: "on"
        auto_off: "00:05:00"

Not sure how to fix this…

Did you make sure to select Netflix after you instantiated the sensor?

I did test it a couple times before posting the config and it worked on my instance…

@koying

You were correct… I ran a few tests and it seems that the behavior of for with attributes has changed and I just missed the memo.

2 Likes

I’ll do more test when I get home, right now testing from work while my kids are watching TV back home :slight_smile:

So if for is working as it should, then no need for template sensor?
E.g. as posted before:
trigger:


  - platform: state
    entity_id: sensor.android_tv
    attribute: source
    to: YouTube
    for: "00:20:00"

No you won’t need the template sensor I originally proposed and the trigger for your automation would change to:

trigger:
  - platform: state
    entity_id: binary_sensor.youtube_20_minutes
    from: 'on'
    to: 'off'
1 Like

Thank you, so this sensor seems to work now:

template:
  - sensor:
      - name: Android TV Source
        state: "{{ state_attr('media_player.example', 'source') }}"

I get the correct value back now e.g. “Netflix”

So lets see if I can properly trigger on it :slight_smile: