Settings up sensors with a for loop?

I have some BLE climate sensors which use their mac address by default. Instead of changing the sensor name which I would have to do in the event of a SNAFU or setup change, I currently have a ridiculous number of lines doing this for every device. [ I also check to see if a remote Pi Zero is active which is closer to the devices and use that if it hasn’t shat itself ]

sensor:  
  - platform: template
    sensors: 
      temperature_kitchen:
        friendly_name: Kitchen Temperature
        unit_of_measurement: "°C"
        value_template: >
          {% set t1 = states('sensor.ble_temperature_A4C1381992C6') %}
          {% set t2 = states('sensor.ble_temperature_') %}
          {{ t2 if t1 == 'unknown' else t1 }}

      humidity_kitchen:
        friendly_name: Kitchen Humidity
        unit_of_measurement: "%"
        value_template: >
          {% set t1 = states('sensor.pz_ble_humidity_kitchen') %}
          {% set t2 = states('sensor.ble_humidity_A4C1381992C6') %}
          {{ t2 if t1 == 'unknown' else t1 }}

      battery_kitchen:
        friendly_name: Kitchen Battery
        unit_of_measurement: "%"
        value_template: >
          {% set t1 = states('sensor.pz_ble_humidity_kitchen') %}
          {% set t2 = states('sensor.ble_battery_A4C1381992C6') %}
          {{ t2 if t1 == 'unknown' else t1 }}

Is it possible to setup the above sensors with a for loop in configuration.yaml? How would I word it with these details?

The sensors are all
sensor.ble_temperature_A4C1381992C6
sensor.ble_humidity_A4C1381992C6
sensor.ble_battery_A4C1381992C6

example

sensorList = {kitchen: 'A4C1381992C6', bathroom: 'a4c138229679', kids: 'a4c138103809'}
for name, mac in sensorList.interitems():


No.

Well then :frowning:

What you’re requesting is effectively a YAML pre-processor. You want to script the production of YAML statements, prior to when Home Assistant’s YAML processor begins loading the statements.

There’s no YAML pre-processor in Home Assistant.

So, what tom_l said (no).

Is there a more optimal way to do the above for 5+ sensors converting to friendly names without changing the entity? Or should I just change the entity and change it everytime I need to readd them

If all you wanted to do was change each entity’s name to something friendlier, that’s possible without having to create a Template Sensor. However, you appear to also want to include some sort of failover capability, to another sensor’s value, in the event the value is unknown. That means you’re obligated to create a Template Sensor for each and every sensor. There are no shortcuts for the path you’ve chosen.

1 Like