How to link binary_sensor to fan/output in ESPHome?

How do I connect my touch sensor to the fan output, so when I press the touch sensor it should get updated in HA.

Right now when I press the touch sensor the state of the fan changes but the state is not displayed on my Lovelace card.

switch:
  - platform: gpio
    pin: D5
    name: "Master Bedroom Fan Speed Relay 1"
    id: mbedspeed1
  - platform: gpio
    pin: D6
    name: "Master Bedroom Fan Speed Relay 2"
    id: mbedspeed2
  - platform: gpio
    pin: D7
    name: "Master Bedroom Fan Speed Relay 3"
    id: mbedspeed4

binary_sensor:
  - platform: gpio
    pin: D0
    name: 'Turn Off Fan'
    on_click:
      then:
        - switch.turn_off: mbedspeed1
        - switch.turn_off: mbedspeed2
        - switch.turn_off: mbedspeed4

  - platform: gpio
    pin: D1
    name: 'Fan Speed 1'
    on_click:
      then:
        - switch.turn_on: mbedspeed1
        - switch.turn_off: mbedspeed2
        - switch.turn_off: mbedspeed4

  - platform: gpio
    pin: D2
    name: 'Fan Speed 2'
    on_click:
      then:
        - switch.turn_off: mbedspeed1
        - switch.turn_on: mbedspeed2
        - switch.turn_off: mbedspeed4

  - platform: gpio
    pin: D3
    name: 'Fan Speed 3'
    on_click:
      then:
        - switch.turn_on: mbedspeed1
        - switch.turn_on: mbedspeed2
        - switch.turn_off: mbedspeed4
        
  - platform: gpio
    pin: D4
    name: 'Fan Speed 4'
    on_click:
      then:
        - switch.turn_off: mbedspeed1
        - switch.turn_off: mbedspeed2
        - switch.turn_on: mbedspeed4
        
output:
  - platform: template
    id: custom_fan
    type: float 
    write_action:
      - if:
          condition:
            lambda: return ((state == 0));
          then:
            # action for off
            - switch.turn_off: mbedspeed1
            - switch.turn_off: mbedspeed2
            - switch.turn_off: mbedspeed4
      - if:
          condition:
            lambda: return ((state > 0) && (state < 0.3));
          then:
            # action for speed 1
            - switch.turn_on: mbedspeed1
            - switch.turn_off: mbedspeed2
            - switch.turn_off: mbedspeed4
            
      - if:
          condition:
            lambda: return ((state > 0.3) && (state < 0.6));
          then:
            # action for speed 2
            - switch.turn_off: mbedspeed1
            - switch.turn_on: mbedspeed2
            - switch.turn_off: mbedspeed4
      - if:
          condition:
            lambda: return ((state > 0.6) && (state < .9));
          then:
            # action for speed 3
            - switch.turn_on: mbedspeed1
            - switch.turn_on: mbedspeed2
            - switch.turn_off: mbedspeed4
      - if:
          condition:
            lambda: return ((state == 1));
          then:
            # action for speed 4
            - switch.turn_off: mbedspeed1
            - switch.turn_off: mbedspeed2
            - switch.turn_on: mbedspeed4
            
fan:
  - platform: speed
    id: mbedfan
    output: custom_fan
    name: "Master Bedroom Fan"
    speed_count: 4

That looks like it should work. Can you see the entities under Developer Tools / States? Could you post a screenshot like this?

and the code for the Lovelace card?

Here we go, they’re unavailable for now as I have not connected them for now…

type: entities
entities:
  - entity: fan.master_bedroom_fan

Did you get a chance to check out the images?

Yes. Is the fan.master_bedroom_fan also visible in the States screen? Ideally post an image when the fan is running…

Here it is… the fan is connected now…

Can we make a condition inside the output, that when a binary_sensor is pressed then do the following action with or condition…

Such as

output:
  - platform: template
    id: custom_fan
    type: float 
    write_action:
      - if:
          condition:
            or:
              - binary_sensor.turn_off_fan
              - lambda: return ((state == 0));
          then:
            # action for off
            - switch.turn_off: mbedspeed1
            - switch.turn_off: mbedspeed2
            - switch.turn_off: mbedspeed4

Something like this, I don’t know the correct syntax for this…

I got the problem to the solution, the answer was provided by @ssieb on the Discord server…

Here is the link to the solution: