Parsing XML from roku into a template sensor

I an having some challenges parsing the response from HASS.io. 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

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

It returns:

player
'@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
    sensors:
      play:
        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!
Thanks.

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.:

<vendor-name>Roku</vendor-name>
<model-name>Roku 3</model-name>
<model-number>4230X</model-number>
<model-region>US</model-region>
<is-tv>false</is-tv>
<is-stick>false</is-stick>
<supports-ethernet>true</supports-ethernet>
<wifi-mac>xx:xx:xx:xx:xx:xx</wifi-mac>
<wifi-driver>aardvark</wifi-driver>
<has-wifi-extender>false</has-wifi-extender>
<has-wifi-5G-support>true</has-wifi-5G-support>
<can-use-wifi-extender>true</can-use-wifi-extender>
<ethernet-mac>xx:xx:xx:xx:xx:xx</ethernet-mac>
<network-type>wifi</network-type>
<network-name>xxxxxxx</network-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>
<user-device-name/>
<build-number>049.30E04170A</build-number>
<software-version>9.3.0</software-version>
<software-build>4170</software-build>

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.