How to test the returned id(binary_sensor).state is valid?

In the following code, the formatted logger.log always reports zero (which doesn’t surprised me). Unless, I un-Rem the delay. Then, the logger correctly reports the reality of the HA binary sensor.
However, delays are never a good strategy. Something, somewhere can change and the delay will be ineffectual.
What other methods (or whatever they are called in yaml) besides id().state are available that I could find useful? Maybe it would return NaN rather than zero if the value has not been retrieved from the HA server yet.

esphome:
  name: "esp32-adc-sleep"
  on_boot:
    - logger.log: "Waiting for the API to connect..."
    - wait_until:
        condition:
          api.connected:
    - if:
        condition:
          api.connected:
        then:
          - logger.log: "API Connected"
        else:
          - logger.log: "API NOT Connected"  
    - logger.log: "I'm in the on_boot code..."
    #- delay: 10s
    - logger.log:
        format: 'Prevent Sleep Flag set to: %i'
        args: [id(prevent_deep_sleep).state]
    # - I'd like to find a "wait_until" to avoid the (above) Rem'd delay: 10s 

Regards, Martin
PS _ Dispite hunting around binary_sensor file reference I’ve not been able to find any clues…

Looks to me like .has_state() will be false until HA connects and updates the binary sensor.

  on_boot:
    - logger.log:
        format: 'Prevent Sleep Flag set to: %i : %i'
        args: [id(prevent_deep_sleep).state, id(prevent_deep_sleep).has_state() ]
    - logger.log: "Waiting for the API to connect..."
    - wait_until:
        condition:
          api.connected:
    - if:
        condition:
          api.connected:
        then:
          - logger.log: "API Connected"
        else:
          - logger.log: "API NOT Connected"  
    - logger.log: "I'm in the on_boot code..."
    - logger.log:
        format: 'Prevent Sleep Flag set to: %i : %i'
        args: [id(prevent_deep_sleep).state, id(prevent_deep_sleep).has_state() ]    
    #- delay: 10s
    - wait_until:
        condition:
            lambda: |-
              return id(prevent_deep_sleep).has_state();
    - logger.log:
        format: 'Prevent Sleep Flag set to: %i : %i'
        args: [id(prevent_deep_sleep).state, id(prevent_deep_sleep).has_state() ]
    - logger.log: "Boot code complete!"   

Just a quick Thank You, to acknowledge that I have seen your suggestion. I’ll get to trying it out as soon as I can.
I think that it is the “update” that is important because although I wait for the HA connection, it still reports false for some time after that.
Regards, Martin