Binary Sensor Template Help

Ok, hope this is the right spot for this.

I’m trying to make a binary sensor template that turns on at one set of conditions if it’s off and turns off at a different set of conditions if it’s on. So, I created this:

    - binary_sensor:
        - name: "Master Bath Humidity Detected"
          state: >-
                {%- if states('binary_sensor.master_bath_humidity_detected') == 'off' -%}
                  {%- if (states('sensor.lumi_lumi_weather_humidity_2') | float >= 50) and (states('sensor.lumi_lumi_weather_humidity_2') | float - states('sensor.lumi_lumi_weather_humidity') | float > 10) -%}
                    on
                  {%- endif -%}
                {%- else -%}
                  {%- if (states('sensor.lumi_lumi_weather_humidity_2') | float < 50) or (states('sensor.lumi_lumi_weather_humidity') | float - states('sensor.lumi_lumi_weather_humidity_2') | float >= 0) -%}
                    off
                  {%- endif -%}
                {%- endif -%}

So the idea is that if the binary sensor is off, and then Bathroom Humidity (lumi_lumi_weather_humidity_2) goes above 50 and the difference between the Bathroom Humidity and the Bedroom Humidity (lumi_lumi_weather_humidity) is greater than or equal to 10, turn the binary sensor on.
If the binary sensor is on, and the Bathroom Humidity goes below 50 or the Bathroom Humidity is equal to or below the Bedroom Humidity, turn the binary sensor off.

So looking at the history I’m getting this:

Turned off triggered by state of Master Bathroom Sensor Humidity changed to 68.5
2:18:40 PM - 27 minutes ago

Turned on triggered by state of Master Bathroom Sensor Humidity changed to 68.5
2:18:40 PM - 27 minutes ago

So it turns on then immediately turns off.

I paste the template in the Developer Tools and it seems to work fine.

Can anyone help??

Thx

Your template has to report a value for all circumstances. Currently, it doesn’t because there are logical gaps in the sequence of if statements.

For example, let’s say the first if evaluates to true so it proceeds to check the nested if and if that’s true it reports on. What if the nested if evaluates to false? The template will report nothing.

So your saying I have to do it like this?:

    - binary_sensor:
        - name: "Master Bath Humidity Detected"
          state: >-
                {%- if states('binary_sensor.master_bath_humidity_detected') == 'off' -%}
                  {%- if (states('sensor.lumi_lumi_weather_humidity_2') | float >= 50) and (states('sensor.lumi_lumi_weather_humidity_2') | float - states('sensor.lumi_lumi_weather_humidity') | float > 10) -%}
                    on
                  {%- else -%}
                    off
                  {%- endif -%}
                {%- else -%}
                  {%- if (states('sensor.lumi_lumi_weather_humidity_2') | float < 50) or (states('sensor.lumi_lumi_weather_humidity') | float - states('sensor.lumi_lumi_weather_humidity_2') | float >= 0) -%}
                    off
                  {%- else -%}
                    on
                  {%- endif -%}
                {%- endif -%}

You should add some hysteresis to triggers. If you check value 50 for on state then check for value 49 or 49.5 for off state. That would prevent flipping it on and off when value is exactly 50.