How can I disable a switch with software?

I’m trying to add a software blockage of a switch by having a boolean I can turn on.

  - platform: homeassistant
    id: sw_block
    entity_id: input_boolean.porttelefon_sw_block

  - platform: gpio
      number: 14
    id: porttelefon
    name: "Porttelefon"
        - if:
                lambda: |-
                        if(id(sw_block).state == "off"){
                        return true;
                          return false;
                - delay: 0.5s
                - switch.turn_off: porttelefon

The switch it self needs to momentary thus the delay and turn off, but how can I make sure it first reads the condition before the turn on?
The way it works now is that if the boolean is off then it works as expected (turn on, delay then off).
When boolean is on, it turns on and stops at that position. I want it to not even turn on.

Is that even possible?

You want to disable it all together butt you’re already defining the button push as on when you use on_turn_on. You’ll need to use on_press then it will evaluate the conditions before taking action.

Ah? So on_press is before.
Sweet, I will try that.

I interpreted momentary as a binary sensor, like physical button, now I’m not sure that is how you have set it up. on_press only works on a binary sensor.

You could make a dummy switch on an unused pin. Then use that to evaluate the conditions and turn on the other pin if true.