Sensor value to change based on other sensor value (Load Shedding Notify)

Good day all. I have created sensors that I read from mysql using the SQL integration and they all have values. I also have another sensor that detects certain values through an API. All sensors have their values so what I am trying to do is to display the value of one of the sensors based on the value of the other sensor. I will post my code in the hopes that someone can understand what it is that I am trying to do. Any help would be greatly appreciated.

The error I receive is : UndefinedError: ‘state’ is undefined

{% set stage = { 
   "stage_1": is_state("sensor.eskom_loadshedding_stage", "1"),
   "stage_2": is_state("sensor.eskom_loadshedding_stage", "2"),
   "stage_3": is_state("sensor.eskom_loadshedding_stage", "3"),
   "stage_4": is_state("sensor.eskom_loadshedding_stage", "4"),
   "stage_5": is_state("sensor.eskom_loadshedding_stage", "5"),
   "stage_6": is_state("sensor.eskom_loadshedding_stage", "6"),
   "stage_7": is_state("sensor.eskom_loadshedding_stage", "7"),
   "stage_8": is_state("sensor.eskom_loadshedding_stage", "8") 
} %}

{% set stage_times = {
   "stime1": state("sensor.loadshedding_times_1"),
   "stime2": state("sensor.loadshedding_times_2"),
   "stime3": state("sensor.loadshedding_times_3"),
   "stime4": state("sensor.loadshedding_times_4"),
   "stime5": state("sensor.loadshedding_times_5"),
   "stime6": state("sensor.loadshedding_times_6"),
   "stime7": state("sensor.loadshedding_times_7"),
   "stime8": state("sensor.loadshedding_times_8")
} %}

{% if stage.stage_2 %} 

Loadshedding is at {{ stage_times.stime2 }} 

{% endif %}

I am sure I am missing something stupid but I cannot for the life of me figure it out.

I have managed to figure it out. I will post my correct code for anyone trying to do something similar. Hopefully it can help you out as well.

{% set stage = { 
   "stage_1": is_state("sensor.eskom_loadshedding_stage", "1"),
   "stage_2": is_state("sensor.eskom_loadshedding_stage", "2"),
   "stage_3": is_state("sensor.eskom_loadshedding_stage", "3"),
   "stage_4": is_state("sensor.eskom_loadshedding_stage", "4"),
   "stage_5": is_state("sensor.eskom_loadshedding_stage", "5"),
   "stage_6": is_state("sensor.eskom_loadshedding_stage", "6"),
   "stage_7": is_state("sensor.eskom_loadshedding_stage", "7"),
   "stage_8": is_state("sensor.eskom_loadshedding_stage", "8") 
} %}

{% set stage_times = {
   "stime1": states("sensor.loadshedding_times_1"),
   "stime2": states("sensor.loadshedding_times_2"),
   "stime3": states("sensor.loadshedding_times_3"),
   "stime4": states("sensor.loadshedding_times_4"),
   "stime5": states("sensor.loadshedding_times_5"),
   "stime6": states("sensor.loadshedding_times_6"),
   "stime7": states("sensor.loadshedding_times_7"),
   "stime8": states("sensor.loadshedding_times_8")
} %}

{% if stage.stage_2 %} 

Loadshedding is at {{ stage_times.stime2 }} 

{% endif %}

If someone could assist me with translating the below code to a lovelace card such as an Entity card that would be great as I seem to be missing how one would set variable within the lovelace card. My code is below and it works perfectly in the Template editor.

{% set stage = { 
   "stage_0": is_state("sensor.eskom_loadshedding_stage", "0"),
   "stage_1": is_state("sensor.eskom_loadshedding_stage", "1"),
   "stage_2": is_state("sensor.eskom_loadshedding_stage", "2"),
   "stage_3": is_state("sensor.eskom_loadshedding_stage", "3"),
   "stage_4": is_state("sensor.eskom_loadshedding_stage", "4"),
   "stage_5": is_state("sensor.eskom_loadshedding_stage", "5"),
   "stage_6": is_state("sensor.eskom_loadshedding_stage", "6"),
   "stage_7": is_state("sensor.eskom_loadshedding_stage", "7"),
   "stage_8": is_state("sensor.eskom_loadshedding_stage", "8") 
} %}

{% set stage_times = {
   "stime0": "Not Loadshedding at the moment :)",
   "stime1": states("sensor.loadshedding_times_1"),
   "stime2": states("sensor.loadshedding_times_2"),
   "stime3": states("sensor.loadshedding_times_3"),
   "stime4": states("sensor.loadshedding_times_4"),
   "stime5": states("sensor.loadshedding_times_5"),
   "stime6": states("sensor.loadshedding_times_6"),
   "stime7": states("sensor.loadshedding_times_7"),
   "stime8": states("sensor.loadshedding_times_8")
} %}

{% if stage.stage_1 %} 

Stage 1 Loadshedding is at {{ stage_times.stime1 }} 

{% elif stage.stage_2 %} 

Stage 2 Loadshedding is at {{ stage_times.stime2 }} 

{% elif stage.stage_3 %}

Stage 3 Loadshedding is at {{ stage_times.stime3 }}

{% elif stage.stage_4 %}

Stage 4 Loadshedding is at {{ stage_times.stime4 }}

{% elif stage.stage_5 %}

Stage 5 Loadshedding is at {{ stage_times.stime5 }}

{% elif stage.stage_6 %}

Stage 6 Loadshedding is at {{ stage_times.stime6 }}

{% elif stage.stage_7 %}

Stage 7 Loadshedding is at {{ stage_times.stime7 }}

{% elif stage.stage_8 %}

Stage 8 Loadshedding is at {{ stage_times.stime8 }}

{% elif stage.stage_0 %}

{{ stage_times.stime0 }}

{% endif %}

Hey Stefan,

you can copy your code in a markdown card or you create a template sensor.

Thank you so much, that did the trick. Was not even aware of the Markdown Card.

Have a good day.

You can also check out https://github.com/wernerhp/ha_integration_load_shedding/ or search for Load Shedding on HACS for an integration that I wrote. The one on HACS is a bit rough around the edges, but I’m busy updating it and will release an update soon.

Thanks for the info , Ive downloaded your integration and seems only the Milnerton suburb expands with times and schedules, any other suburb I try shows correct stage for that area/zone, but no schedule is shown. Ive searched all code for a possible / accidental hard code for your area Milnerton , but nothing found. Any ideas ? Im trying to get Edgemead working.

Unfortunately it only works for Eskom Direct customers at present It must be on https://loadshedding.eskom.co.za/ which doesn’t seem to be the case for Edgemead.

Workaround is to search for another area with the same schedule as Edgemead.

Seems Milnerton is almost isolated with Direct Eskom, all other areas I have checked results in COCT customers.

[EDIT] i used the ESKOMSEPUSH App and found Platteklip in the Swrtland on the same zone. Thanks for the tip, its working

I searched the forum and found a fork of

swartjean/ha-eskom-loadshedding
here: https://github.com/tiaanv/ha-eskom-loadshedding

tiaan has included 2 sensors for COCT customers to pull next schedule, however it doesnt display as involved schedule with start and end times.

Perhaps you want to check his code to see how he pulled COCT customers, if you want to enhance your integration?

@wernerhp , how do you approach the difference the Cape Town stages differ from national ?
Your script is pulling stage 6 schedule currently while Cape Town is on 4. Is there an easy work around for this ?

This should compact things down a bit by detecting the first true item in stage. Might need to refactor a bit to get stage 0 at the end and cope with none of them being true.

{% set idx = (stage|items|selectattr(1,'eq',true)|first)[0][-1] %}
{{ "Stage " ~ idx ~ " Loadshedding is at " ~ stage_times["stime" ~ idx] }}

The first line pulls stage into a list of items like ('stage1', true); selects all the items where the second element (1, but it’s zero-based) is true; takes the first of those; takes the key name ([0]); and extracts the final character of that key name ([-1]) which is the digit at the end.

I don’t cater for CoCT at the moment. That’s some future work (or for someone else to contribute)

The original one by swartjean only supported stage on/off. I had a look at the fork and reached out the tiaanv. Will see if we can get it into the python lib Werner Pieterson / Load Shedding · GitLab

I’ll look at adding an option in the HA Config Flow to search for your Area ID and maybe a checkbox to downgrade by one stage? Is CoCT always one stage lower or does it depend?

Thanks for your response, to quickly answer your question, CoCT is not always one stage lower, it depends on how full Steenbras Dam is and whether it has capacity to assist. So perhaps a checkbox to downgrade, perhaps a selection box so we can set the temporary stage that it can pull the scheduled times on the stage we override from this selction box and if not selected then to pull the normal national stage schedule?

This would be a workaround as first prize would be to pull the schedule based on the actual stage set by CoCT.

If you know of a an API (or a website - something that’s got structure, not a tweet that changes it’s format all the time) where we can get the active stage for CoCT let me know and we can make a plan to integrate this

Im trying to see if Eskom Sepush have released or shared their API yet, there was talk about it.

Im busy reading up here and trying to see if this helps ? but I think this is how yours is currently working ?

I reached out the the guys from ESP. They only have Business API currently which costs a lot of money. They may release a public API in future.

1 Like

The API is now available for personal use too: EskomSePush Business API 2.0 (getpostman.com)

3 Likes

Yip private EskomSePush now available and working. Ive not been able to integrate into Home Assistant unfortunately. Hopefully some of the clever people here can do it

Anyone done any work on getting the EskomsePush api working in Hassio?

@uncapped_shady I have just updated my loadshedding integration with support for the EskomSePush API - hope you find it useful :+1:

2 Likes