Ich selbst zapfe dafür den Recorder von HA an, und lass mir die Daten der letzten maximal 25 Beben als “Erdbeben-Logbuch” in einer Markdown-Karte anzeigen (ich habe zwei solcher Logbücher, eins für lokale und eins für globale Erdbeben, also zwei Markdown-Karten). Aussehen tut das am Ende so:
Die Karte selbst ist sehr einfach:
type: markdown
title: Letzte lokale Erdbeben (max. 25)
content: >
{% set raw = state_attr('sensor.erdbeben_log', 'json') %} {% set entries = raw
| from_json if raw else [] %} {% if not entries %} Keine gespeicherten
Erdbeben gefunden. {% else %}
**Anzahl:** {{ entries | count }}
| Zeitpunkt | Mag | Tiefe [km]| Nächste Stadt | Land | Distanz [km] | Richtung
|
|---|---:|---:|---|---|---:|---|
{% for e in entries -%} | {{ e.recorded }} | {{ e.magnitude }} | {{ e.depth }}
| {{ e.nearest_city }} | {{ e.country }} | {{ e.distance }} | {{ e.bearing }}
|
{% endfor %} {% endif %}
und nimmt die Daten eines SQL-basierten Sensors sensor.erdbeben_log, den ich in der SQL-Integration so angelegt habe (das ist der für die lokalen Erdbeben):
SELECT
'OK' AS state,
COALESCE(
'[' || GROUP_CONCAT(event_json, ',') || ']',
'[]'
) AS json
FROM (
SELECT
json_object(
'recorded', COALESCE(
datetime(event_time_raw, 'localtime'),
datetime(last_updated_ts, 'unixepoch', 'localtime')
),
'magnitude', printf('M%.1f', magnitude),
'depth', CASE
WHEN depth IS NULL THEN '-'
ELSE printf('%.1f', depth)
END,
'nearest_city', COALESCE(nearest_city, '-'),
'country', COALESCE(country, '-'),
'distance', CASE
WHEN distance_km IS NULL THEN '-'
ELSE printf('%.1f', distance_km)
END,
'bearing', COALESCE(bearing_text_geo, '-'),
'unid', COALESCE(unid, '-')
) AS event_json
FROM (
WITH ranked AS (
SELECT
s.last_updated_ts,
json_extract(sa.shared_attrs, '$.time_utc_raw') AS event_time_raw,
CAST(s.state AS REAL) AS magnitude,
CAST(json_extract(sa.shared_attrs, '$.depth') AS REAL) AS depth,
CAST(json_extract(sa.shared_attrs, '$.distance_km') AS REAL) AS distance_km,
json_extract(sa.shared_attrs, '$.nearest_city') AS nearest_city,
json_extract(sa.shared_attrs, '$.country') AS country,
json_extract(sa.shared_attrs, '$.bearing_text_geo') AS bearing_text_geo,
json_extract(sa.shared_attrs, '$.unid') AS unid,
ROW_NUMBER() OVER (
PARTITION BY json_extract(sa.shared_attrs, '$.unid')
ORDER BY s.last_updated_ts DESC
) AS rn
FROM states s
JOIN states_meta sm ON s.metadata_id = sm.metadata_id
LEFT JOIN state_attributes sa ON s.attributes_id = sa.attributes_id
WHERE sm.entity_id = 'sensor.letztes_erdbeben'
AND json_extract(sa.shared_attrs, '$.status') = 'active'
AND s.state NOT IN ('unknown', 'unavailable')
AND json_extract(sa.shared_attrs, '$.unid') IS NOT NULL
)
SELECT *
FROM ranked
WHERE rn = 1
ORDER BY COALESCE(event_time_raw, datetime(last_updated_ts, 'unixepoch')) DESC
LIMIT 25
)
);
Wenn du nur Stärke und Ort haben willst, vereinfacht sich das Ganze natürlich sehr.