SQL Sensor not working

I guess there are some smart people here who can help me out.
I am trying to create a sql sensor. First approach was the following in the configuration.yaml:

  # Sensors for energy consumption per hour
  - platform: sql
    scan_interval: 600
    #db_url: !secret db_url
    queries:
      - name: Electricity usage per hour
        unique_id: electricity_usage_per_hour
        column: "hourly_averages"
        unit_of_measurement: kWh
        query: |
          SELECT AVG(state) as total_average, GROUP_CONCAT(state ORDER BY hour) as hourly_averages
             FROM (SELECT ROUND(AVG(state), 0) as state, strftime("%H",datetime(last_updated_ts,"unixepoch")) as hour
                 FROM states INNER JOIN states_meta ON states_meta.metadata_id=states.metadata_id
                 WHERE states_meta.entity_id = 'sensor.p1_meter_5c2faf102094_active_power'
                 GROUP BY strftime("%H",datetime(last_updated_ts,"unixepoch")) 
                 );

However no sensor dit popup after a restart and loading the config.
Then I did read something that in the new format the following configuration should be used:

sql:
  - name: Electricity usage per hour
    unique_id: electricity_usage_per_hour
    column: "hourly_averages"
    unit_of_measurement: kWh
    query: >
      SELECT AVG(state) as total_average, GROUP_CONCAT(state ORDER BY hour) as hourly_averages
         FROM (SELECT ROUND(AVG(state), 0) as state, strftime("%H",datetime(last_updated_ts,"unixepoch")) as hour
             FROM states INNER JOIN states_meta ON states_meta.metadata_id=states.metadata_id
             WHERE states_meta.entity_id = 'sensor.p1_meter_5c2faf102094_active_power'
             GROUP BY strftime("%H",datetime(last_updated_ts,"unixepoch")) 
             );

However that also did not show any sensor.
When I run the SQL query in the SQLite interface it gives me the required output. So I know the query is correct.
What am I missing here?

You must set up SQL sensors from the UI. The YAML option has been deprecated and removed (your log should’ve told you it’s an invalid config option).

You will also need to use an automation to update the sensor on a custom interval. There’s no scan interval option anymore. I cannot remember what the default for this integration is.

Thanks for the response. That actually did the trick indeed.

1 Like

Ah, so I’ve also just fallen into this trap, and no, nothing in the logs about this. That’s what was completely throwing me. I even checked to see I was editing the correct source files since it was just being silently ignored. Thanks for the heads-up on this one!

1 Like

Soo glad I come across this thread, thought I was going bananas! Could not get an SQL Sensor setup using yaml, no log output to indicate any issues, just nothing, very frustrating, hours spent messing around with implementations. I really prefer a code first based approach, not sure why both UI / Config as Code are not supported? Should have the option, especially if it was supported before. Anyhoo! Good to know now…

Why then does the documentation still say to put it in the configuration yaml?

https://www.home-assistant.io/integrations/sql/