How to use state attributes in condition?

I have a Sony Android TV with built-in Chromecast. The Chromecast integration provides lot of information about which app is playing content on the TV. I see something like this in the events capture:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "media_player.bedroom_tv",
        "old_state": {
            "entity_id": "media_player.bedroom_tv",
            "state": "playing",
            "attributes": {
                "volume_level": 0,
                "is_volume_muted": false,
                "media_content_id": "",
                "media_duration": 1361.402,
                "media_position": 119.415,
                "media_position_updated_at": "2021-12-11T09:43:41.970265+00:00",
                "media_title": "Myrtle Beach Vacation 1",
                "app_id": "AndroidNativeApp",
                "app_name": "Plex",
                "entity_picture_local": null,
                "friendly_name": "Master Bedroom TV",
                "supported_features": 152463
            }

I want to create different actions in automation depending on the state and app_name. So, if state is playing and app_name is Plex, then do some action. If state is playing but app_name is Netflix then do something else. I can use the state in condition clause of automation and set it to playing but how do I use the app_name attribute?

condition: state
entity_id: media_player.bedroom_tv
state: playing

How do I add the app_name attribute?
Thanks,
Arun

use a template condition:

- condition: template
  value_template: "{{ is_state_attr('media_player.bedroom_tv', 'app_name', 'Plex') }}"

or:

- condition: template
  value_template: "{{ state_attr('media_player.bedroom_tv', 'app_name') == 'Plex') }}"
1 Like

This can also be done using a State or Numeric State condition, by simply adding “attribute”… or using the drop down “Attribute” selector in the UI editor.

condition: state
entity_id: media_player.bedroom_tv
attribute: app_name
state: Plex
2 Likes

I want to be able to check status if my roku is playing, and if it is then do nothing else if not playing then hit select on roku remote to select profile. At the end of the day, i am trying to play a show in the morning for the kids automatically but with disney plus and other apps requiring you to select a profile, I need to hit select if it is asking for a profile. my issue is that it doesnt ALWAYS ask what profile because it will remember. Here is what I been using but has been hit or miss.

alias: Play Bluey On Living Room TV
sequence:

  • service: media_player.play_media
    target:
    entity_id: media_player.woody_s_new_stick_in_the_living_rm
    data:
    media_content_id: ‘291097’
    media_content_type: app
    extra:
    content_id: 655f7202-f7de-4d0b-84cd-2e190aaed851
    media_type: episode
  • delay:
    hours: 0
    minutes: 0
    seconds: 25
    milliseconds: 0
  • service: remote.send_command
    data:
    command: select
    mode: single
    icon: mdi:play-pause

Can you format your code so it is readable please?

Highlight it and click this

image

Your question really has nothing to do with this thread. You’re asking for a condition based on the state of the media_player not one of it’s attributes.

I think standby/home are the correct state for a Roku, at least on my Roku TV it is.

I think I misunderstood the first time I read your question. If you only want to the remote command to run if it’s not playing you don’t need the if / else, just a not condition if this is the last thing to run in the script/automation.

Script snippet.
    - service: media_player.play_media
      target:
        entity_id: media_player.woody_s_new_stick_in_the_living_rm
      data:
        media_content_id: '291097'
        media_content_type: app
        extra:
          content_id: 655f7202-f7de-4d0b-84cd-2e190aaed851
          media_type: episode

    - delay:
        hours: 0
        minutes: 0
        seconds: 25
        milliseconds: 0

    - not:
        - condition: state
          entity_id: media_player.woody_s_new_stick_in_the_living_rm
          state:
            - standby
            - home

    - service: remote.send_command
      data:
        command: select
        mode: single
        icon: mdi:play-pause
alias: Play Bluey On Living Room TV
sequence:
  - service: media_player.play_media
    target:
      entity_id: media_player.woody_s_new_stick_in_the_living_rm
    data:
      media_content_id: '291097'
      media_content_type: app
      extra:
        content_id: 655f7202-f7de-4d0b-84cd-2e190aaed851
        media_type: episode
  - delay:
      hours: 0
      minutes: 0
      seconds: 25
      milliseconds: 0
  - service: remote.send_command
    data:
      command: select
mode: single
icon: mdi:play-pause

Did you click the little arrow beside “Script Snippet”? If I understood what you were asking for correctly, I think I already answered your question.

im a couple months new to any scripting at all but was forced by insteon shutdown. at the end of the day didnt realize how much i was missing. thanks for bareing with me on learning terms liker attributes and coding

No worries. I realized there was an error (I changed something and didn’t finish). Make sure you grab the edited version basically you just need this before the remote.send_commend call.

    - not:
        - condition: state
          entity_id: media_player.woody_s_new_stick_in_the_living_rm
          state:
            - standby
            - home

so the -not will basically say if it is in standby or home then run select command?

How would you do this using av numeric state condition?

I’m trying to read the set temperature from a climate entity, where the entity name is ‘climate.heating_bathroom’ and the attribute is ‘Temperature’. I like to make a condition that checks if the attribute is a certain number, for instance ‘22’, and checing it by using a Value Template.

You will need to clarify exactly what you are asking… and it would be best to start a new topic with all the pertinent details.

Numeric state conditions are used for mathematical comparisons “above/greater than” or “below/less than”. This type of condition can use value templates to modify the value before testing it.

State conditions are used to check if a state or attribute is equal to a given value.

Template conditions can perform both of the types of logical checks described above but use a value template.