wernerhp / ha.integration.load_shedding

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

Card 4 - Error #39

Closed leonw1090 closed 1 year ago

leonw1090 commented 1 year ago

Hi,

Thank you for the amazing work being done!

I have the following error. (running dev 1.1.0-5) when copying the card without changing the data (so area is still sensor.load_shedding_area_eskde_14_milnertoncityofcapetownwesterncape) the card shows the correct info on when is the next stage of load shedding etc. is. But obviously it does not show anything when load shedding starts because I do not have sensor.load_shedding_area_eskde_14_milnertoncityofcapetownwesterncape listed anywhere. I then fix it to set area to sensor.load_shedding_area_tshwane_2_faerieglenext7 then the stage prediction breaks of when the next load shedding change will be but my schedule is fixed.

Please find the 2 examples attached.

Am I missing something? all the data is correct when looking under the sensors. and the calender.

Screenshot 2022-11-08 083855

Screenshot 2022-11-08 084422

Regards

DravenSA commented 1 year ago

I also have the same issue, I however also noticed that my mushroom card was showing default value as ON Screenshot 2022-11-08 091113

So looking into the STATES Screenshot 2022-11-08 090535

My card code i believe is correct,

type: markdown
content: >
  {% set stage_sensor = "sensor.load_shedding_stage_eskom" %}  {% set
  area_sensor = "sensor.load_shedding_area_jhbcitypower2_2_greymont" %} {% set
  start_time = state_attr(stage_sensor, "start_time") %}    {% set end_time =
  state_attr(stage_sensor, "end_time") %} {% 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) %}
        <ha-alert alert-type="success">{{ states(stage_sensor) }}</ha-alert>
      {% elif starts_in > 1440  %}
        <ha-alert alert-type="success">{{ alert }}</ha-alert>
      {% elif not is_state_attr(stage_sensor, "stage", 0) and 60 < starts_in <= 1440 %}
        <ha-alert alert-type="warning">{{ alert }}</ha-alert>
      {% else %}
        <ha-alert alert-type="error">{{ alert }}</ha-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)) %} 

        <ha-alert alert-type="error">{{ alert }}</ha-alert>
    {% endif %}
  {% endif %}

yet i still get the display incorrectly

Screenshot 2022-11-08 091424

After writing all this i also wonder is it has something to do with No Load Shedding that the card is not pulling the next piece of information, Will report back when the API updates to Load Shedding

DravenSA commented 1 year ago

Stage 2 Started, restarted HA, (as i was updating another addon, and now this Screenshot 2022-11-08 094408 Still showing the Load Shedding ends in 0d 0h 0m (18:30)

also not sure what is wrong with my top code in the mushroom chip card

wernerhp commented 1 year ago

I made some tweaks to the card this morning. See if this solves it.

DravenSA commented 1 year ago

Unfortunately not, I even used the whole card and only changed my area, the second i did that, it changed to the same as we are getting. My card on the left, your direct copy on the right, just with the area changed Screenshot 2022-11-08 104039

Here is currently what i have in my STATES section I see that my default value is ON, but that should be off, as I am not currently loadshedding although Stage 2 is implemented. Screenshot 2022-11-08 104410

leonw1090 commented 1 year ago

I made some tweaks to the card this morning. See if this solves it.

Thank you for your time. Unfortunately the problem persists. I have noticed under the area sensor a few attributes are missing data. Could this be a reason.

Screenshot 2022-11-08 105549

DravenSA commented 1 year ago

Updated to dev6, still same problem :-(

leonw1090 commented 1 year ago

Also updated. But no change 🙂

gregcsw commented 1 year ago

I made some tweaks to the card this morning. See if this solves it.

I believe this line need to change from {% if is_state(area_sensor, "off") %} to {% if is_state(area_sensor, "on") %} at least it appears to have fixed it or me.

Also updated API entity_id.

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_jhbcitypower2_16_robinacres - type: template content: '{{state_attr(entity, "count")}}/{{state_attr(entity, "limit")}}' entity: sensor.load_shedding_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_jhbcitypower2_16_robinacres" %} {% 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, "on") %} {% 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 starts_in > 1440 %} {{ states(stage_sensor) }} {% elif 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, "next_start_time") %} {% set end_time = state_attr(stage_sensor, "next_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 %} {% if (start_time == 0 or end_time == 0) %} {% set alert = "No Load Shedding" %} {% else %} {% 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)) %} {% endif %} {{ alert }} {% endif %} - type: custom:atomic-calendar-revive enableModeChange: true firstDayOfWeek: 1 refreshInterval: 1800 entities: - calendar.load_shedding_forecast showCurrentEventLine: false showMonth: true showWeekDay: true disableEventLink: true showNoEventsForToday: true disableLocationLink: true showFullDayProgress: false showEventIcon: false showHiddenText: false showCalendarName: false calShowDescription: false showLastCalendarWeek: true disableCalEventLink: true disableCalLocationLink: true disableCalLink: true showDescription: false dateFormat: LL showDate: false sortByStartTime: false showRelativeTime: true showProgressBar: true showLocation: true showDeclined: true showMultiDayEventParts: false showMultiDay: false showLoader: false maxDaysToShow: 3`

Screenshot 2022-11-08 at 16 51 49

wernerhp commented 1 year ago

Cool, will investigate.

DravenSA commented 1 year ago

I believe this line need to change from {% if is_state(area_sensor, "off") %} to {% if is_state(area_sensor, "on") %} at least it appears to have fixed it or me.

This is not a fix, the State should be OFF when load shedding is off, and ON when Load Shedding is on, so the fix is only masking the actual problem

also i see that that your screen shot still shows a Load shed that happened 5 hours ago, I am pretty sure that is not meant to be there.

However now that we are actually loadshedding, this is now working, so there must be something not right when were are NOT loadshedding

Screenshot 2022-11-08 172310

i still however notice even though I am currently load shedding that this is still wonky (hoping someone can give me an if / else command in a template to fix this Screenshot 2022-11-08 172736

Hope that the information we are providing is helping

gregcsw commented 1 year ago

I have made some more changes. I will test some more and see if it works as expected.

As for the last load shedding that happened 5 hours ago still showing, maybe Werner can comment on how often the calendar entity is updated, which would remove the old times, but it from my limited monitoring of it, it only seems to update once a day.

There is an option in the Atomic Calendar Revive card which can be added to hide finished events. hideFinishedEvents: true

Edit: Code removed as it doesn't work

wernerhp commented 1 year ago

Ok, made some modifications. Let's see if v1.1.0-dev.7 resolves it.

Sensors refresh every minute in order for the starts_in and ends_in attributes to work correctly. Data is fetched from the API as follows

ℹī¸ Tips

When posting code use the these tags to format code.

<details>
  <summary>Code</summary>

```yaml
EXAMPLE YAML GOES HERE


<details>
  <summary>Code</summary>

```yml
EXAMPLE YAML GOES HERE

When including images, resize them by using the img tag and specifying a width.

<img src="https://path/to/image" width="25%"/>

Preview before posting.

This helps keep long issues a bit more readable.

leonw1090 commented 1 year ago

Good Morning,

On my side (Dev 1.1.0-7) fixed my issue. The Area is turns off when there is no loadshedding. So the card is perfect. Will continue to test when there is no load shedding.

Just a sidebar comment.... LOVE the new API refresh time. It works well ! well done @wernerhp

DravenSA commented 1 year ago

@wernerhp All looking good on my end too, Thank you so much for all the time and effort you dedicated to this project, for me this is not just information, I have it set for warnings when load shedding is going to start, and i have started to set up automations for my watering system to postpone the waterting till the evening if load shedding occurred in the morning (obviously when it has not been raining that day too). I am looking into switchbot to physically push the buttons on UPS systems that are not in use (eg PC is not on, but still connected to mains power) so the batteries dont die when there is Load Shedding so for me these are just some of the things that this integration does and will do to make my life easier "automatically." so again, a big thank you

@leonw1090

Just a sidebar comment.... LOVE the new API refresh time. It works well

did i miss something? I know i hashed together and 90 minute refresh time since the last time the API updated, is there something else that is more sturdy or reliable? or did i totally misread what you were saying? (i like all the bells and whistles) :-)

wernerhp commented 1 year ago

Thanks for the feedback.

Just a sidebar comment.... LOVE the new API refresh time. It works well ! well done

This makes better use of the API quota. Previously used around 32 from the API quota per day (16 for Stage and 16 for Area) every 90 minutes. Now uses 25 per day (24 for Stage updated hourly and 1 for Area updated daily). Future work will be to support multiple Area sensors, which should add just 1 API call.

I have it set for warnings when load shedding is going to start, and i have started to set up automations for my watering system to postpone the waterting till the evening if load shedding occurred in the morning (obviously when it has not been raining that day too).

That sounds neat. Please feel free to share your automations here.

Please consider buying me a coffee to support my work.

DravenSA commented 1 year ago

That sounds neat. Please feel free to share your automations https://github.com/wernerhp/ha.integration.load_shedding/issues/5.

I will do, I dont mind sharing

Please consider buying me a coffee to support my work.

Done, have a coffee break or two..... until next time :-)

wernerhp commented 1 year ago

Thanks for the coffee 🙏

Looks like there was a little bug causing the area sensor to get stuck on off. Fixed in dev.8.

DravenSA commented 1 year ago

Looks like there was a little bug causing the area sensor to get stuck on off. Fixed in dev.8.

unfortunately i don't see a dev 8,

wernerhp commented 1 year ago

Just as well. Issue was not fixed. Hopefully I got it now. 😂 You can try to Update information or Redownload image

wernerhp commented 1 year ago

Looks like I botched it trying to make the sensor restore it's state after a restart. I need to take some time to dig into the HA code to better understand how this work.

DravenSA commented 1 year ago

We have faith in you

gregcsw commented 1 year ago

v1.1.0-dev.9 seems to work great. my load shedding just ended and the timer changed to count down to the next load shedding tomorrow and the forecast (markdown card) also updated.

wernerhp commented 1 year ago

@gregcsw Awesome! Thanks for the feedback. 🚀

DravenSA commented 1 year ago

Everything is working fantastically, Wit the announcement of stage 3 , even the Next Stage section is working.

I believe this is the answer. Also love the way that Stage 2 and Stage 3 overlap each other, makes it very clear to read. Thanks for all the effort again. My card is full of relevant information. Screenshot 2022-11-14 152423 Lets hope that they dont go changing things for a while. (Lets hope Load Shedding ends)