Multiple modbus slaves, identical sensors request

I was reading the modbus manual to configure multiple modbus slaves with identical sensors. Just couldn’t find what I was looking for.

My request is about the following:
In this example Slave 1, Slave 2 until Slave N are of the same types of slaves with identical sensors. Think of power meters, for example measurements of energy consumption/return of the Solar Panels, Heat pump and Air conditioning.
Like on image:
Modbus1

The three modbus devices in this example will have different device addresses on the same bus, which are connected using modbus. But the way it is configured (as far as I understand now), I would need to repeat these sensor entries for all three modbus device, containing a lot of redundant configuration. See below. Here you can see that only the:

  • name
  • slave
  • unique_id
    are different. All other parts are redundant configuration.
modbus:
- name: SDM
    type: serial
    baudrate: 115200
    bytesize: 8
    method: rtu
    parity: E
    port: /dev/ttyUSB0
    stopbits: 1
    timeout: 1
    delay: 1
    retry_on_empty: true
    close_comm_on_error: false
    retries: 3
    sensors:
      - name: "PV Phase 1 line to neutral volts"
        scan_interval: 5
        slave: 1
        address: 0
        input_type: input
        count: 2
        precision: 2
        data_type: float32
        unit_of_measurement: V
        device_class: voltage
        state_class: measurement
        unique_id: "PV Phase 1 line to neutral volts"
      - name: "Heat pump outside Phase 1 line to neutral volts"
        scan_interval: 5
        slave: 2
        address: 0
        input_type: input
        count: 2
        precision: 2
        data_type: float32
        unit_of_measurement: V
        device_class: voltage
        state_class: measurement
        unique_id: "Heat pump outside Phase 1 line to neutral volts"
      - name: "Heat pump inside Phase 1 line to neutral volts"
        scan_interval: 5
        slave: 3
        address: 0
        input_type: input
        count: 2
        precision: 2
        data_type: float32
        unit_of_measurement: V
        device_class: voltage
        state_class: measurement
        unique_id: "Heat pump inside Phase 1 line to neutral volts"

I couldn’t find a way to define multiple modbus slaves with identical sensors in a smart way. The goal would be to define the modbus slave address once and create the sensors for the slaves also ones.

Perhaps that is already possible with current configuration options in a different or smarter way. I just couldn’t find it in the documentation, hence my request.

Pretty sure other users are running into this as well. Could someone give me hints or tips or or even better some examples to read to achieve this? If not already possible, this would be a nice addition to the modbus integration in HA.

Thank you very much in advance!

PS This may not be 100% the right place to ask. If so please direct to the correct place. Then I will delete this request.

1 Like

YAML anchors

modbus:
- name: SDM
    type: serial
    baudrate: 115200
    bytesize: 8
    method: rtu
    parity: E
    port: /dev/ttyUSB0
    stopbits: 1
    timeout: 1
    delay: 1
    retry_on_empty: true
    close_comm_on_error: false
    retries: 3
    sensors:
      - name: "PV Phase 1 line to neutral volts"
        unique_id: "PV Phase 1 line to neutral volts"
        slave: 1
        <<: &phase_sensor
          scan_interval: 5
          address: 0
          input_type: input
          count: 2
          precision: 2
          data_type: float32
          unit_of_measurement: V
          device_class: voltage
          state_class: measurement
          
      - name: "Heat pump outside Phase 1 line to neutral volts"
        unique_id: "Heat pump outside Phase 1 line to neutral volts"
        slave: 2
        <<: *phase_sensor
          
      - name: "Heat pump inside Phase 1 line to neutral volts"
        unique_id: "Heat pump inside Phase 1 line to neutral volts"
        slave: 3
        <<: *phase_sensor

I will try this practice. If I read the syntax, this is already a very good step in the direction I would like it to go in. It would be even nicer if name and unique_id also could be use repeatedly.

modbus:
  - name: SDM
    type: serial
    baudrate: 115200
    bytesize: 8
    method: rtu
    parity: E
    port: /dev/ttyUSB0
    stopbits: 1
    timeout: 1
    delay: 1
    retry_on_empty: true
    close_comm_on_error: false
    retries: 3
    sensors:
      - name: {VARIABLE} "Phase 1 line to neutral volts"
        scan_interval: 5
        slave: 1
        address: 0
        input_type: input
        count: 2
        precision: 2
        data_type: float32
        unit_of_measurement: V
        device_class: voltage
        state_class: measurement
        unique_id: {VARIABLE} "Phase 1 line to neutral volts"
      - name: {VARIABLE} "Phase 1 line to neutral volts"
        scan_interval: 5
        slave: 2
        address: 0
        input_type: input
        count: 2
        precision: 2
        data_type: float32
        unit_of_measurement: V
        device_class: voltage
        state_class: measurement
        unique_id: {VARIABLE} "Phase 1 line to neutral volts"
      - name: {VARIABLE} "Phase 1 line to neutral volts"
        scan_interval: 5
        slave: 3
        address: 0
        input_type: input
        count: 2
        precision: 2
        data_type: float32
        unit_of_measurement: V
        device_class: voltage
        state_class: measurement
        unique_id: {VARIABLE} "Phase 1 line to neutral volts"
      - name: {VARIABLE} "Phase 1 current"
        scan_interval: 5
        slave: 1
        address: 6
        input_type: input
        count: 2
        precision: 2
        data_type: float32
        unit_of_measurement: A
        device_class: current
        state_class: measurement
        unique_id: {VARIABLE} "Phase 1 current"
      - name: {VARIABLE} "Phase 2 current"
        scan_interval: 5
        slave: 1
        address: 8
        input_type: input
        count: 2
        precision: 2
        data_type: float32
        unit_of_measurement: A
        device_class: current
        state_class: measurement
        unique_id: {VARIABLE} "Phase 1 current"
      - name: {VARIABLE} "Phase 3 current"
        scan_interval: 5
        slave: 1
        address: 10
        input_type: input
        count: 2
        precision: 2
        data_type: float32
        unit_of_measurement: A
        device_class: current
        state_class: measurement
        unique_id: {VARIABLE} "Phase 1 current"

Just omit the name. If you don’t care about adjusting settings via the UI, omit the unique_id.

I would like to keep the name and unique_id. :blush:

Then there’s no simple way to duplicate the entry.

name: &name "blah"
unique_id: *name

That’s a pity.

Maybe the post below explains a little better what I want to achieve. Could you take a look at this please.

Thanks

1 Like

Yes, I understand what you want and it’s not possible. Anchors are all you have to use.

You need to duplicate code

Can you give some more hints, I don’t understand what you mean.

He’s saying to copy/paste. Trust me, what you’re looking for does not exist.

Haha, I’m trusting you and working on my config. First part is already working. The term nikito7 used was just confusing and thought he may have meant a different method.

Thank you all for the help! If it works well, will share my result so that others may also learn from it as I did today. :smiley:

A large number of users don’t speak English as their first language. Got to treat every comment like it was google translated.

I take this one as a compliment though, as I’m a non-native speaker myself. :blush:

Not shure if I’m right here but since I’m new to HA (switched from OH) I won’t open a new topic already.

I’ve a similar setup exept my energy-meters (6 SDM120 und one SDM630) are connected thue a single RTU (RS485) to TCP Device.
So if I try to do this setup:

modbus:
  - name: SDM630
    type: rtuovertcp
    host: 192.168.33.28
    port: 502
    sensors:
      - name: Phase 1 voltage
        unique_id: total_l1_voltage
        unit_of_measurement: V
        address: 0
        state_class: measurement
        device_class: voltage
        slave: 2
        count: 2
        precision: 2
        scan_interval : 60
        input_type: input
        data_type: float32

I don’t get any answer.
Same in RED-Node works fine:



Has someone any ideea what’s wrong with my modbus-config? Any hint?