Making a sensor deep sleep

I have a ESP8266-based sensor. It seems to gather data properly, but it was suggested that I have it go into a deep sleep to make the battery last longer. Following is a part of the sensor’s yaml file:

esphome:
  name: luxsensor
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: <SSID>
  password: pwd

deep_sleep:
  run_duration: 60s
  sleep_duration: 10min

sensor:
  - platform: bh1750
    name: "Light Level"
    address: 0x23
    update_interval: 1s

I have this sensor on the homepage of Home Assistant and when I click on it, I see a chart of data. When I zoomed way in, I noticed that data points are two minutes apart, which strongly suggests I have something wrong in the yaml. Any ideas?

Take a look at the esphome log.

Also, for a reason I now forget, deep sleep is better used with mqtt than the api.

The log shows a data point every second - way too fast! Not sure that I can make this a MQTT device…

Why did you set it to 1s then?

I suspect that some parameters are being set someplace other than my yaml file. Here is the updated yaml file:

esphome:
  name: luxsensor
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: <SSID>
  password: <pwd>

deep_sleep:
  run_duration: 60s
  sleep_duration: 10min

sensor:
  - platform: bh1750
    name: "Light Level"
    address: 0x23
    update_interval: 90s

Once I updated it, I rebooted the HA system. When I looked at the log, I see this:

[17:57:42][C][bh1750.sensor:118]: BH1750 'Light Level'
[17:57:42][C][bh1750.sensor:118]:   Device Class: 'illuminance'
[17:57:42][C][bh1750.sensor:118]:   State Class: 'measurement'
[17:57:42][C][bh1750.sensor:118]:   Unit of Measurement: 'lx'
[17:57:42][C][bh1750.sensor:118]:   Accuracy Decimals: 1
[17:57:42][C][bh1750.sensor:119]:   Address: 0x23
[17:57:42][C][bh1750.sensor:124]:   Update Interval: 1.0s
[17:57:42][C][restart:022]: Restart Switch 'Lux Sensor Restart'

[17:57:42][D][bh1750.sensor:159]: 'Light Level': Got illuminance=17.9lx
[17:57:43][D][bh1750.sensor:159]: 'Light Level': Got illuminance=17.9lx

I have no idea why this is updating every second. Also, no idea where the system is getting the device class or unit of measurement. Perhaps there is a file that defines the BH1750 someplace? Perhaps that file is setting the update time to 1 sec and my yaml is not over-riding that?

The unit_of_measurement and so forth is set here esphome/sensor.py at a8ceeaa7b0616b132091f2fcd1d02c137238e4d8 · esphome/esphome · GitHub

I suspect you haven’t properly over-ridden the 60s default. Or actually compiled and uploaded your code.

Thanks, but…

(Note: This Raspberry Pi is running hassio) I did not see the file sensor.py, so I cd’ed to the root directory and did a ‘find . -name “sensor.py” -print’ and nothing was returned. I looked in the folder /root/config/esphome/.esphome/build and a see a folder named luxsensor. However, almost all of the files in this folder are source code.A quick glance at main.cpp shows the IP address, but nothing else jumped out. I cannot find a folder named esphome/components. I then tried ‘grep -rnw . -e “UNIT_LUX”’ and that too returned nothing. Now what?

Also, I am quite certain that the code was compiled and uploaded because from the device name on the ESP page, I selected (three dots)->Install->Wirelessly, watched it build and upload the code…

I recently un-MQTT’d my sleeping ESP devices. It didn’t make sense to add MQTT just for convincing my device to go into OTA mode.

Is it updating home assistant every second, or is that just the internal workings of the esp C++ code. It is conceivable that the esp gets a value every second, but only passes it on to HA every 60 or 90s.

nickrout - thanks for the reply, but…

  • I have no idea how to adjust the C++ code to reset the time - no idea which parameter to change or if such a change would impact every ESP device.
  • More importantly, when I go to the ESPHome UI page and click luxsensor->Logs, I see it updating the value every second. As such, I suspect that it is sending data every second.

I removed the deep_sleep stuff from the yaml file, changed the update_interval to 90s, and watching the log, only one data point was sent every 90 seconds. This issue seems to be resolved. Now, if I could just figure out how to use the sensor value as a trigger, or condition, in an automation…

Do you mean an esphome automation or a HA automation?

I wasn’t even aware of the fact that there are two kinds of automatons! However, I suspect that this needs to be a HA automation because the switch to be controlled is not an ESP device.

Well just the same as any other automation then.