Slight issue with jinja if statement with OR

I have a binary sensor, which depends on 2 other sensor values. In effect I am trying to detect area outages (which are not due to Loadshedding - yes, I am in South Africa). The first sensor it depends on, is one read from an inverter, which tells me that it is seeing the mains from the utility company. The second is from an integration which works with the Loadshedding schedule, and simply tells me that Loadshedding is currently being applied in our area. So this custom binary sensor should be True when the Loadshedding is Off, and the Inverter’s Utility sensor is also Off.

So the code I have is as follows:

  - binary_sensor:
      - name: Area Outage Detected
        unique_id: binary_sensor.area_outage_detected
        delay_on: "0:05:00"
        state: > 
          {% set ls = is_state('sensor.load_shedding_in_my_area', 'On') %}
          {% set gs = is_state('sensor.inverter_grid_connected_status', 'On-Grid') %}
          {% if ls or gs %}
          {{   'no' }}
          {% else %}
          {{   'yes' }}
          {% endif %}

This part mostly works, but it triggers when the Loadshedding is being applied in my area, as well as when the grid down (for an area outage). This result is false positives.

This sensor is used in a notifier script, where it will notify us, when it is an area outage in stead of just loadshedding (where power will be back within about 2 hours or 4 hours if we are at high stages). The idea being that we can prepare for situations where we will be without power for extended periods of time (and thus ensure we reduce our load on the inverter batteries).

Area outages are currently rare, but have happened a couple of times in the past few months. The strain being put on the grid infrastructure by the constant loadshedding have caused various substations to explode or catch fire (resulting in no power for weeks in affected areas). I want to get early notification in such a case.

Any help will be greatly appreciated.

Your stated requirement is the binary_sensor should report on when Loadshedding and Inverter’s Utility sensor are both off.

Why is your template logically ORing the opposite states (on)?

Assuming the opposite of your sensors’ states are Off and Off-Grid, this template represents your requirement:

        state: > 
          {% set ls = is_state('sensor.load_shedding_in_my_area', 'Off') %}
          {% set gs = is_state('sensor.inverter_grid_connected_status', 'Off-Grid') %}
          {{ ls and gs }}

EDIT

Correction. Removed extra {.

I have gone through a couple of iterations for this code and decided on this version as for 3 of the 4 possible states, it should be False (or returning “no”) → when both are on, or when either of the 2 is on.

Only for the 4th state (where both is off), should it return the True (‘yes’).

That is why I have the OR which would catch the 3 states, and then the else catching the 4th.

Some of my previous iterations tried the “and” which either did not work at all (never triggered, or only triggered for false positives). This is by far the simplest and most reliable version, but still triggers for a false positive when the Loadshedding is on.

It’s unclear to me why the focus in on testing for 3 of the 4 states when the stated requirement is to report true/on for 1 of the four states. It seems to me that the remaining three naturally become false/off.

Anyways, perhaps my understanding of the situation is flawed. If you claim checking for 3 out of 4 works better for you (except when it doesn’t) then I’ll bow out and let someone else help you close the loophole. Good luck.

1 Like