Best way to sync frontend values with modbus sensors

My ventilation unit has its own control panel and is also controlled over modbus:

  • Register 40003 contains integer 0-1 for off/on mode.
  • Register 40000 contains integer 1-7 for current fan speed.

My goal is:

  • a switch (for off/on) and a slider (for fan speed) on dashboard;
  • when values change on dashboard, send them to ventilation unit;
  • when values change on ventilation unit, update them on dashboard.

The code below seems to work. Simple for switch and complicated for number.
Can this be done in a more simple way?

configuration.yaml:

modbus:
  name: Z031
  type: tcp
  host: 192.168.1.200
  port: 502
  switches:
  - unique_id: minibox_on_off
    name: Minibox on/off
    address: 40003
    verify:
  sensors:
  - unique_id: minibox_speed
    name: Minibox speed
    address: 40000

input_number:
  minibox_speed:
    name: Minibox speed
    icon: mdi:fan
    min: 1
    max: 7
    step: 1

automation:

- triggers:
  - trigger: homeassistant
    event: start
    variables: { state: "{{ states.sensor.minibox_speed.state | int }}" }
  - trigger: state
    entity_id: sensor.minibox_speed
    variables: { state: "{{ trigger.to_state.state | int }}" }
  conditions: "{{ (states.input_number.minibox_speed.state | int) != state }}"
  actions:
  - action: input_number.set_value
    target: { entity_id: input_number.minibox_speed }
    data: { value: "{{ state }}" }

- triggers:
  - trigger: state
    entity_id: input_number.minibox_speed
    variables: { state: "{{ trigger.to_state.state | int }}" }
  conditions: "{{ (states.sensor.minibox_speed.state | int) != state }}"
  actions:
  - action: modbus.write_register
    data: { hub: Z031, address: 40000, value: "{{ state }}" }
  - action: homeassistant.update_entity
    target: { entity_id: sensor.minibox_speed }

Dashboard:

type: entities
entities:
  - entity: switch.minibox_on_off
  - entity: input_number.minibox_speed