Include common sensors, possible?

I’ve been trying to separate the three common sensors I use on my devices, but without any success.
But I’m a newbie, at EspHome and yaml, so there’s a high possibility that I’m doing things wrong.

Lets say that I have a device with a configuration like:

...

dallas:
  - pin: D2

sensor:

# Common sensors
  - platform: wifi_signal
    name: "${friendlyname}_RSSI"

  - platform: uptime
    name: "${friendlyname}_Uptime"
  
  - platform: adc
    name: "${friendlyname}_VCC"
######

  - platform: dallas
    id: temp1
    name: "${friendlyname}_temp 1"
    address: 0xEC00000B0EFA5E28
...

I would like to get the sensors between the comments out in a file and add it to all my devices. I’ve tried so far:
Creating the new file with the parent sensor tag works, but does substitution, one or the other.
Tried:

...
dallas:
  - pin: D2

sensor:

<<: !include 10-common-sensors.yaml

  - platform: dallas
    id: temp1
    name: "${friendlyname}_temp 1"
    address: 0xEC00000B0EFA5E28
...

Gives error for the - dallas tag not allowed there.

mapping values are not allowed here
in “/root/config/custom-esp-ds18b20.yaml”, line 33, column 13:
- platform: dallas
^

Adding one indentation to the include throws another error

while parsing a block mapping
in “/root/config/custom-esp-ds18b20.yaml”, line 31, column 3:
<<: !include 10-common-sensors.yaml
^
expected , but found ‘-’
in “/root/config/custom-esp-ds18b20.yaml”, line 33, column 3:
- platform: dallas
^

Also tried:

...

dallas:
  - pin: D2

sensor:

  - platform: dallas
    id: temp1
    name: "${friendlyname}_temp 1"
    address: 0xEC00000B0EFA5E28

<<: !include 10-common-sensors.yaml
...

But gives errors on all the contents of the file.

Failed config

platform: [source /root/config/10-common-sensors.yaml:1]

Component not found: platform.
wifi_signal
name: [source /root/config/10-common-sensors.yaml:2]

Component not found: name.
ACS_RSSI
icon: [source /root/config/10-common-sensors.yaml:3]

Component not found: icon.
mdi:wifi
update_interval: [source /root/config/10-common-sensors.yaml:4]

Component not found: update_interval.
60s
filters: [source /root/config/10-common-sensors.yaml:6]

Component not found: filters.

  • delta: 1
    unit_of_measurement: [source /root/config/10-common-sensors.yaml:11]

Component not found: unit_of_measurement.
minutes
pin: [source /root/config/10-common-sensors.yaml:19]

Component not found: pin.
VCC

Also tried multiple forms of indentation on the file (no, or one tab) to no avail.

Is there a way to accomplish this?

Take a look at packages Configuration Types — ESPHome

1 Like