How to combine sensors from different esp32s?

Hello all,

I am trying to configure my system so I can start with a nice organization from the beginning. At the moment my system is not very complicated by I am planning of adding various things to it.

What I have at the moment is as follows:

  1. Esp32 which has three bluetooth temperature and humidity sensors attached to it and one DHT22 temperature and humidity hardwired to it. This one sits upstairs in the house and monitors the temperature and humidity there.

  2. Esp32 with I water level sensor which is a normal binary sensor and a switch that triggers a relay. This one is used to water flowers and I plan to add a soil humidity sensor when it arrives from China.

  3. Esp32 with bluetooth temperature and humidity sensor connected to it (plus whatever will be added in the future). This one monitors tempearture downstairs.

I would like to group all temperature sensors in a sensor.yaml file. Is such a thing possible? How would I tell first esp32 which sensors are attached to it and which sensors are attached to the rest of the boards?

The reason why I want this is becuase I would like to write a python script eventually where I would like to read the temperature values in all the rooms and perform some operations with them and chose when to turn on a water heater.

I am not sure if it makes sense what I want to do but I am quite confused about how to organize things to make them more accessible from python scripts.

Thank you!

Hello @luciandf,

I’d just go with a name pattern for sensors, this will be pretty easy to handle with a Python script and does not introduce complexity.

How do you interface with the ESP32s? REST, MQTT, …?
Are they Running ESPHome, Arduino, …?


Thank you for your reply! At the moment I use Esphome with my esp32s about how they interface I am not sure how to answer. They connect to the wifi in the house and then they are discovered by HA.

How do I setup the name patterns for the sensors? I would like to (for example) have a pattern for a group of temperature sensors (the ones in the rooms), another group for temperature sensors for outside etc. How so I do this? Do I need separate sensor files?

I appreciate any help you can provide!

While researching how to make a python script for home assistant (I am quite new at Python as well, but I see it as a very good opportunity to learn) I stumbled upon this repository:

Here there is a script which counts how many lights are on. I figured that if I manage to group all the sensors I am interested in for the script as this guy did then I can use them in the same way.

for entity_id in lights:
  state = hass.states.get(entity_id)
  # filter out bulbs not on
  if (state.state == 'on'):
    if ('tradfri' not in
      lightStatus = 'on'
      lightsOn = lightsOn + 1

I would do something like

for entity_id in tempearture_sensors:
  state = hass.states.get(entity_id)
  # calculate averages, make decisions etc.

The config files in the repo seems wery well organized and that inspired me to do the same but the difference is that he accesses the sensors differently.

Should I switch from esphome to MQTT ? would this be a better way? How do I setup MQTT?

Sorry for all these questions!


name patterns are nothing technical, it’s a convention I’d say.
E.g. you could add a prefix/suffix to the entity names like this (assuming you have a temperature sensor sensor.living_room):


You just have to decide for a grouping prefix/suffix, this may be room_temperature for the use case you mentioned. This would result in the names:




This allows you to loop these sensors like this:

for entity_id in lights:
  state = hass.states.get(entity_id)
  # check if this entity is a room_temperature
  if ('room_temperature' in
    # check if this sensor is below my defined threshold
    if (state.state < '20'):
      # count the number of sensors below threshold
      numBelowThreshold = numBelowThreshold  + 1

In the ESPHome config file you define the name of a sensor (entity), this is then used as the entity_id afaik.
So by changing the entity names in the ESPHome config files, you could apply a name pattern easily.

I wouldn’t go away from ESPHome if you have it up and running!
I have to mention that I did only use ESPHome once and didn’t use Python scripts up to now, so I can’t actually tell you how to enable your water heater but I’m sure someone else can and does.