Formula One Card

For who used the data from the API for notifications for example.
I use a REST sensor.

The rest sensor:

- platform: rest
  scan_interval: 86400
  resource_template: https://ergast.com/api/f1/{{ now().strftime('%Y') }}.json
  sensor:
    - name: "F1 Schedule"
      json_attributes_path: "$.MRData"
      value_template: "OK"
      json_attributes:
        - "total"
        - "RaceTable"

The sensor for exposing the data:

platform: template
sensors:
  formula_one_next_race:
    friendly_name: 'Formula One Next Race'
    unique_id: 'formula_one_next_race'
    attribute_templates:
      data: >-
        {% set data = namespace(found=False) %}
        {% for race in state_attr("sensor.f1_schedule", "RaceTable").Races if data.found == false -%}  
          {% set datetime = ((race.date + 'T' + race.time) | as_datetime) -%}
          {%- if datetime > now() -%}
            {{ race }}
            {% set data.found = true %}
          {%- endif %}
        {%- endfor %}
      race_name: >-
        {%- if 'data' in this.attributes -%}
          {{ this.attributes.data.raceName }}        
        {%- endif %}
      race_start: >-
        {%- if 'data' in this.attributes -%}     
          {{ ((this.attributes.data.date + 'T' + this.attributes.data.time) | as_datetime) }}        
        {%- endif %}
      first_practice: >-
        {%- if 'data' in this.attributes -%}
          {{ ((this.attributes.data.FirstPractice.date + 'T' + this.attributes.data.FirstPractice.time) | as_datetime) }}        
        {%- endif %}     
      second_practice: >-
        {%- if 'data' in this.attributes -%}
          {{ ((this.attributes.data.SecondPractice.date + 'T' + this.attributes.data.SecondPractice.time) | as_datetime) }}        
        {%- endif %}
      third_practice: >-
        {%- if 'data' in this.attributes -%}
          {{ ((this.attributes.data.ThirdPractice.date + 'T' + this.attributes.data.ThirdPractice.time) | as_datetime) }}        
        {%- endif %}
      qualifying: >-
        {%- if 'data' in this.attributes and 'Qualifying' in this.attributes.data -%}
          {{ ((this.attributes.data.Qualifying.date + 'T' + this.attributes.data.Qualifying.time) | as_datetime) }}          
        {% else %}
          -
        {%- endif %}
      sprint: >-
        {%- if 'data' in this.attributes and 'Sprint' in this.attributes.data -%}
          {{ ((this.attributes.data.Sprint.date + 'T' + this.attributes.data.Sprint.time) | as_datetime) }}          
        {% else %}
          -
        {%- endif %}
    value_template: >-
      on

Most of the if statements are to prevent warnings when HA boots. The sensor isnt available from the start

6 Likes