Creating sensor getting data from InfluxDB

I am trying to add energy sensors to Home Assistant. Those sensors will have to be feeded from Influx DB, but it doesn’t work because the new sensor doesn’t appear in any place. What am I doing wrong? I share the code I have added. Thank you in advance!


# Loads default set of integrations. Do not remove.

# Text to speech
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

sensor: !include sensors.yaml

  customize: !include customize.yaml


- platform: influxdb
  api_version: 2
  host: IP
  port: PORT
  token: TOKEN
  organization: SweetHome
  bucket: eSolar
    - name: "pvPower"
      range_start: "-1m"
      unit_of_measurement: Wh
      unique_id: "pvPower"
      query: >
        from(bucket: "eSolar")
          |> range(start: -1m, stop: now())
          |> filter(fn: (r) => r["_measurement"] == "sensor.esolar_Modbus")
          |> filter(fn: (r) => r["_field"] == "loadPower")
          |> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
          |> yield(name: "last")


  last_reset: "1970-01-01T00:00:00+00:00"
  state_class: measurement
  device_class: energy

I add a log

2022-11-30 22:09:03.937 ERROR (MainThread) [homeassistant.setup] Error during setup of component influxdb
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/", line 235, in _async_setup_component
    result = await task
  File "/usr/local/lib/python3.10/concurrent/futures/", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/influxdb/", line 481, in setup
    conf = config[DOMAIN]
KeyError: 'influxdb'
2022-11-30 22:09:04.015 ERROR (MainThread) [homeassistant.setup] Unable to prepare setup for platform influxdb.sensor: Unable to set up component.

Unfortunately this is very limited. You should remove “from(bucket: …” and initiate the query with filter(…), specifying the bucket as I did. Unfortunately I cannot write a script this way, even the “option location = …” seems impossible to set.

- platform: influxdb
  api_version: 2
  ssl: false
  port: 8086
  token: !secret influxdb_token
  organization: MEj
  bucket: home_assistant/autogen
  - name: "Flux Test"
    range_start: "date.truncate(t: now(), unit: 1d)"
      - date
      - join
      - timezone
    query: >
        filter(fn: (r) => r["entity_id"] == "heating_system_kwh")
        |> filter(fn: (r) => r["_field"] == "value")
        |> aggregateWindow(every: 1m, fn: last, createEmpty: false)
        |> last()
        |> keep(columns: ["_value"])
    unit_of_measurement: "kWh"