Load shedding card using the EskomSePush API

EskomSePush recently released and API which makes it easy to integrate the data directly into home assistant and create a card that matches the app’s UI

image

This is how it’s done:

  1. Head over to https://docs.google.com/forms/d/e/1FAIpQLSeZhAkhDaQX_mLT2xn41TkVjLkOH3Py3YWHi_UqQP4niOY01g/viewform and ask very nicely for an API key

  2. head over to the API Docsand using post man use “Areas Search (Text)” to find your area and save the id of your area
    image

  3. In your home assistant configuration create a sensor like the one below. (this code is for the city of joburg) others may vary because the api response can differ but if you look at the API response it should be easy to adapt to others. We want the schedule to be part of the attributes and sensor vaule isn’t too important so i just made it the location name

- platform: rest
  name: esp_loadshedding
  resource: https://developer.sepush.co.za/business/2.0/area?id=YOUR_AREA_ID
  headers:
    token: YOUR_API_TOKEN
  json_attributes:
    - events
  scan_interval: 3600
  value_template: '{{ value_json.info.name }}'
  1. Once you have that created then you can validate it in the dev tools and you should get the sensor state looking like this

You can repeat the steps above to get the current status using the status API endpoint

  1. And then to add the loveace card I used a mark down card with the config below
type: markdown
content: >+
  {% set ns = namespace(schedule_date='') %}

  ### {{ states('sensor.esp_loadshedding') }} Currently Stage 

  {% for stage in states.sensor.esp_loadshedding.attributes.events %}

  {%- set dt = strptime(stage.start, '%Y-%m-%dT%H:%M:%S%z') %}

  {% if (ns.schedule_date != dt.date() ) -%}
   #### {{ dt.date() }}
  {% set ns.schedule_date = dt.date() %}

  {%- endif %}

  {%- if (stage.note == "Stage 1") -%}🟡

  {%- elif (stage.note == "Stage 2") -%}🟠

  {%- elif (stage.note == "Stage 3") -%}🔴

  {%- elif (stage.note == "Stage 4") -%}🟤

  {%- elif (stage.note == "Stage 5") -%}⚫

  {%- else %}💀

  {%- endif %} {{ dt.time().strftime('%H:%M') }} - {{strptime(stage.end,
  '%Y-%m-%dT%H:%M:%S%z').time().strftime('%H:%M')}} : {{ stage.note }}

  {%- endfor %}


title: Load Shedding

  1. Buy a gold subscription in the setting of the app if you want to support their project!

Enjoy

5 Likes

This looks interesting. Thanks for the heads-up.

Approximately how long does it take before they reply with an API key?

It took a couple of days before I got a key. And if you want the upcoming schedule and current status then it’s 2 separate API requests so keep that in mind when you fill in the required number of requests per day

1 Like

Thanks so much. Can you just explain this line in the config to me? The rest I got working myself already

You can create a new template sensor like this

sensor:
    - name: 'Next Loadshedding'
      device_class: timestamp
      state: >
        {{ strptime(states.sensor.esp_loadshedding.attributes.events[0].start, '%Y-%m-%dT%H:%M:%S%z') }}
    - name: 'Minutes to next loadshedding'
      state: >
        {{ ((strptime(states.sensor.esp_loadshedding.attributes.events[0].start, '%Y-%m-%dT%H:%M:%S%z') - now() ).seconds/60) | int }}

That goes in templates.yaml with

template: !include templates.yaml

in your configuration.yaml

You can then use the next_loadshedding entitiy as your automation trigger, or some other thing.
And then use a condition to check how long until your next loadshedding

1 Like

You can also use the same template directly in the automation condition and you wont need to create any new entities

description: ""
mode: single
trigger: []
condition:
  - condition: template
    value_template: >-
      ((strptime(states.sensor.esp_loadshedding.attributes.events[0].start, '%Y-%m-%dT%H:%M:%S%z' ) - now()).seconds/60) < 60
action: []

Hi. So the loadshedding schedule is an array and as far as i know you can’t have an array as the value of an entity but you can have it as the attributes. The value_template sets the value of the entity and its not really too important so i just use the suburb/loadshedding area name as a place holder here and then I can put it on the card. It could be anything

Thanks @boozelclark . I’ll play with it on the weekend but makes sense. Thanks for explaining.

I am clearly doing something wrong as from postman i can get the area ID and schedule but in HA template tester it says no events in attributes, but in postman i see the events element in the JSON file

Hi. If you share the details I might be able to help you out

I do the same!

Just note that now that loadshedding is actually ending (who knows for how long), the sensors will start failing as you’re trying to read event[0] which doesn’t exit (the list is empty).

I fixed mine like this:

      next_loadshedding_time:
        friendly_name: Next loadshedding time
        device_class: timestamp
        unique_id: Next_Loadshedding_Time
        value_template: >
            {%if state_attr('sensor.eskomsepush','events')|count > 0 %}
                {%if (as_timestamp(now()) < as_timestamp(state_attr('sensor.eskomsepush','events')[0].start)) and (as_timestamp(now()) < as_timestamp(state_attr('sensor.eskomsepush','events')[0].end)) %}
                {{state_attr('sensor.eskomsepush','events')[0].start }}
                {%elif (as_timestamp(now()) > as_timestamp(state_attr('sensor.eskomsepush','events')[0].start)) and (as_timestamp(now()) < as_timestamp(state_attr('sensor.eskomsepush','events')[0].end)) %}
                {{state_attr('sensor.eskomsepush','events')[0].start }}
                {%elif (as_timestamp(now()) > as_timestamp(state_attr('sensor.eskomsepush','events')[0].start)) and (as_timestamp(now()) > as_timestamp(state_attr('sensor.eskomsepush','events')[0].end)) %}
                {{ state_attr('sensor.eskomsepush','events')[1].start }}
                {%else%}
                Unknown
                {%endif%}
            {%else%}
            Unknown
            {%endif%}

So instead of making the sensor unavailable it will set the value to “Unknown”, which means automations will still run.

I also added the additional code since I noticed that sometimes I still got the previous LS event from the API which is already past. So it will check if it should look for events[0] or [1].

1 Like

If you have HACS then @wernerhp has an update to his Load Shedding Integration that adds a few sensors using EskomSePush which is fantastic.
There is also a new process to get an API Token, if you didn’t get a token from the new gumroad store (ie you used the Google Form) your token will expire at the end of November, follow Werner’s setup it has a link directly to get a token.

1 Like

Thank you for this

Note I believe you now request API access here: EskomSePush API Subscription

3 Likes

Great Work! Working perfectly!

Hi Gents
Not sure if anyone can help me. I setup a load-shedding telegram group (for our community) then an automation in HA which sends out a notification when load-shedding starts and when load-shedding ends it’s based on my grid feed and gets the info from the solarman integration.

  1. Is there anyway I can send the load-shedding info to that group for the day like the stage and times?
  2. ESP normally sends a notification that says load-shedding starts in 55 mins is there anyway i can also send that to the telegram group?

Bear in mind I know fokol about coding. I can kinda find my way around but not too clued up.
I do have an API key and I have setup a card but thats about it. I also know nothing about API’s and what they really do.

Thanks Brett

Can someone please let me know which custom card was used to create this please, I’m not having any luck?
img_11

@HellfireZA you can use the code from the example: https://raw.githubusercontent.com/wernerhp/ha.integration.load_shedding/master/examples/card4.yaml

You need mushroom-chips-card and atomic-calendar-revive cards.

3 Likes

I did manage to figure out what I was missing was atomic, thank you :slight_smile:

HI @boozelclark Im getting the following error although i have just redacted the API key

Any idea why

TIA

@SeaSickMama Sensor configuration should be added to configuration.yaml or some file referred to by configuration.yaml. In the screenshot you try to add it to the user interface (error: card type not found)