- platform: sql
scan_interval: 180
queries:
- name: vtb_shared_sql
query: "SELECT state,attributes FROM states WHERE entity_id = 'sensor.vtb_shared' and state != 'unknown' and state != 'none' ORDER BY state_id DESC LIMIT 1;"
column: "state"
gives to me non readable text (broken codepage i assume)
Hi.
I’m using sql sensors against a MariaDB database; it works like a charm, but I was wondering if some extensions were possible…
For example, I was looking for a way of running an SQL request with a “calculated” query in an automation
Something like : select value from table where column=entity_id
where entity_id would come from the trigger part of the automation.
Thought I’d try this. I use a mariadb
Getting this error:
2021-02-19 11:08:42 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up sql platform for sensor
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 198, in _async_setup_platform
await asyncio.shield(task)
File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/src/homeassistant/homeassistant/components/sql/sensor.py", line 63, in setup_platform
sess.close()
UnboundLocalError: local variable 'sess' referenced before assignment
Config:
- platform: sql
db_url: secret! sqlurl
queries:
- name: DB size
query: 'SELECT table_schema "database", Round(Sum(data_length + index_length) / 1024, 1) "value" FROM information_schema.tables WHERE table_schema="homeassistant" GROUP BY table_schema;'
column: "value"
unit_of_measurement: kB
I used to be a C developer… I’m not familiar with Python. I have almost no time for me : I’m afraid I won’t be able to do that PR during the next 15 years
Hi, I’m trying to get the average time of the day a sensor turns to on during the day using the sql sensor
I treid various things, but all give me errors. Maybe you can help?
- platform: sql
queries:
- name: schlafenszeit_filter
query: "SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(s.`state`))) FROM states s INNER JOIN (SELECT state_id FROM states WHERE entity_id='input_datetime.schlafenszeit_test' AND state <> 'unavailable' ORDER BY state_id DESC LIMIT 2) x ON x.state_id = s.state_id"
column: 'AVG(s.`state`)'
Unfortunately I don’t know where to start with writing the query, so this is a plea for help. An example would let me start off on figuring this out.
I would like to create a sensor which would get the value of a chosen sensor from 30 days before. It would have to come from the long-term statistics database and I guess should just return the entry in time closest to the current date time minus 30days.