How to publish sensor states at boot?

Hi,

Tried my first EspHome project with MQTT. Sensor states are published when they change (binary sensor), but nothing is published at boot.

How can I force the sensor states (all) to be published on start instead of waiting for a change ?

Appreciate your help !
Thx,
Deva

Would the update_entity service work from the backend?

That might work, but I want to make it work without HA too. It should work OK if HA is down …

It should work out of the box with api any reason not using it instead of mqtt

Using a lambda call you can force an update, you’ll have to trigger it somehow, like an automation when mqtt gets connected, unfortunately I don’t know much about mqtt to assist you more.

I would have expected it to do it automatically though.

You should be able to put something int he “on boot” of ESPHome config:

Something like below - but to get status, you would probably need to make a template… and you would have to do it for each sensor… :

esphome:
  # ...
  on_boot:
    priority: -10
    # ...
    then:
      - mqtt.publish:
          topic: !lambda |-
            if (id(reed_switch).state) return "topic1";
            else return "topic2";
          payload: !lambda |-
            return id(reed_switch).state ? "YES" : "NO";

Not very pretty… but I think that would work.
I’ll look and see if I can find anything cleaner…

DeadEnd