Beschreibung |
SQL-Query für die SQL-Integration, als Spalte in der Integration wird "val" über "as val" vorgegeben: |
Delta-Wert (Produktion) eines Energy-Sensors gestern (aus der LTS-Statistik-Tabelle gelesen) |
SELECT (Max(state) - Min(state)) as val FROM statistics
WHERE metadata_id = (SELECT id FROM statistics_meta WHERE statistic_id = 'sensor.household_energy')
AND state NOT IN ('unknown', '', 'unavailable')
AND strftime('%Y%m%d',datetime(created_ts, 'unixepoch','localtime')) == strftime('%Y%m%d', datetime('now','localtime','-1 days'))
|
Delta-Wert (Produktion) eines Energy-Sensors heute bis jetzt (aus der Statistik-Short_Term-Tabelle gelesen) |
SELECT (Max(state) - Min(state)) as val
FROM statistics_short_term
WHERE metadata_id = (SELECT id FROM statistics_meta WHERE statistic_id = 'sensor.household_energy')
AND state NOT IN ('unknown', '', 'unavailable')
AND strftime('%Y%m%d',datetime(created_ts, 'unixepoch','localtime')) == strftime('%Y%m%d', datetime('now','localtime'))
|
Delta-Wert (Produktion) eines Energy-Sensors gestern bis zur selben Zeit (aus der Statistik-Short_Term-Tabelle gelesen) |
SELECT (Max(state) - Min(state)) as val
FROM statistics_short_term
WHERE metadata_id = (SELECT id FROM statistics_meta WHERE statistic_id = 'sensor.household_energy')
AND state NOT IN ('unknown', '', 'unavailable')
AND strftime('%Y%m%d',datetime(created_ts, 'unixepoch','localtime')) == strftime('%Y%m%d', datetime('now','localtime','-1 days'))
AND strftime('%H%M%S',datetime(created_ts, 'unixepoch','localtime')) < strftime('%H%M%S', datetime('now','localtime'))
|
Delta-Wert (Produktion) eines Energy-Sensors gestern zur selben Zeit bis Tagesende: z.B. PV-Produktion verbleibend gestern zur selben Zeit (aus der Statistik-Short_Term-Tabelle gelesen) |
SELECT (Max(state) - Min(state)) as val
FROM statistics_short_term
WHERE metadata_id = (SELECT id FROM statistics_meta WHERE statistic_id = 'sensor.pv_panels_energy')
AND state NOT IN ('unknown', '', 'unavailable')
AND strftime('%Y%m%d', datetime(created_ts, 'unixepoch', 'localtime')) == strftime('%Y%m%d', datetime('now', 'localtime', '-1 days'))
AND strftime('%H%M%S', datetime(created_ts, 'unixepoch', 'localtime')) > strftime('%H%M%S', datetime('now', 'localtime', '-1 days'));
|
Delta-Wert (Produktion) eines Energy-Sensors gestern zur selben Zeit bis Tagesende, aus der States-Tabelle gelesen |
SELECT (Max(state) - Min(state)) as val
FROM states
WHERE metadata_id = (SELECT metadata_id FROM states_meta WHERE entity_id = 'sensor.pv_panels_energy')
AND state NOT IN ('unknown', '', 'unavailable')
AND strftime('%Y%m%d', datetime(last_updated_ts, 'unixepoch', 'localtime')) == strftime('%Y%m%d', datetime('now', 'localtime', '-1 days'))
AND strftime('%H%M%S', datetime(last_updated_ts, 'unixepoch', 'localtime')) > strftime('%H%M%S', datetime('now', 'localtime'));
|
gestern: kleinster Wert eines Sensors (aus der States-Tabelle gelesen) |
SELECT MIN(state) AS val
FROM states
WHERE metadata_id = (SELECT metadata_id FROM states_meta WHERE entity_id = 'sensor.byd_available_capacity')
AND state NOT IN ('unknown', '', 'unavailable')
AND strftime('%Y-%m-%d', last_updated_ts, 'unixepoch', 'localtime') = strftime('%Y-%m-%d', 'now', 'localtime', '-1 day')
|
kleinster Wert, z. B. der tiefste Batterieladestatus von gestern. |
SELECT CAST(state AS INTEGER) AS val
FROM states
WHERE metadata_id = (SELECT metadata_id FROM states_meta WHERE entity_id = 'sensor.byd_battery_box_premium_hv_ladezustand')
AND state NOT IN ('unknown', '', 'unavailable')
AND strftime('%Y%m%d',datetime(last_updated_ts, 'unixepoch','localtime')) == strftime('%Y%m%d', datetime('now','localtime','-1 days'))
ORDER BY val
LIMIT 1;
Für den Ladezustand meiner Batterie (in Prozent) funktionierte weder Max, noch ein normales Order, da z.B. 100 kleiner als 42.2 interpretiert wurde. Als Lösung habe ich state über CAST als Integer interpretiert, sortiert und mit limit 1 wird dann der kleinste Wert ausgegeben.
|
Batterieladestatus gestern zur selben Zeit |
SELECT state as val
FROM states
WHERE metadata_id = (SELECT metadata_id FROM states_meta WHERE entity_id = 'sensor.byd_battery_box_premium_hv_ladezustand')
AND state NOT IN ('unknown', '', 'unavailable')
AND strftime('%Y%m%d',datetime(last_updated_ts, 'unixepoch','localtime')) == strftime('%Y%m%d', datetime('now','localtime','-1 days'))
AND strftime('%H%M%S', datetime(last_updated_ts, 'unixepoch', 'localtime')) > strftime('%H%M%S', 'now', 'localtime')
ORDER BY last_updated_ts
LIMIT 1;
|
letzter Zeitpunkt eines Sensors mit einem bestimmten Wert als Unix Timestamp |
SELECT last_updated_ts AS val
FROM states
WHERE metadata_id = (SELECT metadata_id FROM states_meta WHERE entity_id = 'sensor.fronius_storage_chast')
AND state = 'FULL'
ORDER BY last_updated_ts DESC
LIMIT 1;
|
letzter Zeitpunkt eines Sensors mit einem bestimmten Wert als Datum / Uhrzeit |
SELECT datetime(s.last_updated_ts, 'unixepoch', 'localtime') as val
FROM states s
JOIN states_meta sm ON s.metadata_id = sm.metadata_id
WHERE sm.entity_id = 'sensor.fronius_storage_chast'
AND s.state = "FULL"
ORDER BY s.last_updated_ts DESC
LIMIT 1
|
Delta-Wert (Produktion) eines Energy-Sensors seit dem ein anderen Sensors zuletzt einen bestimmten Wert hatte (aus der Statistik-Short_Term-Tabelle gelesen)
|
WITH last_full_ts AS (
SELECT last_updated_ts
FROM states
WHERE metadata_id = (SELECT metadata_id FROM states_meta
WHERE entity_id = 'sensor.fronius_storage_chast')
AND state = 'FULL'
ORDER BY last_updated_ts DESC
LIMIT 1
)
SELECT (MAX(state) - MIN(state)) AS val
FROM statistics_short_term
WHERE metadata_id = (SELECT id
FROM statistics_meta
WHERE statistic_id = 'sensor.household_energy')
AND created_ts BETWEEN (SELECT last_updated_ts FROM last_full_ts)
AND unixepoch('now')
|
Maximalwert eines Sensors in den letzten 10 Tagen bis zur aktuellen Uhrzeit |
SELECT
MAX(CAST(s.state AS FLOAT)) AS val,
datetime(s.last_updated_ts, 'unixepoch', 'localtime') AS last_updated
FROM
states s
JOIN
states_meta sm ON s.metadata_id = sm.metadata_id
WHERE
sm.entity_id = 'sensor.pv_panels_energy_today'
AND s.state NOT IN ('unknown', 'unavailable')
AND strftime('%H%M%S',datetime(last_updated_ts, 'unixepoch','localtime')) < strftime('%H%M%S', datetime('now','localtime'))
GROUP BY
last_updated;
|
Mindestwert eines Sensors in den letzten 10 Tagen bis zur aktuellen Uhrzeit |
SELECT
MIN(CAST(state AS FLOAT)) AS val,
datetime(last_updated_ts, 'unixepoch', 'localtime') AS last_updated
FROM
states
WHERE
metadata_id = (SELECT metadata_id FROM states_meta WHERE entity_id = 'sensor.pv_panels_energy_yesterday_remaining')
AND state NOT IN ('unknown', 'unavailable')
and strftime('%H%M%S',datetime(last_updated_ts, 'unixepoch','localtime')) < strftime('%H%M%S', datetime('now','localtime'))
GROUP BY
last_updated;
|
Durchschnittswert eines Sensors gestern |
SELECT AVG(state) AS val
FROM states
WHERE metadata_id = (SELECT metadata_id FROM states_meta WHERE entity_id = 'sensor.cloud_coverage_daylight')
AND state NOT IN ("unknown", "unavailable", "")
AND strftime('%Y%m%d',datetime(last_updated_ts, 'unixepoch','localtime')) == strftime('%Y%m%d', datetime('now','localtime','-1 days'))
|
maximaler Unterschied eines Summen-Sensors: der letzten 10 Tage |
WITH RECURSIVE dates(date) AS (
SELECT datetime('now', '-1 day','localtime')
UNION ALL
SELECT datetime(date, '-1 day','localtime')
FROM dates
WHERE date > datetime('now', '-10 day','localtime')
)SELECT strftime('%Y%m%d',date), (Max(state) - Min(state)) as val
FROM states, dates
WHERE metadata_id = (SELECT metadata_id FROM states_meta WHERE entity_id = 'sensor.pv_panels_energy')
AND state NOT IN ('unknown', '', 'unavailable')
AND strftime('%Y%m%d', datetime(last_updated_ts, 'unixepoch', 'localtime')) == strftime('%Y%m%d', date)
GROUP BY date order by val DESC limit 1
Der Minimalwert kann durch das Entfernen von "DESC" in order by entfernt werden
|
Mehrere Werte als csv-String, damit dieser in HA in einem Template weiterverarbeitet werden kann. |
SELECT
GROUP_CONCAT(localdate || ":" || state, ",") AS val,
localdate
FROM (
SELECT
ROUND(state, 2) AS state,
DATE(datetime(last_updated_ts, 'unixepoch', 'localtime')) AS localdate
FROM
states
WHERE
metadata_id = (
SELECT metadata_id
FROM states_meta
WHERE entity_id = 'sensor.pv_panels_energy_yesterday'
)
AND state NOT IN ("unknown", "", "unavailable")
AND CAST(strftime('%H%M', datetime(last_updated_ts, 'unixepoch', '+2 hours')) AS FLOAT) > CAST(strftime('%H%M', date('now', '+2 hours')) AS FLOAT)
GROUP BY
localdate
ORDER BY
localdate DESC
)
|
Mehrere Werte kombiniert mit den Werten eines zweiten Sensors ausgegeben als csv-String, damit dieser in HA in einem Template weiterverarbeitet werden kann. |
WITH energy_data AS (
SELECT
date(datetime(h.last_updated_ts, 'unixepoch', 'localtime'), 'localtime', '-1 days') AS localdate,
round(h.state, 2) AS energy
FROM states h
WHERE
metadata_id = (SELECT metadata_id FROM states_meta WHERE entity_id = 'sensor.pv_panels_energy_yesterday')
AND state NOT IN ("unknown", "", "unavailable")
AND CAST(strftime('%H%M', datetime(last_updated_ts, 'unixepoch', '+121 minutes')) AS FLOAT) > CAST(strftime('%H%M', date('now', '+121 minutes')) AS FLOAT)
GROUP BY localdate
),
cloud_coverage_data AS (
SELECT
date(datetime(c.last_updated_ts, 'unixepoch', 'localtime'), 'localtime', '-1 days') AS localdate,
round(c.state, 2) AS cloudcoverage
FROM states c
WHERE
metadata_id = (SELECT metadata_id FROM states_meta WHERE entity_id = 'sensor.cloud_coverage_daylight_yesterday_avg')
AND state NOT IN ("unknown", "", "unavailable")
AND CAST(strftime('%H%M', datetime(last_updated_ts, 'unixepoch', '+2 hours')) AS FLOAT) > CAST(strftime('%H%M', date('now', '+2 hours')) AS FLOAT)
)
SELECT
GROUP_CONCAT(REPLACE(e.localdate, "-", "") || ":" || e.energy || ":" || c.cloudcoverage, ",") AS val
FROM
energy_data e
LEFT JOIN
cloud_coverage_data c ON e.localdate = c.localdate
ORDER BY
e.localdate DESC;
|