Electric heater with % ON cycle to modulate electricity consumption

Hi all

I have an electric heater controlled by a Sonoff Mini R2 (not yet flashed). I am planning to flash it with Esphome, but I want 2 things:

  1. I have a solar system, and I want to use excess solar power to use the heater. But using it normally (just turning ON the relay) will drain my batteries. So I was hoping that it could expose an “dimmer” or similar interface that would internally cycle it ON/OFF, e.g. in 10% steps (0% is OFF, 10% is 30s on and 4min30s off, etc, with 100% being always on). This is a bit like these old heaters with knobs that allow for a granular control. The reason for the long cycles is that if I understand correctly, cycling a relay at short intervals will burn it. Then, HA will determine (using some temperature sensors) if the heater should be on, and modulate the % based on available solar electricity.
  2. A button is connected between S1 and S2. Turning it to ON shouldn’t do anything if HA has already turned the heater on, but turning it OFF should always switch it off (for emergencies).

So far, I can figure out the HA side easily enough, but how can configure my Sonoff Mini R2 to expose this kind of interface using Esphome? Thanks

I don’t see any obstacles for what you are asking.
This component might be easiest way for switching:

Short is relative. Relays can switch at quite high frequency but it would eventually burn. Once every few seconds should be safe.
Obviously, relays also wear, so longer periods make them last longer.

Slow PWM is exactly what I was looking for, thank you!
How would I expose this to HA as a heater though? Would you mind creating some dummy code that I can play with? Thanks

I think you could use any component that has some “slider”.
Try this:

number:
  - platform: template
    name: "Heater"
    id: heater
    min_value: 0
    max_value: 100
    step: 1
    optimistic: true
    set_action:
      then:
        - output.set_level:
            id: slowpwm1
            level: !lambda "return x/100;"
       
output:
  - platform: slow_pwm
    pin: GPIOXX
    id: slowpwm
    period: 5min

put your relay gpio there…