On_message action mqtt esphome

I want to check relay2 before I turn on the light with a on_message action in esphome but I can’t figure out how to do that.
I tried several possibilities
Is this possible?
Or is this not possible?

on_message :
    - topic: oled5
      qos: 0
      then:
        if:
          relay2 == 1:
        then:
          - switch.turn_on: relay

Help please