ESPHome: How to do measurements without connection to wifi:

Hello, may I ask for help please? I have following script: it takes measurement from BME280, connects to wifi, sends values over MQTT and then goes to sleep for 1 minute.

I would like to modify the script in the way it takes measurement every minute, but connects to wifi and sends value only if the temperature is over 26C. Or with binary sensor like in mailbox - to connect to wifi only if it detects that GPIO state changes. Is there anyone who understands how to do it please?

esphome:
  name: mqtt_test
  platform: ESP8266
  board: d1_mini_pro

wifi:
  ssid: "IoT"
  password: "98569856"
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "MQTT test Fallback Hotspot"
    password: ""
  fast_connect: on

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
#api:

ota:

i2c:
  sda: 4
  scl: 5
  scan: True
  id: bus_a

sensor:
  - platform: bme280
    i2c_id: bus_a
    temperature:
      name: "Bedroom Temperature"
      oversampling: 16x
      id: bme_temp
    pressure:
      name: "Bedroom Pressure"
      id: bme_pressure
    humidity:
      name: "Bedroom Humidity"
      id: bme_rh
    address: 0x76
    update_interval: 60s

deep_sleep:
  # ...
  id: deep_sleep_1
  run_duration: 2s
  sleep_duration: 1min

mqtt:
  # ...
  broker: 10.0.0.54
  username: janb
  password: janb12
  birth_message:
    topic: mqtt_test/status
    payload:
  will_message:
    topic: mqtt_test/status
    payload:
  on_message:
    - topic: mqtt_test/ota_mode
      payload: 'ON'
      then:
        - deep_sleep.prevent: deep_sleep_1
    - topic: mqtt_test/sleep_mode
      payload: 'ON'
      then:
        - deep_sleep.enter: deep_sleep_1

Something like

...
    on_value_range:
      - above: 28
        then:
          connect to wifi, connect to mqtt and exchange messages