Sensor xbox not created, template wont work

HI,

I’m trying to embellish the Xbox sensor a bit. Below is the code I use for exposing its value and an icon reflecting the state.
The thing is, XBOXonline often doesnt kick-in automatically and the sensor.gamertag isn’t created and not displayed .

Hence, below template sensor isn’t created nicely, and the value and icon template do nothing.

Can I add a check for the existence of sensor.gamertag before it runs the rest if the templates below? Preferably i would like it to show sensor.gamertag with ‘login’ or something like that as state and have the
template sensors display Offline.

  • platform: template
    sensors:
    xboxapi_current:
    value_template: ‘{%- if states.sensor.gamertag.state == “Offline” %}Offline
    {%- elif states.sensor.gamertag.state == “unknown” %}Offline
    {%- else %}
    {{ states.sensor.gamertag.attributes[“XboxOne Full”] }}
    {%- endif %}’
    icon_template: ‘{%- if states.sensor.gamertag.state == “Offline” %}mdi:xbox-controller-off
    {%- elif states.sensor.gamertag.state == “unknown” %}mdi:xbox-controller-off
    {%- else %}mdi:xbox-controller
    {%- endif %}’

this it what it looks like when Offline, which is fine:

26

this what is shows when sensor.gamertag is not created:

18

to be precise: the above has nothing to do with the actual Xbox being on or off.

Xbox on shows fine also:

21

Cheers,
Marius

It would be great if something could be made. I’ve found the xboxapi to be very unreliable. It has never worked for me. It always tells me I have to log in again by going to the website, logging in and getting a new key or something.

Something like what is available for the PS4 would be great. There is a tool that’s used called PS4waker, and it’s great!! Shame there is no official xbox api, or some way to monitor over local network.

been some experimenting, but finally made it to something workable…

06

02

next up, differnetiate between icon and text templates…

Issue at hand is that the sensor template gets its value from whats playing, so the icon and value template will have to take that into account, which makes is less straight forward than simply on/off or home/not_home…
Not even sure which states are to be expeted yet, as Sims4, Starwars, Tomb-raider etc have yet to be implemented…

Anyways,
Cheers,
Marius

How are you getting the icon and text to change colors? Also how did you get the Unknown to show as Offline?

I currently have the following under my sensor

      value_template: '{% if is_state("sensor.gamertag", "Offline") %}Offline{% else %}{{ states.sensor.gamertag.attributes["XboxOne Full"] }}{% endif %}

this is what i have so far, put it all in one package_xbox.yaml .


behind # whats next to explore.

homeassistant:
  customize:
    sensor.gamertag:
      entity_picture: /local/gamertag.png
      templates:
        theme: >
          if (state === 'Online') return 'green'; else return 'red';
        _stateDisplay: if (state === 'Unknown') return 'Offline:n/a'; else return state;

# [47,109,16]
    sensor.xboxapi_current:
      friendly_name: Xbox Currently playing
      templates:
#        rgb_color: >
#            if (state === 'Netflix') return 'red';
#            else if (state === 'Offline') return 'blue'; else return null;
        theme: >
            if (state === 'Netflix') return 'green';
            else if (state === 'Offline') return 'red'; else return null;

    sensor.xboxapi_background:
      friendly_name: Xbox Background
      templates:
        theme: >
          if (state === 'Home') return 'green';
          else if (state === 'Offline') return 'red'; else return null;

    device_tracker.xboxone:
      templates:
        theme: >
          if (state === 'home') return 'green'; else return null;
        _stateDisplay: if (state === 'home') return 'Online'; else return 'Offline';

    device_tracker.xboxone_wifi:
      templates:
        theme: >
          if (state === 'home') return 'green'; else return null;
        _stateDisplay: if (state === 'home') return 'Online'; else return 'Offline';

sensor:
  - platform: xbox_live
    api_key: !secret xbox_api
    xuid: !secret xbox_xuid
    scan_interval: 60

  - platform: template
    sensors:

      xbox_gamertag:
        value_template: !secret xbox_gamertag

      xboxapi_current:
        value_template: >
                  {%- if states.sensor.gamertag %}
                    {% if is_state("sensor.gamertag", "Offline") %}Offline
                    {% elif states.sensor.gamertag.state == 'unknown' %}Unknown
                      {% else %}
                        {{ states.sensor.gamertag.attributes["XboxOne Full"] }}
                    {%- endif %}
                    {% else %}
                    n/a
                  {%- endif %}
        icon_template: >
                   {%- if states.sensor.gamertag %}
                     {% if is_state("sensor.gamertag", "Online") %}
                       {%- if is_state_attr("sensor.gamertag", "XboxOne Full" , "Netflix") %} mdi:netflix 
                         {% else %}mdi:xbox-controller
                       {%- endif %}
                      {% elif is_state("sensor.gamertag", "Offline") %}mdi:xbox-controller-off
                       {% else %}mdi:message-bulleted-off
                     {%- endif %}
                   {%- endif %}

      xboxapi_background:
        value_template: >
                 {%- if states.sensor.gamertag %}
                   {% if is_state("sensor.gamertag", "Offline") %}Offline
                   {% elif states.sensor.gamertag.state == "unknown" %}Unknown
                     {% else %}
                       {{ states.sensor.gamertag.attributes["XboxOne Background"] }}
                   {%- endif %}
                   {% else %}
                    n/a
                 {%- endif %}
        icon_template: >
                   {%- if states.sensor.gamertag %}
                     {% if is_state("sensor.gamertag", "Online") %}
                       {%- if is_state_attr("sensor.gamertag", "XboxOne Background" , "Home") %} mdi:home
                         {% else %}mdi:xbox-controller
                         {%- endif %}
                         {% elif is_state("sensor.gamertag", "Offline") %}mdi:xbox-controller-off
                       {% else %}mdi:message-bulleted-off
                     {%- endif %}
                   {%- endif %}

group:
  xbox:
    name: Xbox
    icon: mdi:xbox
    entities:
      - sensor.gamertag
      - sensor.xboxapi_background
      - sensor.xboxapi_current
      - device_tracker.xboxone
      - device_tracker.xboxone_wifi

Awesome thank you! What do you have under your

  xbox_gamertag:
    value_template: !secret xbox_gamertag

My very secret Gamertag ;-))

But I don’t use that right now so you can skip that if you like. It used it in an attempt to automatically fill in my real gamertag by and substituting {{ gamertag}} everywhere .

Didn’t work yet, needs more study…