Parsing XML from roku into a template sensor

I an having some challenges parsing the response from I would like to turn my television off if the roku is idle for 30mins. Kids forget to turn the TV off.

I can get the XML back with the following sensor

  - platform: rest
    name: roku_media_play
    resource: http://192.168.x.x:8060/query/media-player    
      - player
    scan_interval: 15

It returns:

'@error': 'false' '@state': close

Which is what I expect. It converted the xml in json. What I would like is to be able to extract the @state. When it is close or idle for 30 mins, i would automate the turn off of the TV. However when I use the following value template, all i get is “none” no matter what the actual state is

  - platform: template
        friendly_name: "play112"
        entity_id: sensor.roku_media_play        
        value_template: "{{ state_attr('roku_media_play.player', ['@state']) }}" 

I have read just about every post on rest, json and xml I could find, none of the suggestions work…HELP!

There’s a roku integration that brings all this data into home assistant as a media player. Why go through the trouble with rest?

The problem with the integration is that is will show as “playing” whether it is on the roku splash screen, or on the netflex top menu page. What I am hoping to get is if the player is not actively stream a show/movie and idle, shut the TV off. The /query/media-player returns different “states” when the show if play or not…

well, then you’ll have to parse the info out because this is not json.

value_template: >
  {{ states('sensor.roku_media_play').split("'@state': ")[-1] }}

if it was json, it would look like this:

{"@error": "false", "@state": "close" }
1 Like

copy and paste error, this is the output i get…

{’@error’: ‘false’, ‘@state’: ‘close’} does the single quote rather than the double quote change anything

Well, it just means it’s a dictionary, not json.

So with that in mind, this should work:

value_template:  "{{ state_attr('sensor.roku_media_play', 'player')['@state'] }}"

Awesome!!! this works. I now get the state of the roku player and can trigger automations from it!!!

Much appreciated!

Not sure what type of roku you have but for mine if it gets left on long enough in an app menu for the application’s screensaver to appear then the media_player's state switches from playing to idle. Does this not happen for you?

Happens for my system. :man_shrugging:

Not sure if it is the version of the Roku, but mine will stay on play whether it is on the top roku menu , in the stream service menu (ie: netflix or crave etc.) The only way I can tell if it is actually playing is by querying the player with the http://192.168.x.x:8060/query/media-player

if interested this is the output of the device info.:

<model-name>Roku 3</model-name>
<friendly-device-name>Roku 3 - xxxxxxx</friendly-device-name>
<friendly-model-name>Roku 3</friendly-model-name>
<default-device-name>Roku 3 -xxxxxxxxxxxx</default-device-name>

Hm yea it definitely seems like something odd is happening for you. For mine, when I’m in the top Roku menu the state of the media_player is Home not Playing. When I launch any application then the state becomes Playing even if I’m just in the application menu, not watching anything. But if I leave it in an application menu long enough for that application’s screensaver to appear then the state switches from Playing to Idle.

I’d recommend submitting a github issue since it sounds like you’re not seeing either of those behaviors. Might be an issue with how the integration handles your roku model that warrants further investigation.

will do…thanks. i did see others threads with the same issue.