-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SQL Sensor Component UnboundLocalError when no data returned. #12856
Comments
I've fixed the error in the logs only. We use an ORM (sqlalchemy) that can do some caching between python and the actual database. The issue at hand is clearly that your SQL query returns no results. With the fix, it will return None. |
@dgomes Thank you very much for replying and fixing.. What did you mean fixed in logs only? Is there a way to test SQL queries quickly against SQLAlchemy cache rather than directly against MariaDB? (without writing python code ). What is a general problem with query that return no results? I guess some queries will return results sometimes and sometimes will not. Do we need to make sure that query always return results for SQL sensor? What I am basically trying to do is to get the avg numerical value of a sensor for the last 20 mins. If SQLAlchemy does not flush this data to DB quick enough, does that means that my query will ALWAYS be empty and it is not possible to use SQL sensor in the way I intent?
What are the caching parameters at the moment? Do you configure to buffer by memory size or by time? ie: what are the approximate rules that guide when it will flush data to DB? Is there a command-line tool I could use to: Is moving MariaDB to external location like AWS cloud, be a bad idea? Since it seem SQLAlchemy maintain cache locally inside the container running on Pi and would have to talk to a remote database. |
@B1tMaster SQL sensor was not intended as a tool to query HASS internals, but to monitor other databases, so it's not except that this use case would work. What I fixed: it will no longer raise an exception and show an error message (it will return None and show a warning in the logs). This means that it is now acceptable to have queries which return no results. All your questions about SQLalchemy are out of scope of HASS, they are internal to SQLalchemy. In HASS code we are actually flushing things quite fast. To solve your use case: there is currently a PR for a history_average sensor (#10717) and the filter_sensor was recently accepted for next version (but is not currently time bound). |
Make sure you are running the latest version of Home Assistant before reporting an issue.
You should only file an issue if you found a bug. Feature and enhancement requests should go in the Feature Requests section of our community forum:
Home Assistant release (
hass --version
):version 0.64.2
Python release (
python3 --version
):bash-4.4# python3 --version
Python 3.6.3
Component/platform:
SQL Sensor
File "/usr/lib/python3.6/site-packages/homeassistant/components/sensor/sql.py", line 137, in update
if data is None:
UnboundLocalError: local variable 'data' referenced before assignment
Description of problem:
When SQL sensor returns NO data, the UnboundLocalError seem to happen.
i suspect there maybe multiple problems here.
what is very strange is that my sensors on HA GUI are updating.. Both the original Wunderground sensor and my SQL sensor which is based on wunderground sensors. They update in the GUI, which leads me to believe that data is coming and sqlalchemy maybe caching data in memory and does not flush it to MariaDB for some time.. I am not sure about it, but would like to find out if this is actually the case. I had a discussion on this topic on the forum and it's really strange. Maybe recorder is losing some data and does not write it to DB? But that may cause the real issue that I am reporting now.
My discussion on the potential issue with a recorder to MariaDB here:
https://community.home-assistant.io/t/how-often-recorder-flush-data-to-database-seem-like-a-significant-delay/45160/3
And then this SQL sensor error potentially happening.
The SQL queries I am running:
This is the SQL sensor I have:
Expected:
empty or None values should be handled by the SQL sensor.
Problem-relevant
configuration.yaml
entries and steps to reproduce:Traceback (if applicable):
Additional info:
The text was updated successfully, but these errors were encountered: