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
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 %}
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.
@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
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