Binary_sensor: remember last state and ignore unavailable

I have a water flood sensor, wifi, which just connects to wifi when water is detected… for some seconds… and then disconnects to save battery…
When it disconnects expecting to some flood :)… HA lists it as unavailable… even after any flood the sensor went from “alarm”, to “normal”… and then to “unavailable” when disconnected to to energy saving mode…
Is there any simple way to just make HA remember last non-unavailable state?

Thanks

That’s normal operation; if the physical sensor ceases to be accessible, its state is unavailable.

You could use a Template Sensor (to monitor the flood sensor and only report the desired values). It can be configured to ignore unavailable and only report the states you want.

that sounds very good, I was trying to do it but couldn;t figure it out.
I must create a new one in configuration.yaml, right, no way in UI…?
Would one new like:

binary_sensor:
  - platform: template

with some rules to only give on are off with the states I say?

My case is a binary_sensor which only provides “alarm”, which is my “on”… and "normal"which is my off. I’d like to keep it that way and also put as normal/off when unavailable

something like this?? i dont know if its the most simple way… or if this is ok xD, is the first template I do… I’ll test a bit

binary_sensor:
  - platform: template
    sensors:
      flood_sensor_template:
          friendly_name: Flood sensor wifi
          entity_id:
            - binary_sensor.XXXXX
          value_template: >-
            {%- if is_state("binary_sensor.XXXXXXX", "on") -%}
            Wet
            {%- else -%}
            Dry
            {%- endif -%}

The following Template Binary Sensor reports on if your flood sensor reports alarm otherwise it reports off.

template:
  - binary_sensor:
      - name: Flood Status
        device_class: moisture
        state: "{{ states('sensor.your_flood_sensor') == 'alarm' }}"
3 Likes

Thanks, I had to change it because it was not working, it seems that my current sensor is binary so it was not recognized the “alarm”, but it was as “on”. This is the final config that worked:

template:
  - binary_sensor:
      - name: Flood sensor template
        device_class: moisture
        state: "{{ states('binary_sensor.watersensor_state') == 'on' }}"

Thank you so much @123 :slight_smile:

2 Likes