Modbus: read coils not as binary

Below is my version of this implementation. The idea was taken from Kymc0’s implementation, but mine is a bit shorter and is fully synchronous (does not require polling of the status).

The Modbus part:

modbus:
  - name: <your_modbus_converter>
    <your_modbus_converter_config>
    binary_sensors:
      - name: Waveshare relay status
        address: 0x0000
        input_type: coil
        scan_interval: 0
        slave: 0x01
        slave_count: 7
        unique_id: waveshare_relay_sensor
    sensors:
      - name: Waveshare relay current device address
        slave: 0x01
        input_type: holding # 0x03
        address: 0x4000
        data_type: uint16
        scan_interval: 0
      - name: Waveshare relay software version
        slave: 0x01
        input_type: holding # 0x03
        address: 0x8000
        data_type: uint16
        scale: 0.01
        offset: 0
        precision: 2
        scan_interval: 0
    switches:
      - name: Waveshare relay channel 1
        slave: 0x01
        address: 0x0000
        write_type: coil
        command_on: 0xFF00
        command_off: 0x0000
      - name: Waveshare relay channel 2
        slave: 0x01
        address: 0x0001
        write_type: coil
        command_on: 0xFF00
        command_off: 0x0000
      - name: Waveshare relay channel 3
        slave: 0x01
        address: 0x0002
        write_type: coil
        command_on: 0xFF00
        command_off: 0x0000
      - name: Waveshare relay channel 4
        slave: 0x01
        address: 0x0003
        write_type: coil
        command_on: 0xFF00
        command_off: 0x0000
      - name: Waveshare relay channel 5
        slave: 0x01
        address: 0x0004
        write_type: coil
        command_on: 0xFF00
        command_off: 0x0000
      - name: Waveshare relay channel 6
        slave: 0x01
        address: 0x0005
        write_type: coil
        command_on: 0xFF00
        command_off: 0x0000
      - name: Waveshare relay channel 7
        slave: 0x01
        address: 0x0006
        write_type: coil
        command_on: 0xFF00
        command_off: 0x0000
      - name: Waveshare relay channel 8
        slave: 0x01
        address: 0x0007
        write_type: coil
        command_on: 0xFF00
        command_off: 0x0000
      - name: Waveshare relay all channels
        slave: 0x01
        address: 0x00FF
        write_type: coil
        command_on: 0xFF00
        command_off: 0x0000

The only non-Modbus extra piece is the following automation that reacts to the status change of any of the switches and initiates the update of the Modbus binary sensor (status of all channels).

- id: "<some_unique_id>"
  alias: Waveshare relay fetch channel status
  mode: queued
  max: 10
  trigger:
    - id: any_relay_channel_toggled
      platform: state
      entity_id:
        - switch.waveshare_relay_channel_1
        - switch.waveshare_relay_channel_2
        - switch.waveshare_relay_channel_3
        - switch.waveshare_relay_channel_4
        - switch.waveshare_relay_channel_5
        - switch.waveshare_relay_channel_6
        - switch.waveshare_relay_channel_7
        - switch.waveshare_relay_channel_8
        - switch.waveshare_relay_all_channels
      not_to:
        - "unknown"
        - "unavailable"
  action:
    - alias: Get current status of all relay channels
      service: homeassistant.update_entity
      data:
        entity_id: binary_sensor.waveshare_relay_status

Statuses of the switches and statuses of individual relay channels are separate entities that should be visualized together. An example Lovelace card:

square: false
type: grid
cards:
  - type: tile
    entity: binary_sensor.waveshare_relay_status
    name: Channel 1
    show_entity_picture: false
    vertical: true
    tap_action:
      action: none
    icon_tap_action:
      action: none
    state_content:
      - state
      - last-changed
  - type: tile
    entity: binary_sensor.waveshare_relay_status_1
    name: Channel 2
    show_entity_picture: false
    vertical: true
    tap_action:
      action: none
    icon_tap_action:
      action: none
    state_content:
      - state
      - last-changed
  - type: tile
    entity: binary_sensor.waveshare_relay_status_2
    name: Channel 3
    show_entity_picture: false
    vertical: true
    tap_action:
      action: none
    icon_tap_action:
      action: none
    state_content:
      - state
      - last-changed
  - type: tile
    entity: binary_sensor.waveshare_relay_status_3
    name: Channel 4
    show_entity_picture: false
    vertical: true
    tap_action:
      action: none
    icon_tap_action:
      action: none
    state_content:
      - state
      - last-changed
  - type: tile
    entity: binary_sensor.waveshare_relay_status_4
    name: Channel 5
    show_entity_picture: false
    vertical: true
    tap_action:
      action: none
    icon_tap_action:
      action: none
    state_content:
      - state
      - last-changed
  - type: tile
    entity: binary_sensor.waveshare_relay_status_5
    name: Channel 6
    show_entity_picture: false
    vertical: true
    tap_action:
      action: none
    icon_tap_action:
      action: none
    state_content:
      - state
      - last-changed
  - type: tile
    entity: binary_sensor.waveshare_relay_status_6
    name: Channel 7
    show_entity_picture: false
    vertical: true
    tap_action:
      action: none
    icon_tap_action:
      action: none
    state_content:
      - state
      - last-changed
  - type: tile
    entity: binary_sensor.waveshare_relay_status_7
    name: Channel 8
    show_entity_picture: false
    vertical: true
    tap_action:
      action: none
    icon_tap_action:
      action: none
    state_content:
      - state
      - last-changed
  - show_name: true
    show_icon: true
    type: button
    tap_action:
      action: toggle
    entity: switch.waveshare_relay_channel_1
    name: Channel 1
    show_state: true
    icon_height: 40px
  - show_name: true
    show_icon: true
    type: button
    tap_action:
      action: toggle
    entity: switch.waveshare_relay_channel_2
    name: Channel 2
    show_state: true
    icon_height: 40px
  - show_name: true
    show_icon: true
    type: button
    tap_action:
      action: toggle
    entity: switch.waveshare_relay_channel_3
    name: Channel 3
    show_state: true
    icon_height: 40px
  - show_name: true
    show_icon: true
    type: button
    tap_action:
      action: toggle
    entity: switch.waveshare_relay_channel_4
    name: Channel 4
    show_state: true
    icon_height: 40px
  - show_name: true
    show_icon: true
    type: button
    tap_action:
      action: toggle
    entity: switch.waveshare_relay_channel_5
    name: Channel 5
    show_state: true
    icon_height: 40px
  - show_name: true
    show_icon: true
    type: button
    tap_action:
      action: toggle
    entity: switch.waveshare_relay_channel_6
    name: Channel 6
    show_state: true
    icon_height: 40px
  - show_name: true
    show_icon: true
    type: button
    tap_action:
      action: toggle
    entity: switch.waveshare_relay_channel_7
    name: Channel 7
    show_state: true
    icon_height: 40px
  - show_name: true
    show_icon: true
    type: button
    tap_action:
      action: toggle
    entity: switch.waveshare_relay_channel_8
    name: Channel 8
    show_state: true
    icon_height: 40px
title: MyTest
columns: 8
1 Like