ESP sensor unavailable -> off -> on, triggers my automation, how to prevent?

I have a BLE sensor that by some reason become unavailable sometimes.
I think it’s due to low wifi coverage in the area.

image

I have an automation that triggers when the sensor is on and although it’s correct it’s not as intended.
What can I do to make sure this does not trigger?

ESP-Home yaml:

esphome:
  name: thirtytwo
  platform: ESP32
  board: esp32dev
  includes:
    - OneEuro.h
    - ble_dist.h
  on_boot:
    then:
      lambda: |-
        addTracker("phone", "748e5f85-d9c5-039f-2049-5d052aa44fe2");
        addTracker("phone2","afb4b4d6-d483-68a2-3845-a87be0c5392b");

esp32_ble_tracker:
  scan_parameters:
    duration: 25s
    active: false
  on_ble_advertise:
    then:
      - lambda: |-
          parseAdvertisement(x);

binary_sensor:
  - platform: ble_presence
    ibeacon_uuid: 'e24fa42a-055d-4920-9f03-c5d9855f8e74'
    name: "Andreas telefon"
    id: "andreas_telefon"
    filters:
      - delayed_off: 60s

The automation that I want to hinder from running in this event:

When either of us gets home the ESP32 mounted in the bedroom sees us and opens the front door in the apartment house.
The condition is that we need to have wifi enabled on the phones and not be connected to wifi.
But since there is a slight coverage issue then if both the phone(s) and the ESP drops off the network then all conditions are meet, wifi is on, currently not connected to home network and the ESP notice our phones → door opens.

alias: Öppna porten (BLE)
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.andreas_telefon
    to: 'on'
    id: andreas
  - platform: state
    entity_id: binary_sensor.petras_telefon
    id: petra
    to: 'on'
  - platform: state
    entity_id:
      - binary_sensor.door_switch_window
    from: 'off'
    to: 'on'
    id: button
condition: null
action:
  - choose:
      - conditions:
          - condition: trigger
            id: andreas
          - condition: template
            value_template: '{{ states(''sensor.wifi_connection'')[0:9] != "wifi-name" }} '
          - condition: state
            entity_id: binary_sensor.andreas_wifi_state
            state: 'on'
        sequence:
          - service: esphome.ns_kitchen_play_rtttl
            data:
              song_str: siren:d=25,o=10,b=150:d,e,d,e,d,e
          - service: switch.turn_on
            target:
              entity_id: switch.porttelefon
            data: {}
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
          - service: switch.turn_on
            target:
              entity_id: switch.porttelefon
            data: {}
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
          - service: switch.turn_on
            target:
              entity_id: switch.porttelefon
            data: {}
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
          - service: switch.turn_on
            target:
              entity_id: switch.porttelefon
            data: {}
  - choose:
      - conditions:
          - condition: trigger
            id: petra
          - condition: template
            value_template: '{{ states(''sensor.petra_wifi_connection'')[0:9] != "wifi-name" }} '
          - condition: state
            entity_id: binary_sensor.petra_wifi_state
            state: 'on'
        sequence:
          - service: esphome.ns_kitchen_play_rtttl
            data:
              song_str: siren:d=25,o=10,b=150:d,e,d,e,d,e
          - service: switch.turn_on
            target:
              entity_id: switch.porttelefon
            data: {}
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
          - service: switch.turn_on
            target:
              entity_id: switch.porttelefon
            data: {}
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
          - service: switch.turn_on
            target:
              entity_id: switch.porttelefon
            data: {}
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
          - service: switch.turn_on
            target:
              entity_id: switch.porttelefon
            data: {}
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
          - service: switch.turn_on
            target:
              entity_id: switch.porttelefon
            data: {}
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
          - service: switch.turn_on
            target:
              entity_id: switch.porttelefon
            data: {}
          - service: notify.mobile_app_petra
            data:
              message: command_high_accuracy_mode
              title: turn_off
mode: single

Where can I create a condition that the BLE sensor can not have been unavailable “lately”?
I know the obvious is to strengthen the wifi network so that I don’t have this slight coverage issue, but the problem will still remain since a reboot of the router or anything like that could make the conditions true.

I think I just figured out a solution.

Trigger on “from unavailable”, set a boolean to on for 20 seconds then off again
Have this boolean as a condition in the code posted above.