Setting and reading relay state over mqtt with input select

I want to control 4 relays with input select options; off,1,2,3,4 and only 1 relay can be active at once.
I can do this with mqtt and esp8266, but cant figure out how to read the actual state over mqtt and set the input select option accordingly

configurator
  • platform: mqtt
    name: Huippuimuri1
    state_topic: “/ESP/gpio/12”
    command_topic: “/ESP/gpio/12”
    payload_on: “0”
    payload_off: “1”
  • platform: mqtt
    name: Huippuimuri2
    state_topic: “/ESP/gpio/13”
    command_topic: “/ESP/gpio/13”
    payload_on: “0”
    payload_off: “1”
  • platform: mqtt
    name: Huippuimuri3
    state_topic: “/ESP/gpio/14”
    command_topic: “/ESP/gpio/14”
    payload_on: “0”
    payload_off: “1”
  • platform: mqtt
    name: Huippuimuri4
    state_topic: “/ESP/gpio/16”
    command_topic: “/ESP/gpio/16”
    payload_on: “0”
    payload_off: “1”
automation
  • alias: Huippuimuri pois päältä
    trigger:
    platform: state
    entity_id: input_select.huippuimuri
    to: “OFF”
    action:

    • service: switch.turn_off
      entity_id:
      • group.huippuimurin_nopeus
  • alias: Huippuimuri nopeus 1
    trigger:

    • platform: state
      entity_id: input_select.huippuimuri
      to: “1”
      action:
    • service: switch.turn_off
      entity_id:
      • group.huippuimurin_nopeus
    • service: switch.turn_on
      entity_id:
      • switch.huippuimuri1
  • alias: Huippuimuri nopeus 2
    trigger:
    platform: state
    entity_id: input_select.huippuimuri
    to: “2”
    action:

    • service: switch.turn_off
      entity_id:
      • group.huippuimurin_nopeus
    • service: switch.turn_on
      entity_id:
      • switch.huippuimuri2
  • alias: Huippuimuri nopeus 3
    trigger:
    platform: state
    entity_id: input_select.huippuimuri
    to: “3”
    action:

    • service: switch.turn_off
      entity_id:
      • group.huippuimurin_nopeus
    • service: switch.turn_on
      entity_id:
      • switch.huippuimuri3
  • alias: Huippuimuri nopeus 4
    trigger:
    platform: state
    entity_id: input_select.huippuimuri
    to: “4”
    action:

    • service: switch.turn_off
      entity_id:
      • group.huippuimurin_nopeus
    • service: switch.turn_on
      entity_id:
      • switch.huippuimuri4

They are made to group and set all relays off before setting only one on. This works fine, but with more automation the input select will eventually lose track of what switch is actually active…