Using Attributes in Lovelace

I am trying to modify an existing custom plugin for Strava to download more information rather than the distance I have run this year. At the moment, Lovelace and the History GUI will only display the year to date distance (State) whereas there are attributes with more information.

19%20pm

What I cannot work out is how to access the attributes returned. Or I am returning the wrong information if I want access to this data?

The Lovelace YAML I have tried appears below. The commented lines don’t work. Only sensor.strava_11527827 is working for me. Thanks

  - type: entities
    title: Strava
    entities:
      - sensor.strava_11527827
      #- sensor.strava_11527827.state.attributes["ytd_ride_distance"]
      #- sensor.strava_11527827.attributes["ytd_ride_distance"]
      #- sensor.strava_11527827.ytd_ride_distance
2 Likes

Unfortunately Lovelace doesn’t yet support showing attributes.

Try this

1 Like

You could create a template sensor and extract the attribute you want with that and then you can display in lovelace. I display heaps of sensor attributes in lovelace this way. I am out at the moment but I can show you how later.

2 Likes

You can extract the attributes of your sensor like below by putting this into your configuration.yaml

sensor:
  - platform: template
    sensors: 
      ytd_ride_distance:
        value_template: '{{ states.strava_11527827.attributes.ytd_ride_distance|int }}'  
        friendly_name: 'YTD Ride Distance' 
        unit_of_measurement: 'm'

      ytd_ride_distance:
        value_template: '{{ states.strava_11527827.attributes.all_run_distance|int }}'  
        friendly_name: 'All Run Distance' 
        unit_of_measurement: 'm'

if you want to convert to km you can do this

sensor:
  - platform: template
    sensors: 
      ytd_ride_distance:
        value_template: '{{ (states.strava_11527827.attributes.ytd_ride_distance|int / 1000)|round(2) }}'  
        friendly_name: 'YTD Ride Distance' 
        unit_of_measurement: 'km'

      ytd_ride_distance:
        value_template: '{{ (states.strava_11527827.attributes.all_run_distance|int / 1000)round(2) }}'  
        friendly_name: 'All Run Distance' 
        unit_of_measurement: 'km'
3 Likes

Just to second what @poebae already posted - works great for me:

You can extract the attributes of your sensor like below by putting this into your configuration.yaml

Thanks. I will try that. Might also look at extending the library not to use attributes in this way too

For completeness, here is the relevant part of my configuration.yaml file…

  - platform: template
    sensors: 
      ytd_ride_distance:
        value_template: '{{ (states.sensor.strava_11527827.attributes.ytd_ride_distance | int / 1000 ) | round(2)  }}' 
        friendly_name: 'YTD Ride Distance' 
        unit_of_measurement: 'km'
        icon_template: mdi:bike

And of my ui-lovelace.yaml file

  - type: entities
    title: Strava
    entities:
      - sensor.strava_11527827
      - sensor.ytd_run_distance
      - sensor.ytd_run_time
      - sensor.ytd_ride_distance

I am still working on completing the Strava integration. I will publish it in full when I get it all working.

My current roadblock is that the Strava Python API requires named arguments for get_actiities().

    @asyncio.coroutine
    def async_fetch_stats(self):
        """Fetch athlete statistics from Strava."""
        return self.hass.async_add_job(
            self._client.get_activities, limit=3)

This code fails because hass.async_add_job() does not appear to like named arguments. I am still coming to terms with Python 3, yield, yield from etc… Any ideas on how to add a job with named arguments? Of do I need to edit async_add_job and get that propagated? Thanks.

1 Like

Hi,

I have trouble getting it to work, as my sensor I want to get the states off are binary_sensor.

I tried:
sensor:

 sensor:
   - platform: template
     sensors: 
       530ix_brake_fluid_date:
         value_template: '{{ states.binary_sensor.530i_xdrive_condition_based_services.attributes.brake_fluid_date|int }}'  
         friendly_name: 'Bremseveske Dato' 
         unit_of_measurement: 'date'
 
       530ix_brake_fluid_status:
         value_template: '{{ states.binary_sensor.530i_xdrive_condition_based_services.attributes.brake_fluid_status|int }}'  
         friendly_name: 'Bremseveske Status'

And got this error:
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘i_xdrive_condition_based_services’) for dictionary value @ data[‘sensors’][‘530ix_brake_fluid_date’][‘value_template’]. Got ‘{{ states.binary_sensor.530i_xdrive_condition_based_services.attributes.brake_fluid_date|int }}’ invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘i_xdrive_condition_based_services’) for dictionary value @ data[‘sensors’][‘530ix_brake_fluid_status’][‘value_template’]. Got ‘{{ states.binary_sensor.530i_xdrive_condition_based_services.attributes.brake_fluid_status|int }}’. (See /config/configuration.yaml, line 173). Please check the docs at Template - Home Assistant

you can try:

   - platform: template
     sensors: 
       530ix_brake_fluid_date:
         value_template: '{{ state_attr('binary_sensor.530i_xdrive_condition_based_services', 'brake_fluid_date')|int }}'  
         friendly_name: 'Bremseveske Dato' 
         unit_of_measurement: 'date'
 
       530ix_brake_fluid_status:
         value_template: '{{ state_attr('binary_sensor.530i_xdrive_condition_based_services', 'brake_fluid_status')|int }}'  
         friendly_name: 'Bremseveske Status'

That won’t work, quotes aren’t correct. Notice the coloring and the entire template isn’t red? That means you’re quotes aren’t correct.

Also, I helped @TheStigh with this issue over private messages so he should be all set. The solution ended up being:

  - platform: template
    sensors: 
      bmw_brake_fluid_date:
        value_template: >
          {% set date = states.binary_sensor['530i_xdrive_condition_based_services'].attributes['brake fluid date'] %}
          {{ as_timestamp(strptime(date, '%Y-%m-%d')) | timestamp_custom('%d.%m.%Y') }} 
        friendly_name: 'Neste bremseveske' 
        unit_of_measurement: 'date'

Another nice solution for the OP would be:
secondaryinfo-entity-row

Results look like this:
image
image

Corresponding code:

entities:
  - entity: cover.gaste_wc
    name: Gäste-WC
    type: 'custom:secondaryinfo-entity-row'
    secondary_info: '[[ {entity}.attributes.current_position ]]%'
  # more entities...
title: Rolläden EG
type: entities
entities:
  - entity: climate.viessmann_vitocal_222_s_heating
    name: Heizung
    type: 'custom:secondaryinfo-entity-row'
    secondary_info: >-
      Außen: [[
      {entity}.attributes.outside_temperature
      ]]°C, <br>Vorlauf: [[
      {entity}.attributes.supply_temperature
      ]]°C, <br>Rücklauf: [[
      {entity}.attributes.return_temperature
      ]]°C, <br>Zirkulation: [[
      {entity}.attributes.circulationpump_active
      ]]
title: Viessmann Vitocal 222-S
type: entities
7 Likes