ESPHome Sonoff handling via mqtt

I have used yaml code as stated below, but notices that sending message ‘on’ repeatedly, I actually toggle between ‘ON’ and ‘OFF’ status. Though sending that message would put sonoff device on ‘ON’ status only?

on_message:
    - topic: esp_pow1_To
      payload: 'on'
      then:
      - switch.toggle: relayandled
      - mqtt.publish:
          topic: esp_pow1_From
          payload: "On"
    - topic: esp_pow1_To
      payload: 'off'
      then:
      - switch.toggle: relayandled
      - mqtt.publish:
          topic: esp_pow1_From
          payload: "Off"

Possibly this part of yaml is relevant too:

switch:

  • platform: gpio
    pin: GPIO12
    id: relay
  • platform: template
    name: “Main desk light relay”
    optimistic: true
    id: relayandled
    turn_on_action:
    • switch.turn_on: relay
    • light.turn_on: led
      turn_off_action:
    • switch.turn_off: relay
    • light.turn_off: led