Outdoor Rain Sensor & Aqara Leak Sensor – Binary Sensor Inverted

Hello Everyone,

I am pretty new to HA and this is the first shout for help, so hope its in the right place and I am very much learning, lots of trial by error, more errors than anything else. Anyway I have decided to try and make a rain sensor using a Claber rain sensor which has a magnetic reed switch in it (makes contact when dry / breaks contact when wet) and an Aqara Zigbee water leak sensor.

I have got the Rain sensor and Aqara leak sensor connected within HA and it is changing state when filled and emptied of water but the Dry and Wet are the wrong way round –

Capture 123

Here it is shown as ‘Dry’ (Rain Sensor) although the rain sensor is full of water.

I have tried lots of different suggestions from the forum but no such luck

So in the interim I have ended up creating two binary sensors ‘Garden Dry’ / ‘Garden Wet’ as per the image above

The code I have used for this is

  - platform: template
    sensors:
      garden_rain_sensor_wet:
        entity_id: binary_sensor.water_sensor
        value_template: >
          {{ is_state("binary_sensor.water_sensor", "off") }}
        device_class: moisture
        friendly_name: Garden Wet
  - platform: template
    sensors:
      garden_rain_sensor_dry:
        entity_id: binary_sensor.water_sensor
        value_template: >
          {{ is_state("binary_sensor.water_sensor", "on") }}
        device_class: moisture
        friendly_name: Garden Dry

Does anyone have any suggestions to have one binary sensor that shows dry and wet the corrected way round and if it is in color (wet = red / dry = green) that would be fantastic…

Thank you for any help you can provide….

Welcome Square_eye!

I fear I cannot follow. Both binary sensors are „damaged“ and show the wrong state?

If you want to create one template sensor that corrects the ‘inverted’ states, do something like this:-

- sensor:
    - name: "Garden is wet"
      unique_id: garden_is_wet
      state: >
        {% if states("binary_sensor.water_sensor") %}
        off
        {%else%}
        on
        {%endif%}
      icon: mdi:thermometer

Or even shorter:-

- sensor:
    - name: "Garden is wet"
      unique_id: garden_is_wet
      state: >
        {{ not states("binary_sensor.water_sensor") }}
      icon: mdi:thermometer

Should be


- sensor:
    - name: "Garden is wet"
      unique_id: garden_is_wet
      state: >
        {%  if states('binary_sensor.water_sensor') == 'on' %}
        off
        {%else%}
        on
        {%endif%}
      icon: mdi:thermometer

(or short: )


{{ 'on' if states('binary_sensor.water_sensor') == 'off' else 'off' }}

But that doesn’t explain the basic problem.

@pedolsky

I believe:-

{{ if states("binary_sensor.water_sensor") }}

resolves to ‘true’ in an if statement (checked in dev tools) when the value is ‘on’, you don’t need to explicitly check for it. See Template - Home Assistant

state template REQUIRED

The sensor is on if the template evaluates as True, yes, on, enable or a positive number. Any other value will render it as off. The actual appearance in the frontend (Open/Closed, Detected/Clear etc) depends on the sensor’s device_class value

Equally,

{{ not states("binary_sensor.water_sensor") }}

resolves to false correctly.

And a third option:-

{{ iif(states('binary_sensor.water_sensor'),'off','on')}}

makes no sense to me. Check it again in Developer Tools.

Ah Indeed! , it seems that it always evaluates to true. My mistake.

Hi All,

After some time away, I am back and trying to get this to work but, in an attempt to try and simplify what I am doing.

Can anyone give some guidance to how to get ha to display the opposing state to what a rain sensor is saying. I.E if it is reading as wet change it to say it is dry and vice verse.

Thanks for any support

Hi Pedolsky, i haven’t had the chance to try and move this little project on, but I find I have some more time now and would appreciate your help.
To cut things back to a simpler arrangement I have an aqura leak sensor which I would like to invert its output within home assistant.
It current shows as ‘wet’ when I want it to ‘Show’ dry and vice versa.
Any suggestions?
Thanks