Automation to read sensor values

Hi,

I have below “code” for an ESPHOME device for now. I run my device on a battery and solar and would like to make it as power efficient as possible. There I would like to keep the uptime of the esp32 as small as possible to that I can enter deep sleep again as fast as possible, hence an automation that forces read of temperature and battery voltage and then asks the esp32 to enter deep sleep again. With my current setup I see that it is not always reading a temperature on boot and therefore I have currently set uptime to 90 secs before going to deep sleep (60 turned out not to be enough).

As well, if any suggestions on how to power optimize, please let me know (I do e.g. need to turn off the onboard power led, I think I know how to do that).

I’m completely new to ESPHOME, so hope you can help with some descriptive guidance.

esphome:
  name: altan
  platform: ESP32
  board: esp-wrover-kit

wifi:
  ssid: !secret wifi_iot_ssid
  password: !secret wifi_iot_pass
  
  # Optional manual IP
  manual_ip:
    static_ip: 10.0.30.50
    gateway: 10.0.0.1
    subnet: 255.255.255.0
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Altan Fallback Hotspot"
    password: !secret esphome_fallback_hotspot

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret esphome_oat_pass

ota:
  password: !secret esphome_oat_pass

#Deep sleep
deep_sleep:
  run_duration: 90s
  sleep_duration: 30min
  id: deep_sleep_1
  
# Dallas sensor/hub component (in my case for the DS18B20 Waterproof Digital Temperature Temp Sensor Probe)
dallas:
  - pin: GPIO4

sensor:
  - platform: dallas
    address: 0x5E02131E08F5AA28
    name: "Temperatur - Altan"
    id: dallas_temp_1
    
  - platform: adc
    pin: 39
    name: "Battery Voltage - Altan"
    update_interval: 20s
    attenuation: 2.5db
    accuracy_decimals: 1
    id: battery_voltage_1
    filters:
      - multiply: 4.268367

Thanks in advance. KR
Torben

Don’t use the api with deep sleep, use mqtt.

Thanks Tom,

I have now changed my code as shown below. I need to optimize on the uptime (run_duration before entering deep sleep), it is set to 60 secs, but I see it takes around 5 secs for it to boot up and post the MQTT messages for temperature and battery voltage.

Any idea to how I ensure it does exactly one reading of respectively the temperature and the battery voltage (it is using “on_boot” or defining a template or an action in one of the readings? Then along with that I can trigger deep sleep.

Thanks in advance.

substitutions:
  display_name: DIY_Solar_1
  
esphome:
  name: solar_1
  platform: ESP32
  board: esp-wrover-kit

status_led:
  pin: GPIO2
  
wifi:
  ssid: !secret wifi_iot_ssid
  password: !secret wifi_iot_pass
  fast_connect: true
  
  # Optional manual IP
  manual_ip:
    static_ip: !secret ip_static_diy_solar_1
    gateway: !secret ip_gateway
    subnet: !secret ip_subnet
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: ${display_name}_AP
    password: !secret esphome_fallback_hotspot

captive_portal:

# Enable logging
logger:
  #level: INFO #set to INFO to save a bit of time (hence battery)

# MQTT preferred over API when using deep sleep
mqtt:
  broker: !secret mqttip
  port: !secret mqttport
  username: !secret mqttuser
  password: !secret mqttpass
  birth_message: #Setting the MQTT Birth and Will message to blank stops the device from going Unavailable while it is asleep which messes with history graphs (lots of dots/broken lines)
  will_message:
  on_message:
    - topic: ${display_name}/ota_mode
      payload: 'ON'
      then:
        - deep_sleep.prevent: deep_sleep_1
    - topic: ${display_name}/sleep_mode
      payload: 'ON'
      then:
        - deep_sleep.enter: deep_sleep_1

ota:
  password: !secret esphome_oat_pass

#Deep sleep
deep_sleep:
  run_duration: 60s
  sleep_duration: 15min
  id: deep_sleep_1
  
# Dallas sensor/hub component (in my case for the DS18B20 Waterproof Digital Temperature Temp Sensor Probe)
dallas:
  - pin: GPIO4
    update_interval: 5s
    id: dallas_1
  
sensor:
  - platform: dallas
    address: 0x5E02131E08F5AA28
    name: ${display_name} Temperature
    id: dallas_temp_1
    
  - platform: adc
    pin: 39
    name: ${display_name} Battery Voltage
    update_interval: 5s
    attenuation: 2.5db
    accuracy_decimals: 1
    id: battery_voltage_1
    filters:
      - multiply: 4.268367