Component variable (like address) to be configurable using global variable

Hi all, I’m developing some modules where each of them needs an unique modbus ID. Due to easy module replacement (or initial configuration) would be perfect to have an option to set ID with f.i. dip switches. In that case, I’d like to check switches state during on_boot phase, calculate ID and populate this value to a global variable, and finally use it as a modbus controller’s address, like below. Unfortunately, it doesn’t compile with This option is not templatable!. Any chance to achieve this? Many thanks!

INFO Reading configuration .\x-CAN.yaml...
Failed config

modbus_controller: [source .\x-CAN.yaml:63]
  - modbus_id: modbus_server

    This option is not templatable!.
    address: !lambda |-
      return id(modbus_id).state;
    server_registers:

The api has a set_address function. Maybe this could be used in a trigger for on_boot or on_dip_switch_state_change.

Thanks, but that function isn’t exported to API, so couldn’t find a way to use it.

Should be something like below lambda. This compiled for me without errors but I didn’t have any hardware handy to test it.

button:
  - platform: template
    name: "button"
    id: test_btn
    on_press:
    - lambda: |-
        id(modbus_device).set_address(0xFF);
1 Like

Awesome, it’s working! Many thanks!

1 Like