Read sensor, post MQTT message, then deep sleep

I’m trying to set up a ESP32 with four Dallas sensors. I am able to successfully read from all of them, connect the ESP32 to wifi, etc. However, I now want to set it up so that it works well on a battery so I can put it outside in the yard (it’s for a soil temperature monitoring application so I can optimize my garden plantings).

What I really want to do is set up a deep sleep loop. My understanding is that waking up from deep sleep activates the esphome.on_boot trigger, so I could use that in my automation/template. There’s an action (mqtt.publish) that will allow me to post the sensor values to the MQTT broker as well as an action to trigger another round of deep sleep (deep_sleep.enter). However, I cannot find an “action” that let’s me read in each of the temperatures. This is the missing step, then I think I can set up an automation loop that would do:

  • Wake up from deep sleep
  • Read in each of the temperature sensor values
  • Post to MQTT
  • Check to see if an OTA update is pending
  • Check the time to determine when the next boot time should be (so that we can poll the temperature sensor at regular intervals)
  • Enter deep sleep for the calculated time.

Is this feasible? How would I read in the temperature sensor values on my own schedule rather than what’s defined in the sensor library?

component.update but it does not work with all sensors.

1 Like

Thanks so much! I will try this out and post the script if I’m successful.

So, I found out that I don’t need to do anything fancy. The moment I added the MQTT integration and the sensors, the Dallas sensors gets scanned on boot and updated values are read in. Then, when MQTT launches, it automatically posts the values. Keeping the device on for 1 second seems to be enough for everything to boot up, read in sensors, send the MQTT messages, then it will sleep for 10 minutes. Nice and simple.

I still need to add a flag so that I can prevent deep sleep when I want an OTA update.

esphome:
  name: garden

esp32:
  board: esp32thing
  framework:
    type: arduino

logger:

ota:
  password: "SECRET"

mqtt:
  broker: 192.168.0.2

dallas:
  - id: 'dallas_hub_1'
    update_interval: '1h'
    pin:
      number: GPIO16
      mode:
        input: true
        pullup: true
  - id: 'dallas_hub_2'
    update_interval: '1h'
    pin:
      number: GPIO19
      mode:
        input: true
        pullup: true


wifi:
  ssid: "MyIOT"
  password: "SECRET" 
  fast_connect: true

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

captive_portal:

deep_sleep:
  id: "deep_sleep_1"
  run_duration: '1s'
  sleep_duration: '10min'

sensor:
  - platform: dallas
    name: 'Greenhouse soil temperature'
    address: XXXXXXXXXXXXXXXXXXX
    dallas_id: 'dallas_hub_1'
  - platform: dallas
    name: 'Greenhouse air temperature'
    address: XXXXXXXXXXXXXXXXXXX
    dallas_id: 'dallas_hub_1'
  - platform: dallas
    name: 'Garden soil temperature'
    address: XXXXXXXXXXXXXXXXXXX
    dallas_id: 'dallas_hub_2'
  - platform: dallas
    name: 'Garden air temperature'
    address: XXXXXXXXXXXXXXXXXXX
    dallas_id: 'dallas_hub_2'

You can do that with a template switch. When you create the automation to turn on the switch just be sure to set the retain flag to true.