mirror of
https://github.com/home-assistant/home-assistant.io.git
synced 2025-07-19 15:26:59 +00:00
Add SQL example to get former value (#19105)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io> Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
parent
3dcf4c126b
commit
a4ba5c934a
@ -106,10 +106,19 @@ Note that the SQL sensor state corresponds to the last row of the SQL result set
|
|||||||
|
|
||||||
### Previous state of an entity
|
### Previous state of an entity
|
||||||
|
|
||||||
This example only works with *binary_sensors*:
|
Based on previous example with temperature, the query to get the former state is :
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
SELECT * FROM states WHERE entity_id = 'binary_sensor.xyz789' GROUP BY state ORDER BY last_changed DESC LIMIT 1;
|
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;
|
||||||
|
```
|
||||||
|
Thus in yaml
|
||||||
|
```yaml
|
||||||
|
# Example configuration.yaml
|
||||||
|
sensor:
|
||||||
|
- platform: sql
|
||||||
|
queries:
|
||||||
|
- name: Former_Temperature_In
|
||||||
|
query: "SELECT * FROM (SELECT state FROM states WHERE entity_id = 'sensor.temperature_in' ORDER BY state_id DESC LIMIT 2) two_entity ORDER BY state_id ASC LIMIT 1;"
|
||||||
|
column: "state"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Database size
|
### Database size
|
||||||
|
Loading…
x
Reference in New Issue
Block a user