Trying to have relay on after a boot?

I’ve used @frenck’s very advanced esp config to get my Sonoff Pow 2.
I have it mostly where I want, there are two minor issues left.
I would like it be turned on when it boots.
I have the blue led to turn on when it uses power, I would rather have it reflect the ‘running’ sensor.

But I can’t get it to be on when it boots, and so far the blue LED indicates if it uses more than 4w.

esphome:
  name: dryer
  platform: ESP8266
  board: esp01_1m
  board_flash_mode: dout
  on_boot:
    then:
      - switch.turn_on: relay

wifi:
  ssid: "xx"
  password: "xxx"

uart:
  rx_pin: RX
  baud_rate: 4800

logger:
  baud_rate: 0
  
api:

ota:
  password: "veryverysecret"

time:
  - platform: homeassistant
    id: homeassistant_time

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "Dryer Button"
    on_press:
      - switch.toggle: fakebutton
  - platform: template
    name: "Dryer Running"
    filters:
      - delayed_off: 120s
    lambda: |-
      if (isnan(id(power).state)) {
        return {};
      } else if (id(power).state > 4) {
        // Running
        return true;
      } else {
        // Not running
        return false;
      }

switch:
  - platform: template
    name: "Dryer Relay"
    optimistic: true
    id: fakebutton
    turn_on_action:
    - switch.turn_on: relay
    turn_off_action:
    - switch.turn_off: relay
  - platform: gpio
    id: relay
    pin: GPIO12

output:
  - platform: esp8266_pwm
    id: pow_blue_led
    pin:
      number: GPIO13
      inverted: True

light:
  - platform: monochromatic
    name: "Dryer Blue LED"
    output: pow_blue_led
    id: led

sensor:
  - platform: wifi_signal
    name: "Dryer WiFi Signal"
    update_interval: 60s
  - platform: uptime
    name: "Dryer Uptime"
  - platform: cse7766
    update_interval: 20s
    current:
      name: "Dryer Current"
      accuracy_decimals: 2
    voltage:
      name: "Dryer Voltage"
      accuracy_decimals: 0
    power:
      name: "Dryer Power"
      accuracy_decimals: 0
      id: power
      on_value_range:
        - above: 4.0
          then:
            - light.turn_on: led
        - below: 3.0
          then:
            - light.turn_off: led
  - platform: total_daily_energy
    name: "Dryer daily energy"
    power_id: power
    filters:
      - multiply: 0.001
    unit_of_measurement: kWh

text_sensor:
  - platform: version
    name: "Dryer ESPHome Version"
  - platform: wifi_info
    ip_address:
      name: "Bad Multisensor ip"
    ssid:
      name: "Bad Multisensor ssid"

Try

 - platform: gpio
   id: relay
   pin: GPIO12
   restore_mode: ALWAYS_ON
2 Likes

Thankyou, that is much neater than what I did.
I actually just found out that my method works as well, the problem is that it isn’t shown correctly in HA after the boot??? It is shown as being off, where it actually is on.
If I manually turn it on in HA, I can turn it off after that.
Ok, so the problem is not that it isn’t on, the problem is that it doesn’t report the correct state after the boot.

I see

problem is ESPHome is reporting your switch state which is optimistic template switch, not the gpio which seems to be what you want.

Unfortunately switch does not have restore_mode
but since you want it to boot powered “on” you can try this:


  on_boot:
    then:
      - switch.turn_on: relay
      - switch.template.publish:
          id: fakebutton
          state: ON
1 Like

That worked perfectly, thankyou!
I tried publishing to the switch, but that didn’t work :smile: