Update frequency adc for deep sleep -> answered

This works (almost) perfectly:
When waking up, the sensor averages the voltage calculated after (11 updates x 1.3 seconds =) 14.3 seconds and pushes one value out before going back into deep sleep. So that there is only one value pushed out before the end of the “run_duration” of 20 seconds and going back into deep sleep.
I use this code because the first value read after waking up is often off. (The window_size in my code is just one that works for me.)
HOWEVER.
When I keep the ESP out of deep sleep in order to ota, a new value is pushed to HA every 14.3 seconds or so.
I would like to push a value to HA only every hour or so when awake for ota (and keeping one averaged value whenever waking up from deep sleep as it works now.)
I Googled around without succes. I tried adding a ‘heartbeat’ of 15 minutes without succes: nothing is sent to HA before going back to sleep.
Here is my code as it is now:

sensor:
  - platform: adc
    name: "Sensor Battery Voltage"
    pin: GPIO35
    accuracy_decimals: 2
    update_interval: 1.3s
    attenuation: 11dB
    unit_of_measurement: "V"
    icon: mdi:battery-medium
    filters:
      - multiply: 2.0
      - sliding_window_moving_average:
             window_size: 11
             send_every: 11
             send_first_at: 11

Anyone?
(Please be aware: I am a beginner in yaml and English is not my mother language.)

You can use skip_initial: filter before your moving average to handle them if you want.

You can try a throttle: filter after your moving average to increase the push interval.

Thanks for your reply.
(The skip_initial before the moving average will not change anything to the frequency of pushing a new value every 14.3 seconds while doing OTA and keep awake for testing other sensors settings, will it?)

Will give that throttle filter a try after a good night’s sleep.

It shouldn’t. But it should make your moving average value more accurate by removing an outlier/ data quality concern.

BTW I prefer moving median rather than moving average because as median is a robust measure of center it is not sensitive to spikes/outliers.

https://www.google.com/search?q=median+robust&oq=median+robust+&gs_lcrp=EgZjaHJvbWUyCggAEEUYFhgeGDkyBwgBEAAYgAQyCAgCEAAYFhgeMgoIAxAAGA8YFhgeMggIBBAAGBYYHjIICAUQABgWGB4yCAgGEAAYFhgeMggIBxAAGBYYHjIICAgQABgWGB4yDQgJEAAYhgMYgAQYigUyDQgKEAAYhgMYgAQYigUyDQgLEAAYhgMYgAQYigUyDQgMEAAYhgMYgAQYigUyBwgNECEYjwLSAQg1NDIxajBqNKgCALACAA&sourceid=chrome-mobile&ie=UTF-8

1 Like

A very big thanks for your help and patience!

  • 100% agree with your proposal to change from sliding_window_moving_average: to median:. Done. skip_initial still to add after more testing.
  • I have now added the throttle: filter. There indeed seems to be no second value passed forward to HA after my famous 14.3 seconds when deep sleep remains switched off (through the ‘well known’ script with input_boolean evaluation). But I am still confused about the wording in the ESPHome documentation:

When this filter gets an incoming value, it checks if the last incoming value is at least specified time period old.

It gives me (!) the impression that this will pass forward the first value (x) coming from the sensor (and do nothing with the median filter after that but wait until the throttle time is passed). Or will it first calculate the median and then consider that as first incoming value (y) for the throttle? Otherwise it is useless to add the median: filter. I cannot make that out from what I see in the logs and HA: a value is passed forward but which one? x or y?
My confusion held me from using throttle: in the first place and made me starting this thread :disappointed_relieved:.

My new yaml:

sensor:
  - platform: adc
    name: "Sensor Battery Voltage"
    pin: GPIO35
    accuracy_decimals: 2
    update_interval: 1.3s
    attenuation: 11dB
    unit_of_measurement: "V"
    icon: mdi:battery-medium
    filters:
      - multiply: 2.0
      - median:
          window_size: 11
          send_every: 11
          send_first_at: 11
      - throttle: 15min

You need to think of filters like there is the raw incoming data, and then they pass through one at a time in the order you’ve put them in.

When you have multiple filters, the input of a later one is the output of the previous one. They build on top of each other. Well this is how I understand them to work.

So the throttle fliter would be looking at the incoming median values. The median is looking at the multiplied values etc.

From the docs.
“Filters are processed in the order they are defined in your configuration.”

Thanks a 1,000,000 for your patience and cristal clear explanations.

1 Like