PWM transition for a switch?

I’ve got a solenoid that I’m trying to drive with a MOSFET. I want to avoid the ‘slam’ of the initial inrush so I’d like the PWM driving the MOSFET to use the 1s transition speed. I’ve made the below config and want it to be operated as a template switch to transition on, delay, then transition off, but I’m getting the error below.

Any advice?

output:
- platform: esp8266_pwm
  id: pwm_output
  frequency: 10000Hz
  min_power: 0.5
  max_power: 1.0
  pin: D6

light:
- platform: monochromatic
  name: "MOSFET Light Component"
  id: mosfet_light
  output: pwm_output
  default_transition_length: 1s
  restore_mode: ALWAYS_OFF




switch:
- platform: template
  name: "ZD01 MOSFET Timed"
  lambda: |-
    if (id(pwm_output).state) {
      return true;
    } else {
      return false;
    }
  turn_on_action:
    - light.turn_on: mosfet_light
    - delay: 3s
    - light.turn_off: mosfet_light
Compiling .pioenvs/esphome_zdev_01/src/main.cpp.o
src/main.cpp: In lambda function:
src/main.cpp:178:23: error: 'class esphome::esp8266_pwm::ESP8266PWM' has no member named 'state'
if (pwm_output->state) {
^
src/main.cpp:183:3: warning: control reaches end of non-void function [-Wreturn-type]
});
^
*** [.pioenvs/esphome_zdev_01/src/main.cpp.o] Error 1

Is this your idea or is it a common practice to drive solenoids this way?

Unfortunately you cannot get the state of an output that way.

to do it yaml style:
(untested code)

esphome:
  ...
  on_loop:
    - if:
        condition:
          light.is_on: mosfet_light
        then:
          - switch.template.publish:
              id: template_switch
              state: ON
        else:
          - switch.template.publish:
              id: template_switch
              state: OFF

switch:
- platform: template
  name: "ZD01 MOSFET Timed"
  id: template_switch
  turn_on_action:
    - light.turn_on: mosfet_light
    - delay: 3s
    - light.turn_off: mosfet_light

PS: don’t be afraid of on_loop, microcontrollers are always in a loop doing something.

Awesome!! Thank you, this worked exactly as expected!

Driving a solenoid through a MOSFET is rather normal. It’s essentially a solid state way of doing it over using a relay. The unique thing is that I’m trying to “ramp up” the MOSFET rather treat it as a on/off switch. Maybe there is a better way to handle this while still having a switch front end.

So I tried looking for more info on the site and only saw a bit about loops in custom components section. If I’m understanding this correctly, on each loop of the mcu it will check to see if the light is on, and update the value of the switch (on/off) based on the actual status of the light (being on/off)?

Exactly the switch will follow the state of the light all times.

Is the solenoid working “smoothly”?

It’s not as quiet/smooth as I’d like it to be, but it’s to be expected based on how a solenoid’s magnetic field works. It is probably about half as loud as was when it was just acting like a switch and sending the full 12v power to the solenoid. So in that regard, it is a great improvement.

1 Like