Influxdb sensor - simplejson.errors.JSONDecodeError: Expecting value

I’m trying to set up a influxdb sensor, but no matter how I do the configuration, I get this error in home assistant:
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/entity_platform.py”, line 150, in _async_setup_platform
await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
File “/usr/local/lib/python3.7/asyncio/tasks.py”, line 442, in wait_for
return fut.result()
File “/usr/local/lib/python3.7/concurrent/futures/thread.py”, line 57, in run
result = self.fn(*self.args, **self.kwargs)
File “/usr/src/homeassistant/homeassistant/components/influxdb/sensor.py”, line 85, in setup_platform
sensor = InfluxSensor(hass, influx_conf, query)
File “/usr/src/homeassistant/homeassistant/components/influxdb/sensor.py”, line 124, in init
influx.query(“SHOW SERIES LIMIT 1;”)
File “/usr/local/lib/python3.7/site-packages/influxdb/client.py”, line 456, in query
data = response.json()
File “/usr/local/lib/python3.7/site-packages/requests/models.py”, line 897, in json
return complexjson.loads(self.text, **kwargs)
File “/usr/local/lib/python3.7/site-packages/simplejson/init.py”, line 518, in loads
return _default_decoder.decode(s)
File “/usr/local/lib/python3.7/site-packages/simplejson/decoder.py”, line 370, in decode
obj, end = self.raw_decode(s)
File “/usr/local/lib/python3.7/site-packages/simplejson/decoder.py”, line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The config is:

- platform: influxdb
    host: 192.168.2.56
    port: 443
    ssl: true
    verify_ssl: false
    username: dario
    password: !secret influxdb_password_2
    queries:
      - name: power usage it systems
        unit_of_measurement: W
        value_template: '{{ value | round(1) }}'
        group_function: last
        where: '"host" = ''srv-ubn-services-1'''
        measurement: '"ups"'
        field: ups_load_perc
        database: telegraf_data

Any idea what I’m doing wrong?

Looks like you’re trying to define the influxDB platform and the sensor at the same time
You need 2 separate blocks:
influxdb definition (pretty much everything from your example until queries
and a separate block under your sensors definition with
platform: influxdb followed by your block starting with queries:

*** config ***

- platform: influxdb
    host: 192.168.2.56
    port: 443
    ssl: true
    verify_ssl: false
    username: dario
    password: !secret influxdb_password_2

*** sensors ***

sensor:
  - platform: influxdb
    queries:
      - name: power usage it systems
        unit_of_measurement: W
        value_template: '{{ value | round(1) }}'
        group_function: last
        where: '"host" = ''srv-ubn-services-1'''
        measurement: '"ups"'
        field: ups_load_perc
        database: telegraf_data

I have actually used this example from the official documentation:

sensor:
  platform: influxdb
  host: localhost
  username: home-assistant
  password: password
  queries:
    - name: last value of foo
      unit_of_measurement: °C
      value_template: '{{ value | round(1) }}'
      group_function: last
      where: '"name" = ''foo'''
      measurement: '"°C"'
      field: value
      database: db1
    - name: Min for last hour
      unit_of_measurement: '%'
      value_template: '{{ value | round(1) }}'
      group_function: min
      where: '"entity_id" = ''salon'' and time > now() - 1h'
      measurement: '"%"'
      field: tmp
      database: db2

Because the sensor should query a different db then the one HA is pushing to for it’s own sensors.

ok. Only differences I can see with HA’s official example are:

  • your measurement is not in quotes. Don’t think that’ll be an issue, but thought of pointing out just in case
  • you have an extra dash - in front of platform
    if your config is split that should be fine provided you have the correct indentation, if not, that would defo be an issue.
    Have you tried to not use a value_template to see if you get something and maybe your template doesn’t work with the data you get? (e.g. "string" | round(1) appears to convert the string to a number but there may be more at play…)

Thanks for still helping me out, finding the error.

The measurement is in quotes.
Removing the value template doesn’t help either :frowning:

Have you looked at that too?

How do you mean an extra dash?

My actual config is:

- platform: influxdb
    host: 192.168.2.56
    port: 443
    ssl: true
    verify_ssl: false
    username: dario
    password: !secret influxdb_password_2
    queries:
      - name: power usage it systems
        unit_of_measurement: W
        value_template: '{{ value | round(1) }}'
        group_function: last
        where: '"host" = ''srv-ubn-services-1'''
        measurement: '"ups"'
        field: ups_load_perc
        database: telegraf_data

Can you share where that fits in your config?
Indentations are incredibly important in yaml so be sure to double check every space

Sure:

sensors:
- platform: influxdb
  host: 192.168.2.56
  port: 443
  ssl: true
  verify_ssl: false
  username: dario
  password: !secret influxdb_password_2
  queries:
    - name: power usage it systems
      unit_of_measurement: W
      value_template: '{{ value | round(1) }}'
      group_function: last
      where: '"host" = ''srv-ubn-services-1'''
      measurement: '"ups"'
      field: ups_load_perc
      database: telegraf_data

I can’t see anything wrong.
Are you sure your influxdb query is correct?

That’s the query in grafana which work:
SELECT mean(“ups_load_perc”) *2300/100 FROM “ups” WHERE (“host” = ‘srv-ubn-services-1’) AND $timeFilter GROUP BY time($__interval) fill(null)

Have you tried the query directly in the influx CLI?
Here is an example of a query (completely unrelated) that I use and that works in the influx CLI and I notice a few differences in quotations and wording etc.

select difference(last(value)),time from kWh where entity_id = 'sparsnas_energy_consumption_over_time' and time > '2019-01-01' group by time(1w,4d) fill(none)

Also, can you see in the log the resulting query string being sent to influx?

The query is working as expected:
select last(“ups_load_perc”) * 2300/100 FROM “ups” where “host” = ‘srv-ubn-services-1’
name: ups
time last


1573989241000000000 46