Skip to content

Instantly share code, notes, and snippets.

@malinovsku
Last active May 6, 2022 17:00
Show Gist options
  • Save malinovsku/cf49a938a3b6524b3e6adfe24722b2b1 to your computer and use it in GitHub Desktop.
Save malinovsku/cf49a938a3b6524b3e6adfe24722b2b1 to your computer and use it in GitHub Desktop.
SELECT
old.state AS old_state
,new.state AS new_state
,old.last_updated AS old_last_updated
,new.last_updated AS new_last_updated
,LAG(old.last_updated,1) OVER (ORDER BY old.last_updated) old_old_last_updated
,new.last_updated - LAG(old.last_updated,1) OVER (ORDER BY old.last_updated) AS diff_time
FROM homeassistant.states AS new
LEFT JOIN homeassistant.states AS old ON (old.old_state_id = new.state_id)
WHERE old.entity_id = 'sensor.sensor' -- сенсор
AND ((old.state::real < 50
AND new.state::real > 50)
OR (old.state::real > 50
AND new.state::real < 50))
AND new.state NOT IN ( 'unavailable', 'unknown' )
AND old.state NOT IN ( 'unavailable', 'unknown' )
ORDER BY new.last_updated DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment