Home Assistant Binary Sensor - Force Update

Hi,

I have been using this guide to help keep a device awake for updates:

I seem to have a problem that the device doesn’t reliably get the value from HASS (it does sometimes) and am really struggling to troubleshoot. I see nothing VERBOSE logging and I can’t recreate the failure scenario either.

Is there a method, lambda function or anything I can call to force the binary_sensor to update from the home assistant value? (I will add this forced update before the deep sleep check).

Thanks in advance
Cameron.

@tatham are you around?

Using the ap is not the best with deep sleep in any event. See from the creator No api connection to HA after a while using Deep Sleep · Issue #350 · esphome/issues · GitHub

Thanks. Sounds exactly like my problem. I’ll have to go the MQTT route then.

Hi,

So I’ve got this all working, but my device state is showing up as unavailable shortly after reporting a value. Seems like a timeout/going to sleep is causing the sensor to go offline and HASS to treat it as unavailable.

Any suggestions, on how I can fix this? Here is is my ESPHome config for the devices.

Thanks
Cameron.

esphome:
  name: pooltherm
  on_boot:
    then:
      - script.execute: consider_deep_sleep

esp8266:
  board: d1_mini

# Enable logging
logger:
#  level: VERBOSE

# Enable Home Assistant API
#api:
#  password: !secret api_password
  
ota:
  password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

#   Optional manual IP
#  manual_ip:
#    static_ip: 192.168.1.195
#    gateway: 192.168.1.250
#    subnet: 255.255.255.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Pooltherm Fallback Hotspot"
    password: "GpS4S9L1M1SU"

captive_portal:
    
mqtt:
  broker: !secret homeassistantIP
  username: esphome
  password: !secret mqtt_password
  on_message:
    topic: helper/input_boolean/esphome_sleepoff_pool
    then:
      - binary_sensor.template.publish:
          id: prevent_deep_sleep
          state: !lambda 'if(x=="on"){return 1;}else{return 0;}'

dallas:
  - pin: GPIO12
    update_interval: 2s

sensor:
  - platform: dallas
    index: 0
    name: "Pool Temp"
  - platform: adc
    pin: A0
    name: "Pool Battery"
    filters:
      - multiply: 5.24
    update_interval: 2s
  - platform: wifi_signal
    name: "WiFi Signal Sensor"
    update_interval: 2s

binary_sensor:
  - platform: template
    id: prevent_deep_sleep
    name: Prevent Deep Sleep
    #entity_id: input_boolean.esphome_sleepoff_pool

deep_sleep:
  id: deep_sleep_control
  sleep_duration: 20min
  # Do NOT set a run_duration or it will override the script setting

script:
  id: consider_deep_sleep
  mode: queued
  then:
    - delay: 20s  #Note: 10s with weak signal is not long enough
    - logger.log:
        format: "Sleep Signal: %1.1f"
        args: [ 'id(prevent_deep_sleep).state' ]
    - delay: 1s
    - if:
        condition:
          binary_sensor.is_on: prevent_deep_sleep
        then:
          - logger.log: 'Skipping sleep, per prevent_deep_sleep from script'
        else:
          - logger.log: 'Entering Deep Sleep from Script'
          - deep_sleep.enter: deep_sleep_control
    - script.execute: consider_deep_sleep

:point_right: Last Will And Birth Messages :point_left:

ESPHome uses the last will testament and birth message feature of MQTT to achieve availability reporting for Home Assistant. If the node is not connected to MQTT, Home Assistant will show all its entities as unavailable (a feature :wink:).

../_images/mqtt-availability.png

Is there any way to trick that during sleep? With native HASS MQTT sensor you can use expiry_after and availability latest or something. (Or are you saying if I fudge the last will and testament then it’ll stop these being reported as unavailable? Interesting thought)

Otherwise MQTT isn’t a real alternative to API during sleep…

I guess I’ll have to create another manual MQTT sensor in HASS directly reading that queue.

It’s written in docs how to do disable the availability reporting (the link to that is found in my last post) :point_left:

Thank you!
:see_no_evil:
(And yes, it’s working now by setting different topics - I get error messages in compile if I tried to make them empty/null).