Publish state to lights via modbus inputs / expose switch as light entity

Hi!

I'm new to using ESPHome and HA. I managed to control a modbus RTU board via ESPHome, but as I'm new to HA aswell (was using iobroker before) I realized, that It's better to define my outputs as lights, so they show up in HA as lights, not switches what they are now...

This is my code now (part of):

binary_sensor:
# Read Inputs Board 1 (32 Channels)
  - platform: modbus_controller
    modbus_controller_id: io_board1
    id: in_1_1
    name: "${name1_1} Input"
    address: 0x00C0
    register_type: holding
    bitmask: 0x01
    internal: true

  - platform: modbus_controller
    modbus_controller_id: io_board1
    id: in_1_2
    name: "${name1_2} Input"
    address: 0x00C0
    register_type: holding
    bitmask: 0x02
    internal: true
# up to 32 ...

# Control Outputs (Digital Outputs to 25IOB16)
script:
  - id: write_output_momentary
    parameters:
      address: int
      controller_id: modbus_controller::ModbusController*
    then:
      - lambda: |-
          auto cmd = modbus_controller::ModbusCommandItem::create_write_single_command(
            controller_id, address, 0x0500);
          controller_id->queue_command(cmd);
button:
# _________________________ BOARD 1 _________________________
  - platform: template
    id: outcmd_1_1
    name: "${name1_1} Output momentary"
    internal: true
    on_press:
      - script.execute:
          id: write_output_momentary
          controller_id: io_board1
          address: 0x0000

  - platform: template
    id: outcmd_1_2
    name: "${name1_2} Output momentary"
    internal: true
    on_press:
      - script.execute:
          id: write_output_momentary
          controller_id: io_board1
          address: 0x0001
# up to 32 ...

switch:
# _________________________ BOARD 1 _________________________
  - platform: template
    id: ch_1_1
    name: "${name1_1}"
    icon: "${icon1_1}"
    restore_mode: DiSABLED
    lambda: |-
      return id(in_1_1).state;
    turn_on_action:
      - button.press: outcmd_1_1
    turn_off_action:
      - button.press: outcmd_1_1
  - platform: template
    id: ch_1_2
    name: "${name1_2}"
    icon: "${icon1_2}"
    restore_mode: DiSABLED
    lambda: |-
      return id(in_1_2).state;
    turn_on_action:
      - button.press: outcmd_1_2
    turn_off_action:
      - button.press: outcmd_1_2
# up to 32 ...

How can I change the switch definition to lights?
It's working right now this way (and should be with switches):

  1. display a switch witch the current state of the input
  2. when changing the switch, fire a modbus command (now it switches, but jumps back to off, if the switch did not turn on / the input stays off)
  3. If the input is set externally (parallel button), the switch should switch to on without firing the modbus command (only publishing)!

I know, I could show/define each entity as light in HA, but' I'd like to avoid defining 80 entites twice, just to list them as lights in HA...

Does someone know a solution?
Is it possible to make a switch. appear as a light. from esphome? - ESPHome - Home Assistant Community
Does not work for me, as I need the light o show the cuurent state based on the modbus input, not the output.

appreciate any hints!

You could make a template output (binary type) and then refer to those outputs as a binary light. There is an example in the template output docs:

thank you for your idea. I tried it this way, but I would need to publish the input state to the light which seems not be possible...

binary_sensor:
# Read Inputs Board 1 (32 Channels)
  - platform: modbus_controller
    modbus_controller_id: io_board1
    id: in_1_1
    name: "${name1_1} Input"
    address: 0x00C0
    register_type: holding
    bitmask: 0x01
    internal: true
    on_state:
      then:
        - lambda: |-
            id(ch_1_1).publish_state(id(in_1_1).state);

script:
  - id: write_output_momentary
    parameters:
      address: int
      controller_id: modbus_controller::ModbusController*
    then:
      - lambda: |-
          auto cmd = modbus_controller::ModbusCommandItem::create_write_single_command(
            controller_id, address, 0x0500);
          controller_id->queue_command(cmd);

output:
  - platform: template
    type: binary
    id: outcmd_1_1
    write_action: 
      then:
        - script.execute:
            id: write_output_momentary
            controller_id: io_board1
            address: 0x0000

switch:
# _________________________ BOARD 1 _________________________
  - platform: output
    id: ch_1_16
    name: "${name1_16}"
    icon: "${icon1_16}"
    output: outcmd_1_16

light:
# _________________________ BOARD 1 _________________________
  - platform: binary
    id: ch_1_1
    name: "${name1_1}"
    icon: "${icon1_1}"
    output: outcmd_1_1
/config/esphome/tableaus.yaml: In lambda function:
/config/esphome/tableaus.yaml:355:28: error: no matching function for call to 'esphome::light::LightState::publish_state(bool&)'
  355 |             id(ch_1_1).publish_state(id(in_1_1).state);
      |             ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
In file included from src/esphome/core/entity_includes.h:18,
                 from src/esphome/core/application.h:39,
                 from src/esphome/components/api/api_frame_helper.h:14,
                 from src/esphome/components/api/api_connection.h:5,
                 from src/esphome.h:4,
                 from src/main.cpp:3:
src/esphome/components/light/light_state.h:139:8: note: candidate: 'void esphome::light::LightState::publish_state()'
  139 |   void publish_state();
      |        ^~~~~~~~~~~~~
src/esphome/components/light/light_state.h:139:8: note:   candidate expects 0 arguments, 1 provided