From 88dbe347491f7251bb0635425818b6420756c529 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Fri, 8 Aug 2025 22:11:16 +0000 Subject: [PATCH 1/4] Use template for query in SQL --- source/_integrations/sql.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/_integrations/sql.markdown b/source/_integrations/sql.markdown index 8eee1da5adaa..2193c58bc98d 100644 --- a/source/_integrations/sql.markdown +++ b/source/_integrations/sql.markdown @@ -81,7 +81,7 @@ sql: query: description: An SQL QUERY string, should return 1 result at most. required: true - type: string + type: template column: description: The field name to select. required: true From 690c8279bcbf2552555078e6709c1a611c4f2cb6 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Thu, 6 Nov 2025 12:19:32 +0000 Subject: [PATCH 2/4] Add example --- source/_integrations/sql.markdown | 45 ++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/source/_integrations/sql.markdown b/source/_integrations/sql.markdown index 2193c58bc98d..af448889d77c 100644 --- a/source/_integrations/sql.markdown +++ b/source/_integrations/sql.markdown @@ -149,6 +149,7 @@ The `sql.query` action returns a list of rows, where each row is a dictionary of #### Data type conversion The data returned by the database is converted to be compatible with the action response. The following conversions are applied: + - `Decimal` types are converted to floats. - `Date` and `Datetime` objects are converted to ISO 8601 formatted strings. - `bytes` and `bytearray` are converted to a hexadecimal string prefixed with `0x`. @@ -156,9 +157,10 @@ The data returned by the database is converted to be compatible with the action #### Example -Example of calling the `sql.query` action in an automation: +##### Example of calling the `sql.query` action in an automation: {% raw %} + ```yaml action: sql.query data: @@ -178,11 +180,13 @@ data: 3; response_variable: sun_history ``` + {% endraw %} This would return a result similar to this, which will be stored in the `sun_history` variable: {% raw %} + ```yaml result: - state: below_horizon @@ -245,6 +249,44 @@ LIMIT Use `state` as column for value. +### Amount of state changes since using a template + +This example shows the amount of state changes of the sensor `sensor.temperature_in` +using another sensors state to provide the time window. + +```yaml +sensor: + - platform: random + name: Temperature in + unit_of_measurement: "°C" +``` + +The query will look like this: + +```sql +SELECT + count(state) as changes +FROM + ( + SELECT + states.state + FROM + states + WHERE + metadata_id = ( + SELECT + metadata_id + FROM + states_meta + WHERE + entity_id = 'sensor.temperature_in' + ) + AND last_updated_ts >= strftime('%s','{{ states("sensor.datetime_helper") }}') + ) +``` + +Use `changes` as column for value. + ### Previous state of an entity Based on previous example with temperature, the query to get the former state is : @@ -275,6 +317,7 @@ WHERE 1 ); ``` + Use `state` as column for value. ### State of an entity x time ago From 468f02e3783e920a4eb63a8a058b4532a775f309 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Thu, 6 Nov 2025 12:24:02 +0000 Subject: [PATCH 3/4] Make raw --- source/_integrations/sql.markdown | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/_integrations/sql.markdown b/source/_integrations/sql.markdown index af448889d77c..0b73a26f1c3f 100644 --- a/source/_integrations/sql.markdown +++ b/source/_integrations/sql.markdown @@ -263,6 +263,8 @@ sensor: The query will look like this: +{% raw %} + ```sql SELECT count(state) as changes @@ -285,6 +287,8 @@ FROM ) ``` +{% endraw %} + Use `changes` as column for value. ### Previous state of an entity From 0151ccd9aff1282ad408ae76cf7bc3cfe4785114 Mon Sep 17 00:00:00 2001 From: c0ffeeca7 <38767475+c0ffeeca7@users.noreply.github.com> Date: Fri, 7 Nov 2025 05:16:26 +0100 Subject: [PATCH 4/4] tiny tweak --- source/_integrations/sql.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/_integrations/sql.markdown b/source/_integrations/sql.markdown index 0b73a26f1c3f..342d75f6daed 100644 --- a/source/_integrations/sql.markdown +++ b/source/_integrations/sql.markdown @@ -252,7 +252,7 @@ Use `state` as column for value. ### Amount of state changes since using a template This example shows the amount of state changes of the sensor `sensor.temperature_in` -using another sensors state to provide the time window. +using another sensor's state to provide the time window. ```yaml sensor: