Water Leak Sensor - Unknown Status at start-up

Hi,
I have some Aqara Water Leak sensors, connected via Zigbee->Zigbee2MQTT->Home Assistant.
They works fine and they detect dry and moisture. I created this Automation to send me notification, when status change:

alias: Water Leak Notification
description: Send a Notification when Water Leak Sensor Detect Moist
trigger:
  - type: moist
    platform: device
    device_id: 83ec0ea70514efc5c8b3b9010ae5663e
    entity_id: b3614c30067eb04c8ed22864cad70ae8
    domain: binary_sensor
    id: WC_,moist
  - type: moist
    platform: device
    device_id: 1061b11f8cfda0cd845a81956479b37d
    entity_id: 8389006168a00169997a0f8e81697bfa
    domain: binary_sensor
    id: LavaboCucina_moist
  - type: moist
    platform: device
    device_id: 2b8bd04267ecf65e720fdaec13b15631
    entity_id: 1fff943fe6c070e87bf5f3b4e595c2c6
    domain: binary_sensor
    id: LavaboBagno01_moist
  - type: not_moist
    platform: device
    device_id: 83ec0ea70514efc5c8b3b9010ae5663e
    entity_id: b3614c30067eb04c8ed22864cad70ae8
    domain: binary_sensor
    id: WC_dry
  - type: not_moist
    platform: device
    device_id: 1061b11f8cfda0cd845a81956479b37d
    entity_id: 8389006168a00169997a0f8e81697bfa
    domain: binary_sensor
    id: LavaboCucina_dry
  - type: not_moist
    platform: device
    device_id: 2b8bd04267ecf65e720fdaec13b15631
    entity_id: 1fff943fe6c070e87bf5f3b4e595c2c6
    domain: binary_sensor
    id: LavaboBagno01_dry
condition: []
action:
  - if:
      - condition: or
        conditions:
          - condition: trigger
            id:
              - WC_,moist
              - LavaboCucina_moist
              - LavaboBagno01_moist
    then:
      - service: notify.pushover
        data:
          message: >-
            Water Leak Sensor {{ trigger.to_state.name }} detect a moist
            condition
          title: Water Leak Sensor Alarm
          data:
            priority: 2
            retry: 60
            expire: 5
            sound: siren
  - if:
      - condition: or
        conditions:
          - condition: trigger
            id:
              - WC_dry
              - LavaboCucina_dry
              - LavaboBagno01_dry
    then:
      - service: notify.pushover
        data:
          message: Water Leak Sensor {{ trigger.to_state.name }} detect a dry condition
          title: Water Leak Sensor Clear
          data:
            priority: 1
mode: single

The automation works fine. Only concer I have is on Home Assistant Startup. When HA start up, after a restart, sensor status is “Unknown” until next update. Whe first update happen, sensor move from “Unknown” to “Dry” and automation is fired and send the notification. In there a way to detect the “Unknown” status in automation condition, and do not fire the Automation after restart? Or there is a way to persist last sensor status over a reboot, so sensor will not became “unknown” and automation will be not fired?

Many Thanks for help

Marco

You might try setting “Retain” on one of those devices to see if that keeps it from triggering. In my situation, my automation only triggers if “moist” so I don’t have that situation, but I can see how moving from “unknown” to “Dry” can trigger your automation. I think setting “Retain” will keep the previous known value and potentially keep your automation from triggering upon startup.

Hi @lymkin
unfortunately It dows not work. I set retain on zigbee2MQTT but on HA restart, Dry was triggered.
Thanks

Don’t use device triggers use state triggers and specify the from state. e.g.

trigger:
  - platform: state
    entity_id: binary_sensor.water_leak
    to: 'on'
    id: leak_detected
  - platform: state
    entity_id: binary_sensor.water_leak
    from: 'on' # won't trigger from unknown
    to: 'off'
    id: dry

Note that no from state is specified for the leak detected trigger. You do want to know if it goes from anything to on (leak).

Setting the retain flag should have fixed this though.