# Please explain this code line by line

**URL:** <https://forum.freecodecamp.org/t/please-explain-this-code-line-by-line/487843>\
**Category:** General\
**Created:** [December 7, 2021, 1:32am UTC](https://forum.freecodecamp.org/t/please-explain-this-code-line-by-line/487843 "2021-12-07T01:32:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hotsummer](https://avatars.discourse-cdn.com/v4/letter/h/258eb7/32.png) [@hotsummer](https://forum.freecodecamp.org/u/hotsummer)\
**Post date:** [December 7, 2021, 1:32am UTC](https://forum.freecodecamp.org/t/please-explain-this-code-line-by-line/487843/1 "2021-12-07T01:32:29Z")

</div>

```auto
## this part is the ESPhome Yaml file ##
esphome:
  name: window_control_linear
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: !secret wifi
  password: !secret wifi_pw

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

output:
  - platform: gpio
    id: 'win_1'
    pin: D1
  - platform: gpio
    id: 'win_2'
    pin: D2

switch:
  - platform: output
    name: "win1"
    output: 'win_1'
    id: win1
  - platform: output
    name: "win2"
    output: 'win_2'
    id: win2

cover:
  - platform: template
    name: "Window Control"
    id: win_open
    optimistic: true
    open_action:
      - switch.turn_off: win2
      - switch.turn_on: win1
      - delay: 360s
      - switch.turn_off: win1
    close_action:
      - switch.turn_off: win1
      - switch.turn_on: win2
      - delay: 360s
      - switch.turn_off: win2
    stop_action:
      - switch.turn_off: win1
      - switch.turn_off: win2
     
binary_sensor:
  - platform: gpio
    pin: D3
    name: "Window Control Button"
    filters:
      invert:
    on_press: 
      then:
        - lambda: |
            if (id(win_open).state == cover::COVER_OPEN) {
              if (id(win1).state){
                // window is opening
                id(win_open).stop();
              } else {
                // window is open and not moving
                id(win_open).close();
              }
            } else {
              if (id(win2).state){
                // window is closing
                id(win_open).stop();
              } else {
                // win is closed and not moving
                id(win_open).open();
              }
            }
## this part goes in your Configuration.yaml ##

switch:
  - platform: mqtt
    name: "QuietCool Low"
    command_topic: "cmnd/QuietCool/power1"
    state_topic: "stat/QuietCool/POWER1"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: false
  - platform: mqtt
    name: "QuietCool Med"
    command_topic: "cmnd/QuietCool/power2"
    state_topic: "stat/QuietCool/POWER2"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: false
  - platform: mqtt
    name: "QuietCool High"
    command_topic: "cmnd/QuietCool/power3"
    state_topic: "stat/QuietCool/POWER3"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: false

```

Link for code [sliding\_window\_opener.yaml · GitHub](https://gist.github.com/Snipercaine/75fcfbb14f8dabc8918d7d6723501821)

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [December 7, 2021, 2:19am UTC](https://forum.freecodecamp.org/t/please-explain-this-code-line-by-line/487843/2 "2021-12-07T02:19:22Z")

</div>

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [December 7, 2021, 2:26am UTC](https://forum.freecodecamp.org/t/please-explain-this-code-line-by-line/487843/3 "2021-12-07T02:26:21Z")

</div>

Do you want a foot rub, while we’re at it? 😉

That’s nearly 100 lines. You want someone to take the time to explain every line?

I think better would be if you ask a specific question. What don’t you understand?

I see some yaml code with a lambda function in what I assume is C++.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [June 7, 2022, 2:26pm UTC](https://forum.freecodecamp.org/t/please-explain-this-code-line-by-line/487843/4 "2022-06-07T14:26:45Z")

</div>

Closed on June 7, 2022, 2:26pm UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
