mirror of
https://github.com/home-assistant/home-assistant.io.git
synced 2025-07-19 15:26:59 +00:00
Update SQL query examples for schema 40 (#26591)
This commit is contained in:
parent
b4a6b2d3a3
commit
691c673ebb
@ -35,11 +35,26 @@ To enable it, add the following lines to your `configuration.yaml` file (example
|
|||||||
sql:
|
sql:
|
||||||
- name: Sun state
|
- name: Sun state
|
||||||
query: >
|
query: >
|
||||||
SELECT *
|
SELECT
|
||||||
FROM states
|
states.state
|
||||||
WHERE entity_id = 'sun.sun'
|
FROM
|
||||||
ORDER BY state_id
|
states
|
||||||
DESC LIMIT 1;
|
LEFT JOIN state_attributes ON (
|
||||||
|
states.attributes_id = state_attributes.attributes_id
|
||||||
|
)
|
||||||
|
WHERE
|
||||||
|
metadata_id = (
|
||||||
|
SELECT
|
||||||
|
metadata_id
|
||||||
|
FROM
|
||||||
|
states_meta
|
||||||
|
where
|
||||||
|
entity_id = 'sun.sun'
|
||||||
|
)
|
||||||
|
ORDER BY
|
||||||
|
state_id DESC
|
||||||
|
LIMIT
|
||||||
|
1;
|
||||||
column: "state"
|
column: "state"
|
||||||
```
|
```
|
||||||
{% endraw %}
|
{% endraw %}
|
||||||
@ -117,7 +132,23 @@ sensor:
|
|||||||
The query will look like this:
|
The query will look like this:
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
SELECT * FROM states WHERE entity_id = 'sensor.temperature_in' ORDER BY state_id DESC LIMIT 1;
|
SELECT
|
||||||
|
states.state
|
||||||
|
FROM
|
||||||
|
states
|
||||||
|
WHERE
|
||||||
|
metadata_id = (
|
||||||
|
SELECT
|
||||||
|
metadata_id
|
||||||
|
FROM
|
||||||
|
states_meta
|
||||||
|
WHERE
|
||||||
|
entity_id = 'sensor.temperature_in'
|
||||||
|
)
|
||||||
|
ORDER BY
|
||||||
|
state_id DESC
|
||||||
|
LIMIT
|
||||||
|
1;
|
||||||
```
|
```
|
||||||
|
|
||||||
Use `state` as column for value.
|
Use `state` as column for value.
|
||||||
@ -126,7 +157,31 @@ Use `state` as column for value.
|
|||||||
|
|
||||||
Based on previous example with temperature, the query to get the former state is :
|
Based on previous example with temperature, the query to get the former state is :
|
||||||
```sql
|
```sql
|
||||||
SELECT * FROM (SELECT * FROM states WHERE entity_id = 'sensor.temperature_in' ORDER BY state_id DESC LIMIT 2) two_entity ORDER BY state_id ASC LIMIT 1;
|
SELECT
|
||||||
|
states.state
|
||||||
|
FROM
|
||||||
|
states
|
||||||
|
WHERE
|
||||||
|
state_id = (
|
||||||
|
SELECT
|
||||||
|
states.old_state_id
|
||||||
|
FROM
|
||||||
|
states
|
||||||
|
WHERE
|
||||||
|
metadata_id = (
|
||||||
|
SELECT
|
||||||
|
metadata_id
|
||||||
|
FROM
|
||||||
|
states_meta
|
||||||
|
WHERE
|
||||||
|
entity_id = 'sensor.temperature_in'
|
||||||
|
)
|
||||||
|
AND old_state_id IS NOT NULL
|
||||||
|
ORDER BY
|
||||||
|
last_updated_ts DESC
|
||||||
|
LIMIT
|
||||||
|
1
|
||||||
|
);
|
||||||
```
|
```
|
||||||
Use `state` as column for value.
|
Use `state` as column for value.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user