esphome:
  name: "SonOff POW r1"
esp8266:
  board: esp01_1m
  board_flash_mode: dout
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: SonOff POW Button
    on_press:
      - switch.toggle: fakebutton
  - platform: template
    name: SonOff POW Running
    filters:
      - delayed_off: 15s
    lambda: |-
      if (isnan(id(power).state)) {
        return {};
      } else if (id(power).state > 4) {
        // Running
        return true;
      } else {
        // Not running
        return false;
      }
sensor:
  - platform: hlw8012
    sel_pin: 5
    cf_pin: 14
    cf1_pin: 13
    update_interval: 2s
    current:
      name: SonOff POW Current
    voltage:
      name: SonOff POW Voltage
    power:
      name: SonOff POW Power
      id: power
      on_value_range:
        - above: 4.0
          then:
            - light.turn_on: led
        - below: 3.0
          then:
            - light.turn_off: led
switch:
  - platform: template
    name: SonOff POW Relay
    optimistic: true
    id: fakebutton
    turn_on_action:
      - switch.turn_on: relay
      - light.turn_on: led
    turn_off_action:
      - switch.turn_off: relay
      - light.turn_off: led
  - platform: gpio
    id: relay
    pin: GPIO12
output:
  - platform: esp8266_pwm
    id: pow_blue_led
    pin:
      number: GPIO15
      inverted: True
light:
  - platform: monochromatic
    name: SonOff POW Blue LED
    output: pow_blue_led
    id: led