Send sensor value when api connects

I have a battery powered device that wakes up for a short time when running on battery but stays awake when externally powered. I also have a battery voltage sensor in that device that by default sends it value once every other minute. Right now I use sliding_window_moving_average filter to send one value out after boot and then every two minutes but sometimes connecting to the api takes a bit longer so the first value doesnt make it and the device is sleeping again before the next value gets sent.

So my question: Is there a way to wait with sending the first value until the api is connected and after that every 2 minutes? I did not find anything about that in the documentation.

There’s a condition for api connection but no trigger. You would have to poll it with the interval component.

https://esphome.io/guides/automations.html#interval

https://esphome.io/components/api#api-connected-condition

I see, but how would I trigger sending the value from the battery sensor then? The docs only describe sending a value to the sensor, but not how to trigger the sensor to take and send a measurement now…

No. Do this:

OK maybe there’s a misunderstanding here.

Right now I have this:

sensor:
  # battery voltage measurement for guesstimating battery state
  - platform: adc
    name: "Battery voltage"
    id: battery_voltage
    pin: GPIO34
    accuracy_decimals: 2
    update_interval: 5s
    entity_category: DIAGNOSTIC
    device_class: VOLTAGE
    state_class: measurement
    internal: False
    attenuation: auto
    filters:
      - sliding_window_moving_average:
          window_size: 24
          send_every: 24
          send_first_at: 1
      - multiply: 2.0  # multiply by 2 because of voltage divider

This sends the first value after 5 seconds and then one every 2 minutes but sometimes the first one doesnt get through because the api isnt connected yet.

When using the interval I would probably do something like this:

interval:
  - interval: 1s
    then:
      - if:
          condition:
            api.connected:
          then:
            # trigger sensor measurement

But the last line is what is missing for me right now… of course I would need to add another condition to make sure this only happens once because i do not want it sent every second but that I can do :wink:

Try the update component action to update your ADC, https://esphome.io/guides/automations#component-update-action

Awesome, that works!

This is my code now. I added a global bool voltage_sent and this to my interval section:

interval:
  - interval: 1s
    then:
      - if:
          condition:
            lambda: return (global_api_server->is_connected() && !id(voltage_sent));
          then:
            - component.update: battery_voltage
            - globals.set:
                id: voltage_sent
                value: "1"

Though I am not sure if global_api_server->is_connected() is the best way but that’s what google found to get the api connected state in a lambda… also missing in the documentation as far as I can see

1 Like