Can someone help me get started with building API sensors?

Running Docker on a pi.

I’m new to home assistant, but not to home automation in general. I’ve gotten a ton done with NodeRed and am very proficient in SQL (for work). I’ve been really trying to understand the documentation, but I don’t fully get what goes where. All the documentation keeps telling me to update my configuration.yaml, but it’s not always obvious which one (I don’t think it’s actually the home_assistant/configuration.yaml, but rather it means “a” configuration file that is written in yaml.

Anyway, I’m struggling following this post here:

Continuing the discussion from UK (and worldwide) Pollen Count using tomorrow.io API:

my configuration currently looks like this:

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
#sensor: !include sensors.yaml
rest:
 - resource: https://api.tomorrow.io/v4/timelines?location=[MY lat/long]&fields=temperature&timesteps=1h&units=metric&apikey=[my API key]&fields=treeIndex,grassIndex,weedIndex tomorrowio_pollen_api
   scan_interval: 600
   sensor:
   ### Pollen - Tree Index
     - name: "Pollen - Grass Index - Day 0"
       value_template: "{{ value_json.data.timelines[0].intervals[0]['values']['grassIndex'] }}"
       json_attributes_path: "$.data.timelines[0].intervals[0].values"
       json_attributes:
         - treeIndex
         - weedIndex
         - temperature
     - name: "Pollen - Grass Index - Day 1"
       value_template: "{{ value_json.data.timelines[0].intervals[1]['values']['grassIndex'] }}"
       json_attributes_path: "$.data.timelines[0].intervals[1].values"
       json_attributes:
         - treeIndex
         - weedIndex
         - temperature
     - name: "Pollen - Grass Index - Day 2"
       value_template: "{{ value_json.data.timelines[0].intervals[2]['values']['grassIndex'] }}"
       json_attributes_path: "$.data.timelines[0].intervals[2].values"
       json_attributes:
         - treeIndex
         - weedIndex
         - temperature
     - name: "Pollen - Grass Index - Day 3"
       value_template: "{{ value_json.data.timelines[0].intervals[3]['values']['grassIndex'] }}"
       json_attributes_path: "$.data.timelines[0].intervals[3].values"
       json_attributes:
         - treeIndex
         - weedIndex
         - temperature
     - name: "Pollen - Grass Index - Day 4"
       value_template: "{{ value_json.data.timelines[0].intervals[4]['values']['grassIndex'] }}"
       json_attributes_path: "$.data.timelines[0].intervals[4].values"
       json_attributes:
         - treeIndex
         - weedIndex
         - temperature

IF I check my config, everything is good. When I uncomment #sensor, it breaks. In my sensors.yaml file (in the main /home_assistant/ folder), I get:

Invalid config for [sensor]: required key not provided @ data['platform']. Got None. (See /config/configuration.yaml, line 12).

Which I interpret to mean that it thinks my sensors.yaml file is written incorrectly.

Here’s what I have in that:

template:
  - sensor:
      - name: Pollen - Grass
        unique_id: sensor.pollen_grass
        state: "{{ states('sensor.pollen_grass_index_day_0') }}"
        icon: 'mdi:grass'
        attributes:
          Today: "{{ states('sensor.pollen_grass_index_day_0') }}"
          Tomorrow: "{{ states('sensor.pollen_grass_index_day_1') }}"
          Today + 2: "{{ states('sensor.pollen_grass_index_day_2') }}"
          Today + 3: "{{ states('sensor.pollen_grass_index_day_3') }}"
          Today + 4: "{{ states('sensor.pollen_grass_index_day_4') }}"
      - name: Pollen - Tree
        unique_id: sensor.pollen_tree
        state: "{{ state_attr('sensor.pollen_grass_index_day_0', 'treeIndex') }}"
        icon: 'mdi:tree'
        attributes:
          Today: "{{ state_attr('sensor.pollen_grass_index_day_0', 'treeIndex') }}"
          Tomorrow: "{{ state_attr('sensor.pollen_grass_index_day_1', 'treeIndex') }}"
          Today + 2: "{{ state_attr('sensor.pollen_grass_index_day_2', 'treeIndex') }}"
          Today + 3: "{{ state_attr('sensor.pollen_grass_index_day_3', 'treeIndex') }}"
          Today + 4: "{{ state_attr('sensor.pollen_grass_index_day_4', 'treeIndex') }}"
      - name: Pollen - Weed
        unique_id: sensor.pollen_weed
        state: "{{ state_attr('sensor.pollen_grass_index_day_0', 'weedIndex') }}"
        icon: 'mdi:spa'
        attributes:
          Today: "{{ state_attr('sensor.pollen_grass_index_day_0', 'weedIndex') }}"
          Tomorrow: "{{ state_attr('sensor.pollen_grass_index_day_1', 'weedIndex') }}"
          Today + 2: "{{ state_attr('sensor.pollen_grass_index_day_2', 'weedIndex') }}"
          Today + 3: "{{ state_attr('sensor.pollen_grass_index_day_3', 'weedIndex') }}"
          Today + 4: "{{ state_attr('sensor.pollen_grass_index_day_4', 'weedIndex') }}"

I’m just not sure how to debug this and what exactly is broken. I really have tried researching this, but the different docs don’t tell me where to put things, they just say "edit your config.yaml". Could someone point me in the right direction? Thank you so much!

You are very close, which, as you can’t tell, doesn’t really count. :joy:

Those include files are basically glued together at runtime, so what’s in each file has to make sense if you just copy and paste it into the main file. In the case of the sensor domain, it needs to look like this if it was all in the main file:

sensor:
  - platform: template
    sensors:
      normaltime:
        friendly_name: "Time - Normal"
        value_template: >
          {{ (now().strftime('%I')|int)~now().strftime(':%M') }}

When you use the includes format, your sensors.yaml file would need to look something like this:

- platform: template
  sensors:
    normaltime:
      friendly_name: "Time - Normal"
      value_template: >
        {{ (now().strftime('%I')|int)~now().strftime(':%M') }}

I think if you change the:

template:

in your sensors.yaml to:

- platform: template

and then in the line right under that change:

sensor:

to:

sensors:

it will work. I’m a little fuzzy on the accepted format for sensors, so you might also need to change:

- name: Pollen - Grass

to:

pollengrass:
  friendly_name: "Pollen - Grass"

and then have the rest of the sensor under that.

No, do not do that.

That would give you sensors in the old format. It is recommended that you use the new format, which you have done but in the wrong place.

template: is an integration like sensor: so it gets it’s own section in your configuration.yaml file, it does not go under sensor: (or in its include file).

Just move the entire template: block out of your sensors.yaml file and put it in your configuration.yaml file.

There are posts on the forum about splitting your config to have templates in their own file or files if you want (e.g. templates.yaml).

Sorry about that. Can you share where the documentation is for the new format? The forum and the documentation is so cluttered with examples that aren’t current, it’s hard to tell what is “right” and what isn’t.

1 Like

Thank you both, I’ll work on trying to get this in, but I did end up getting things to work rather well through node red, so I might take a bit to update if I end up getting this working the way I want.