First of all, this is a fantastic integration. I’m using it to pull energy usage statistics for a given date range.
I wrote a complex query in my automation using the SQL WITH clause against MariaDB. It always returns an empty response.
The query runs fine directly on the database. Below is an example of this type of query (this simple example shows the issue). The “response” is always empty though.
description: >-
Test SQL With Clause
mode: single
trigger:
- platform: state
entity_id:
- input_button.run_energy_cost_report_button
condition: []
action:
- service: mysql_query.query
data:
query: |
WITH ABC AS (SELECT 'Hello' FROM DUAL)
SELECT * FROM ABC
response_variable: response
- service: input_text.set_value
data:
value: |
{{ response }}
target:
entity_id: input_text.sql_result
I’m not sure if this is a bug? I’ve since re-written my query to use a sub-select, instead of the WITH clause.
On another note… I don’t think the next issue is an integration bug, but maybe the way Home Assistant works. For testing, I’m trying to set the same input text helper, called input_text.sql_result, (configured for a max of 3000 characters) with the result of a query, and the set_value service is not storing the result in the input text helper.
I’m not sure if Home Assistant can’t handle the length, newlines, special characters, etc. Below is the SQL response JSON I’m trying to set, which is returned from my query:
{'result': [{'date': datetime.date(2024, 5, 29), 'datetime_local':
datetime.datetime(2024, 5, 29, 22, 0, 10, 354330), 'latest_created_ts':
1717038010.3387063, 'statistics_id': 17, 'stat_id_name':
'sensor.4244_consumed_energy_kwh_cost', 'total_sum': 0.41204799999999797,
'mean': None, 'min': None, 'max': None, 'last_reset': None,
'last_reset_ts': 1717032229.294907, 'state': 0.263755999999998, 'sum':
0.41204799999999797}, {'date': datetime.date(2024, 5, 30),
'datetime_local': datetime.datetime(2024, 5, 30, 0, 0, 10, 346312),
'latest_created_ts': 1717041610.3463125, 'statistics_id': 1197,
'stat_id_name': 'sensor.consumed_energy_kwh_cost', 'total_sum':
1.421792, 'mean': None, 'min': None, 'max': None, 'last_reset': None,
'last_reset_ts': 1717032229.294907, 'state': 1.2735, 'sum': 1.421792}]}
I would think that any text can be stored as long as it doesn’t exceed the size limit?