wernerhp / ha.integration.load_shedding

A Home Assistant integration to track your load schedding schedule.
MIT License
115 stars 21 forks source link

The starts_in sensor is incorrect when loadshedding is the following day #34

Closed boozelclark closed 1 year ago

boozelclark commented 2 years ago

The starts in sensor only looks at times so it gives an incorrect value in the low stages of loadshedding when an areas next slot is the following day. The delta calculation needs to be between the two datetimes and not just the times.

wernerhp commented 2 years ago

Is this the starts_in in the sensor attributes or the Card's yaml?

boozelclark commented 2 years ago

I think it's both. It may not actually be if the next slot is not the following day but if it is more than 24 hours away. I'll try and replicate this next time my loadshedding schedule is in that state and share more accurate details

gregcsw commented 2 years ago

It appears to be the card's yaml.

It is currently 18:55 on 03 November. The sensor attributes shows 1625 minutes (1d, 3h, 5m). But the cards shows 3h 5m.

I'v spent an hour looking at it but templating skills are not up to fixing this. I narrowed it down to {{ starts_in.seconds }} drops the day and only returns the hours and minutes, But i don;t know how to fix it.

Thanks Werner for this integration it is great!

Screenshot 2022-11-03 at 18 58 29 Screenshot 2022-11-03 at 18 54 44

wernerhp commented 2 years ago

The issue is with .seconds which should be .total_seconds() | int.

Made some tweaks. Let me know if this works.

image image image image

Code ```yaml type: vertical-stack cards: - type: custom:mushroom-chips-card chips: - type: entity entity: sensor.load_shedding_stage_eskom icon: '' - type: entity entity: sensor.load_shedding_area_eskde_14_milnertoncityofcapetownwesterncape - type: template content: '{{state_attr(entity, "count")}}/{{state_attr(entity, "limit")}}' entity: sensor.sepush_api_quota icon: mdi:api tap_action: action: more-info alignment: center - type: markdown content: > {% set stage_sensor = "sensor.load_shedding_stage_eskom" %} {% set area_sensor = "sensor.load_shedding_area_eskde_14_milnertoncityofcapetownwesterncape" %} {% set area_schedule = state_attr(area_sensor, "forecast") %} {% if area_schedule %} {% set start_time = area_schedule[0].start_time %} {% set end_time = area_schedule[0].end_time %} {% if is_state(area_sensor, "off") %} {% set starts_in = timedelta(minutes=state_attr(area_sensor, "starts_in")).total_seconds() | int // 60 %} {% set mins = starts_in % 60 %} {% set hrs = starts_in // 60 % 24 %} {% set days = starts_in // 1440 %} {% set alert = "Load Shedding starts in {d}d {h}h {m}m ({next})".format(d=days, m=mins, h=hrs, next=as_timestamp(start_time) | timestamp_custom("%H:%M", True)) %} {% if is_state_attr(stage_sensor, "stage", 0) %} {{ states(stage_sensor) }} {% elif starts_in > 1440 %} {{ alert }} {% elif not is_state_attr(stage_sensor, "stage", 0) and 60 < starts_in <= 1440 %} {{ alert }} {% else %} {{ alert }} {% endif %} {% else %} {% set ends_in = timedelta(minutes=state_attr(area_sensor, "ends_in")).total_seconds() | int // 60 %} {% set mins = ends_in % 60 %} {% set hrs = ends_in // 60 % 24 %} {% set days = ends_in // 1440 %} {% set alert = "Load Shedding ends in {d}d {h}h {m}m ({next})".format(d=days, m=mins, h=hrs, next=as_timestamp(end_time) | timestamp_custom("%H:%M", True)) %} {{ alert }} {% endif %} {% else %} {% set stage = state_attr(stage_sensor, "next_stage") %} {% set start_time = state_attr(stage_sensor, "start_time") %} {% set end_time = state_attr(stage_sensor, "end_time") %} {% set starts_in = timedelta(minutes=state_attr(stage_sensor, "starts_in")).total_seconds() | int // 60 %} {% set mins = starts_in % 60 %} {% set hrs = starts_in // 60 % 24 %} {% set days = starts_in // 1440 %} {% set alert = "Stage {stage} starts in {d}d {h}h {m}m ({next})".format(stage=stage, d=days, m=mins, h=hrs, next=as_timestamp(start_time) | timestamp_custom("%H:%M", True)) %} {{ alert }} {% endif %} - type: custom:atomic-calendar-revive enableModeChange: true firstDayOfWeek: 1 maxDaysToShow: 7 refreshInterval: 1800 entities: - calendar.load_shedding_forecast showCurrentEventLine: false showMonth: true showWeekDay: true disableEventLink: true showNoEventsForToday: false disableLocationLink: true showFullDayProgress: false showEventIcon: false showHiddenText: false showCalendarName: false calShowDescription: false showLastCalendarWeek: false disableCalEventLink: true disableCalLocationLink: true disableCalLink: true showDescription: false dateFormat: LL showDate: false sortByStartTime: false showRelativeTime: true showProgressBar: true showLocation: true ```

I'd also recommend you check out the latest dev release for a new card. image

wernerhp commented 1 year ago

Fixed card4.yaml in v1.1.0-dev.2