I can't figure out "if"

Still a novice user…

Sonoff Basic:
I have an LED on GPIO 13 that in Tasmota indicated my “WiFi connected” status.
I would like to replicate this in ESPHome, but I don’t understand YAML well enough to fix the code error.

I would appreciate any assistance. Can someone point me to where in the documentation I might find my error?

The error:
esphome-error

Here is my YAML file:

#OfficeLight.yaml

esphome:
  name: office_light
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: "Kaywinnet"
  password: !secret wifi_password
  fast_connect: true

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Office Ligt Fallback Hotspot"
    password: "HKDgVm8kBd5h"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

###############################
# Virtual switch GPI12  
switch:
  - platform: gpio
    name: "OfficeLight"
    pin:
      number: 12
      mode: output
    id: OfficeLight_relay
    
  - platform: gpio
    name: "OfficeLight_led"
    pin:
      number: 13
      mode: output
    id: OfficeLight_led
    
    
# Physical Button on GPIO0
binary_sensor:
  - platform: gpio
    pin: 
      number: 0
      inverted: False
      mode: INPUT_PULLUP
    name: OfficeLight_Button
    internal: true
    on_press:
      - switch.toggle: OfficeLight_relay    
      
  # Physical Button on GPIO14  
  - platform: gpio
    pin: 
      number: 14
      inverted: False
      mode: INPUT_PULLUP
    name: "OfficeLight Remote Button"
    on_press:
      then:
      - switch.toggle: OfficeLight_relay
      - switch.toggle: OfficeLight_led
    on_release:
      then:
      - switch.toggle: OfficeLight_relay
      - switch.toggle: OfficeLight_led
      
      
# Get the WiFi details
text_sensor:
  - platform: wifi_info
    ip_address:
      name: Office Light IP Address
    ssid:
      name: Office Light SSID
    bssid:
      name: Office Light BSSID
    mac_address:
      name: Office Light Mac Address
      
sensor:
  - platform: wifi_signal
    name: "OfficeLight WiFi Signal Sensor"
    id: OfficeLight_WiFi_level
    update_interval: 10s
    
    - if:
      condition:
        wifi.connected:
      then:
        switch.turn_on: OfficeLight_led
      else:
        switch.turn_off: OfficeLight_led

The error points to:

    - if:
      condition:
        wifi.connected:
      then:
        switch.turn_on: OfficeLight_led
      else:
        switch.turn_off: OfficeLight_led
1 Like

Hi,

YAML is a very fussy beast and spaces are critical. To be honest, i don’t really know much either but I use
http://yaml-online-parser.appspot.com/

Paste your yaml in there (and hit enter) and then you can move lines in real time and try get rid of your errors.

If I paste your code in then it looks like you need to line up the “- if” with the “-platform”.

Like this

sensor:
  - platform: wifi_signal
    name: "OfficeLight WiFi Signal Sensor"
    id: OfficeLight_WiFi_level
    update_interval: 10s
    
  - if:
      condition:
        wifi.connected:
      then:
        switch.turn_on: OfficeLight_led
      else:
        switch.turn_off: OfficeLight_led

Simon

1 Like

Thanks, but now I have a new error:
“No platform specified. See ‘platform’ key”.

That’s a neat tool- thanks for the tip.

It is much simpler to use the status_led component - https://esphome.io/components/status_led.html

Your if block is misplaced. It needs to have a trigger of some sort. Check the documentation for this condition:

It is inside an on_....: block. Are you trying to check everytime the wifi signal changes whether it is connected? If so you could use on_value: from this sensor. See:

Edit: used better link.

Many thanks. When I first read up on the status_led component, I was thinking that I want the LED on if everything is ok. Then, a duh! moment, inverted: True. Blinking doesn’t care if the LED logic is inverted or normal. It still blinks.

While this solves my goal of an LED on if all is good…

How do do a simple if ? (For future reference).

Many thanks for the reply.
While Nick helped with my goal, I am still flummoxed by how do I do a simple if?

Code snippets in the docs are a great reference; If you know YAML and where to put the snippets. I spent hours with the snippets in the docs and could not figure out where to put this in my code. What does on_…: refer to?

on_...:
  if:
    condition:
      wifi.connected:
    then:
      - logger.log: WiFi is connected!

Yeah I am often puzzled by the docs. on... always get me wondering.

The example code in the docs is pretty good for that.

One of the triggers. See here:

I already have the status binary sensor for all my esphome nodes (so I can see if any nodes are offline) so I add this to it for a status LED:

binary_sensor:
  - platform: status
    name: "OfficeLight WiFi Status"
    on_press:
      - switch.turn_on: OfficeLight_led
    on_release:
      - switch.turn_off: OfficeLight_led

Also, if you use this instead of switch: you’ll get a light in HA instead of a switch:

output:
  - platform: gpio
    id: OfficeLight_relay
    pin:
      number: 12

light:
  - platform: binary
    name: "OfficeLight"
    id: OfficeLight
    output: OfficeLight_relay

Thanks for the tips. I am still learning…
I’ve often wondered, if the light is only on or off, what difference does it make whether I use the light or switch component?

Nothing really, just looks better in the UI and easier to find