Is there some way to delay setting up a sensor, is that setup_priority?

I have an ESP32 with three I2C sensors hooked up to it, all Adafruit with the STEMMA QT connectors. The one sensors is a PMSA003i and it randomly doesn’t work on boot most of the time for me. If I reboot over and over I can get it to work, and then it’ll work fine for weeks, but if I have to unplug the ESP32 I’ll need to mess with it again to get it working.

The other two sensors (SCD40 and SGP40) are connected via daisy chain from the PMSA003i, and they work fine every time, so it makes me think it’s very unlikely it’s a connection issue.

Googling this I found this issue, which seems very similar to my problem. The issue is closed now so I can’t post there, but the one post says:

Per ssieb on discord I tried setting setup_priority to 250 to give the pmsa more time to initialize. This worked and the sensor now initializes consistently.

I’d like to try that, but I can’t seem to find any documentation about how to set setup_priority on a sensor. I have found some stuff about on_boot and then priority but I’m not sure if that’s the same thing as setup_priority and I don’t see any examples of setting up a sensor after on_boot.

So my questions are:

  1. How do I set setup_priority on a sensor?
  2. Is there some other way I can delay trying to set up a sensor to ensure the sensor has had a enough time to start up fully?
1 Like

I was curious about this and searched the ESPHome Discord for it.

It seems some people are using it like this:

sensor:
  - platform: am2320
    setup_priority: -100
    address: 0x5C
    temperature:
      name: "temp02"
    humidity:
      name: "humi02"
    update_interval: 60s

I couldn’t find anything on a quick docs check.

2 Likes

Thanks, that seems to have worked. It’s hard to say for sure, since it was seemingly random, but it just worked 3 out of 3 times I tried unpluging and repluging it in.

Here’s some documentation on what the available priorities are. I used -100 which may be much later than necessary, but it still reports values within a few seconds, so fine for me.

Very weird. I looked at the docs and the term setup_priority is not explained except for in respect of a custom component, and in relation to using a modbus controller.

For the latter, it is used in a number of examples, but nowhere in the docs is it made clear (afaict) that it is of more general application.

1 Like

I had this exact same issue with the Adafruit pmsa003i not reporting values on boot. Reinstalling once it was powered up seemed to work but replugging would cause it to not report.

Will test this out and see if it permanently resolves the issue, thanks for the tip!

Also try testing with a good quality power supply, cables, connections.

Smells a bit like too much power demand on hard boot.

I found this topic after having this exact problem with this sensor and esphome not detecting the i2c device like 90% of the time, and setting the setup_priority to -100 fixed it!

1 Like