I’ve been stuck for a good 4 hours.
Maybe someone can help and see the problem (?)
- name: "Solarprognose Heute PEAK"
unique_id: "solarprognose_werte_heute_peak"
unit_of_measurement: "kWh"
state: >
{% set raw = state_attr('sensor.solarprognose_raw_json_hourly', 'raw') %}
{% if raw and raw.get('data') %}
{% set data = raw.get('data') %}
{% set start = as_timestamp(utcnow().replace(hour=0, minute=0, second=0, microsecond=0)) | int %}
{% set end = as_timestamp(utcnow().replace(hour=23, minute=59, second=59, microsecond=0)) | int %}
{% set ns = namespace(max_val=0, max_ts=0, count=0) %}
{% for ts, v in data.items() if (ts | int) >= start and (ts | int) <= end %}
{% set ns.count = ns.count + 1 %}
{% if (v[0] | float) > ns.max_val %}
{% set ns.max_val = v[0] | float %}
{% set ns.max_ts = ts | int %}
{% endif %}
{% endfor %}
{{ ns.max_ts }}
{% else %}
unknown
{% endif %}
Returns the timestamp from 1739196000
The code here returns the correct peak of 4.866
- name: "Solarprognose Werte Heute PEAK"
unique_id: "solarprognose_werte_heute_PEAK"
unit_of_measurement: "kWh"
state: >
{% set raw = state_attr('sensor.solarprognose_raw_json_hourly', 'raw') %}
{% if raw and raw.get('data') %}
{% set data = raw.get('data') %}
{% set start = as_timestamp(utcnow().replace(hour=0, minute=0, second=0, microsecond=0)) | int %}
{% set end = as_timestamp(utcnow().replace(hour=23, minute=59, second=59, microsecond=0)) | int %}
{% set ns = namespace(max_val=0) %}
{% for ts, v in data.items() if (ts | int) >= start and (ts | int) <= end %}
{% if v[0] > ns.max_val %}{% set ns.max_val = v[0] %}{% endif %}
{% endfor %}
{{ ns.max_val | round(3) }}
{% else %}
unknown
{% endif %}
The aim is now to combine the timestamp with the peak.
4,866 kWh (1739196000)
(I will convert this into a readable form later)
But as soon as I combine it I get unavailable.
Where is my error?
This is wrong:
- name: "Solarprognose Heute PEAK"
unique_id: "solarprognose_werte_heute_peak"
unit_of_measurement: "kWh"
state: >
{% set raw = state_attr('sensor.solarprognose_raw_json_hourly', 'raw') %}
{% if raw and raw.get('data') %}
{% set data = raw.get('data') %}
{% set start = as_timestamp(utcnow().replace(hour=0, minute=0, second=0, microsecond=0)) | int %}
{% set end = as_timestamp(utcnow().replace(hour=23, minute=59, second=59, microsecond=0)) | int %}
{% set ns = namespace(max_val=0, max_ts=0) %}
{% for ts, v in data.items() if (ts | int) >= start and (ts | int) <= end %}
{% if (v[0] | float) > ns.max_val %}
{% set ns.max_val = v[0] | float %}
{% set ns.max_ts = ts | int %}
{% endif %}
{% endfor %}
{{ ns.max_val | round(3) }} kWh (ca. {{ ns.max_ts | timestamp_custom('%H:%M') }})
{% else %}
unknown
{% endif %}
or
- name: "Solarprognose Heute PEAK"
unique_id: "solarprognose_werte_heute_peak"
unit_of_measurement: "kWh"
state: >
{% set raw = state_attr('sensor.solarprognose_raw_json_hourly', 'raw') %}
{% if raw and raw.get('data') %}
{% set data = raw.get('data') %}
{% set start = as_timestamp(utcnow().replace(hour=0, minute=0, second=0, microsecond=0)) | int %}
{% set end = as_timestamp(utcnow().replace(hour=23, minute=59, second=59, microsecond=0)) | int %}
{% set ns = namespace(max_val=0, max_ts=0) %}
{% for ts, v in data.items() if (ts | int) >= start and (ts | int) <= end %}
{% if (v[0] | float) > ns.max_val %}
{% set ns.max_val = v[0] | float %}
{% set ns.max_ts = ts | int %}
{% endif %}
{% endfor %}
{# Hier wird der Timestamp formatiert – als lokale Zeit (zweiter Parameter true) #}
{% set peak_time = ns.max_ts | timestamp_custom('%H:%M', true) %}
{{ ns.max_val | round(3) }} kWh (ca. {{ peak_time }})
{% else %}
unknown
{% endif %}