Escaping a space in attribute for a custom sensor

Hi,

I am trying to create a custom sensor to pull back some game data from the Xbox - I can see the data I want to pull back in my dev-state - however, there is a space in the attribute:

   - platform: template
     sensors:
       xxxxxx_xbox_playing:
         friendly_name: xxxxxx is playing
         value_template: '{{ states.sensor.xxxxxx.attributes.XboxOne full }}'

I have tried to quote around “states.sensor.xxxxxx.attributes.XboxOne full” but then I just see the entire line in dev-state - what is the easiest way to escape the space between XboxOne and full? - I have also tried %20 - but I still get an error.

Thanks

Check the very bottom of state objects doc
You want

{{ states.sensor.xxxxxx.attributes.["XboxOne full"] }}

Thanks Pete, I’ve tried that, but I get the following error:

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected name or number) for dictionary value @ data['sensors']['xxxxxx_xbox_playing']['value_template']. Got '{{ states.sensor.xxxxxx.attributes.["XboxOne full"] }}'. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/

I’ve tried removing the ‘.’ between attributes and [“XboxOne full”] but then it just returns a blank value.

I think you need to get rid of the “.” after attributes:

{{ states.sensor.xxxxxx.attributes["XboxOne full"] }}

I have tried that also (see above) - but just get a blank value returned :frowning:

I didn’t see that anywhere. All I saw was the one posted by Pete that had the . in it.

Not to worry, threw it in my reply here

OK I just noticed that in your log the error is showing that the part it doesn’t like is {{ states.sensor.xxxxxx.attributes.[“XboxOne full”] }}.

just clarify you are actually putting in a valid sensor that actually exists on your system correct? If you’re really putting in {{ states.sensor.xxxxxx.attributes.[“XboxOne full”] }} do you have a sensor that is actually called “sensor.xxxxxx” that has an attribute of “XboxOne full”? Also capitalization matters, too.

I just tried with one of my sensors in the template editor using the format I posted above and it works fine for me.

ex

HI everyone,

After a deep search and without any good results, I’ve finally decided to ask for some help. I’m also having an attribute with a space and I’m trying to just create a new senssor using this attribute (in my configuration file). I’ve trying several combination but I get all the time an unknown value.

Here is the original senssor and attribute with “space”:

Here is what I get when I check my new senssor value in dev tools:

Here below all the tries I’ve given:

    qnap_djeev_memory_used_gb:
      friendly_name: "Qnap TS-453A used memory in GB rounded"
      unit_of_measurement: "GB"
      value_template: (state_attr('sensor.qnap_djeev_memory_usage', 'Memory Size')
    # {{ states.sensor.qnap_djeev_memory_usage.attributes["Memory Size"] }}
    # ((states.sensor.qnap_djeev_memory_usage.attributes['Memory Size']))
    # "{{ (state_attr('sensor.qnap_djeev_memory_usage', 'Memory Size')) }}"
    # "{{ ((states.sensor.qnap_djeev_memory_usage.attributes['Memory Size'])) }}"

Many thanks if someone can help.

Ok I found the answer to my own question, so I will post it here just in case it can help some one else.

In my case what was wrong all the time was: in my attributes with “space” there is already an unit of measurment and in my template I was forcing to use another unit of measurement “GB”. So this was leading to a crash and I will find it later how to force a unit of measurment.

So the following 2 codes did work for me:

    qnap_djeev_memory_used_gb:
      friendly_name: "Qnap TS-453A used memory in GB rounded"
      value_template: "{{ (state_attr('sensor.qnap_djeev_memory_usage', 'Memory Size')) }}"

or

    qnap_djeev_memory_used_gb:
      friendly_name: "Qnap TS-453A used memory in GB rounded"
      value_template: '{{ states.sensor.qnap_djeev_memory_usage.attributes["Memory Size"] }}'


.
.
.

Also this source helped me:

Again I reply to my previous post.

I have managed to get rid of the original measurment unit by adding [:-3] to the end of my formula as given here:

Now everything is working, even with the measurment_unit. Here below my final code:

 qnap_djeev_memory_used_gb:
   friendly_name: "Qnap TS-453A used memory in GB rounded"
   unit_of_measurement: "GB"
   value_template: "{{ (((state_attr('sensor.qnap_djeev_memory_usage', 'Memory Size'))[:-3] | float) }}"

Now it looks like this