Skip to content

Commit 1891351

Browse files
committed
Fix Status Alert card
1 parent a13c9fe commit 1891351

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed
Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
type: markdown
22
content: >-
33
{%- set stage_sensor = "sensor.load_shedding_stage_eskom" -%}
4-
{%- set area_sensor = "sensor.load_shedding_area_tshwane_3_garsfonteinext8d" -%}
4+
{%- set area_sensor = "sensor.load_shedding_area_tshwane_3_garsfonteinext8" -%}
5+
56
{%- set stage = state_attr(stage_sensor, "stage") or 0 -%}
67
{%- set next_stage = state_attr(stage_sensor, "next_stage") or 0 -%}
78
{%- set next_start = state_attr(stage_sensor, "next_start_time") or 0 -%}
89
{%- set next_end = state_attr(stage_sensor, "next_end_time") or 0 -%}
10+
{%- set next_in = 0 -%}
11+
{%- set next_time = 0 -%}
912
{%- set alert = states(stage_sensor) or 0 -%}
1013
{%- set alert_type = "success" -%}
11-
{% set area_forecast = state_attr(area_sensor, "forecast") or 0 -%}
14+
15+
{% set area_forecast = state_attr(area_sensor, "forecast") or [] -%}
1216
{%- set starts_in = state_attr(area_sensor, "starts_in") or 0 -%}
1317
{%- set ends_in = state_attr(area_sensor, "ends_in") or 0 -%}
1418
1519
{%- if area_forecast -%}
16-
{%- set next_start = area_forecast[0].start_time -%}
17-
{%- set next_end = area_forecast[0].end_time -%}
20+
{%- set next_start = area_forecast[0].start_time or 0 -%}
21+
{%- set next_end = area_forecast[0].end_time or 0 -%}
1822
{%- endif -%}
19-
20-
{%- set next_in = 0 -%}
2123
{%- if is_state(area_sensor, "off") and starts_in and next_start and next_end -%}
2224
{%- set next_in = starts_in if starts_in else 0 -%}
2325
{%- if next_start == 0 or next_end == 0 -%}
2426
{%- set next_time = as_timestamp(next_start, default=0.0) -%}
25-
{%- set alert = "Stage {stage}".format(stage=next_stage) + " starts in {d}d {h}h {m}m ({next})" -%}
27+
{%- set alert = "Stage {stage}".format(stage=next_stage) + " starts in {countdown} ({next})" -%}
2628
{%- elif not stage and starts_in > 1440 -%}
2729
{%- set next_time = as_timestamp(next_start, default=0.0) -%}
2830
{%- set alert = "No Load Shedding" -%}
2931
{%- else -%}
3032
{%- set next_time = as_timestamp(next_start, default=0.0) -%}
31-
{%- set alert = "Load Shedding starts in {d}d {h}h {m}m ({next})" -%}
33+
{%- set alert = "Load Shedding starts in {countdown} ({next})" -%}
3234
{%- endif -%}
3335
{% if next_in > 1440 %}
3436
{%- set alert_type = "success" -%}
@@ -39,15 +41,17 @@ content: >-
3941
{% endif %}
4042
{%- elif is_state(area_sensor, "on") and ends_in -%}
4143
{%- set next_time = as_timestamp(next_end, default=0.0) -%}
42-
{%- set next_in = ends_in if ends_in else 0 -%}
43-
{%- set alert = "Load Shedding ends in {d}d {h}h {m}m ({next})" -%}
44+
{# {%- set next_in = ends_in if ends_in else 0 -%} #}
45+
{%- set alert = "Load Shedding ends in {countdown} ({next})" -%}
4446
{%- set alert_type = "error" -%}
4547
{%- endif -%}
4648
47-
{%- set next_in_sec = timedelta(minutes=next_in).total_seconds() | int(default=0) // 60 -%}
48-
{%- set mins = next_in_sec % 60 -%}
49-
{%- set hrs = next_in_sec // 60 % 24 -%}
50-
{%- set days = next_in_sec // 1440 -%}
51-
{%- set alert = alert.format(d=days, m=mins, h=hrs, next=next_time | timestamp_custom("%H:%M", True, default=0)) -%}
49+
{% set mins = starts_in %}
50+
{% if is_state(area_sensor, "on") %}
51+
{% set mins = ends_in %}
52+
{% endif %}
53+
{% set countdown = "%02dh%02d"|format(mins // 60, mins % 60) %}
54+
55+
{%- set alert = alert.format(countdown=countdown, next=next_time | timestamp_custom("%H:%M", True)) -%}
5256
5357
<ha-alert alert-type="{{ alert_type }}">{{ alert }}</ha-alert>

0 commit comments

Comments
 (0)