Help with this Lambda

This is gettig the better of me, I want it to be fast, like microcontroler fast. I had it working but still had issues with is only running when the sensor updated. I’m trying to implement a crude solar constant voltage input control as an approx to MPP. it doesn’t matter if I get voltage overshoot or undershoot but id like it to be faster than the sensor update.
I think i need the an analog read in a lamda that runs all the time?
logic is simple, if voltage is above 100v increase pwm duty, if it’s below 80v reduce it.

This is where im at with it atm it’s getting messy as im fiddling with it and getting tired.

globals:
   - id: pwm_int
     type: float
     restore_value: no
     initial_value: '0'
   - id: pwm_test
     type: int
     restore_value: no
     initial_value: '0'
esphome:
  name: hotwater
  platform: ESP32
  board: esp-wrover-kit

wifi:
  ssid: "ssid"
  password: "pswd"

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

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

output:
  - platform: ledc
    pin: GPIO19
    id: gpio_19
    frequency: 19500


    
sensor:
  - platform: adc
    pin: GPIO34
    filters:
       - multiply: 100
    name: "Hotwater Voltage"
    id: voltage
    update_interval: 3s
  
lambda: !lambda |-
  if (id(voltage)>100) {
       id(pwm_int) += 0.1;
       -output.set_level:
  id: gpio_19

  level: !lambda 'return id(pwm_int);'
      }
 if (id(voltage)<80) {
      id(pwm_int) -= 0.1;
   -output.set_level:
    id: gpio_19
    level: 50% #!lambda 'return id(pwm_int);'
   }
       
      
#    on_value_range:
#      - above: 100.0
#        then:
#           - lambda: 'id(pwm_int) += 0.1;'
#           - output.set_level:
#               id: gpio_19
#               level: !lambda 'return id(pwm_int);'
#      - below: 80.0
#        then:
#          - lambda: 'id(pwm_int) -= 0.1;'
#          - output.set_level:
#              id: gpio_19
#              level: !lambda 'return id(pwm_int);'
#    update_interval: 2s
    
    
#light:
 # - platform: monochromatic
  #  output: gpio_19
   # name: "hotwater pwm"

Ministry for Pacific Peoples is returned in google.

How about you tell us what is going wrong - we can’t guess. What result are you seeing/not seeing?

I ran out of talent thats whats wrong.
MPP stands for max power point, doesn’t matter. above 100 duty up, below 80 duty down, as fast as it can.

Like I said I want it to run fast like arduino code, not only in the update interval like the comented out version did.

I’ve tidied it up, now all it does on the first time it goes over the 100 it sets it to 10% duty but even if the voltage remains above 100 it doesn’t do it again, and even if it did I think it’d only do it at the update interval, what happens if i set my update interval too short ? flood wifi/ database?? seems the wront way to do it.

globals:
   - id: pwm_int
     type: float
     restore_value: no
     initial_value: '0'
   - id: pwm_test
     type: int
     restore_value: no
     initial_value: '0'
esphome:
  name: hotwater
  platform: ESP32
  board: esp-wrover-kit
wifi:
  ssid: "
  password: ""
  ap:
    ssid: "Hotwater Fallback Hotspot"
    password: "ybeV8oeqSVkM"
captive_portal:
logger:
api:
ota:
output:
  - platform: ledc
    pin: GPIO19
    id: gpio_19
    frequency: 19500
sensor:
  - platform: adc
    pin: GPIO34
    filters:
       - multiply: 100
    name: "Hotwater Voltage"
    id: voltage
    update_interval: 3s
    on_value_range:
      - above: 100.0
        then:
           - lambda: 'id(pwm_int) += 0.1;'
           - output.set_level:
               id: gpio_19
               level: !lambda 'return id(pwm_int);'
      - below: 80.0
        then:
          - lambda: 'id(pwm_int) -= 0.1;'
          - output.set_level:
              id: gpio_19
              level: !lambda 'return id(pwm_int);'

more i think about it the code needs to do it all in a lambda, I know the adc counts won’t be in the same units but I can work that out.

if (analogRead(Pin34)>100)) 
  {
    pwm +=0.1;
  }
if (analogRead(Pin34)<80))
  {
    pwm -=0.1;
  }
ledcAttachPin(GPIO 19, 1);
ledcWrite(1,pwm)
1 Like

This gives an error “component not found: lambda”
I’m guessing I can’t use a lambda that just runs all the time it has to be inside a sensor or some othe block of yaml??
Maybe i need to read the custom sensor component?

   - id: pwm_float
     type: float
     restore_value: no
     initial_value: '0'
   - id: pwm_test
     type: int
     restore_value: no
     initial_value: '0'
esphome:
  name: hotwater
  platform: ESP32
  board: esp-wrover-kit
wifi:
  ssid: ""
  password: 
  ap:
    ssid: "Hotwater Fallback Hotspot"
    password: "ybeV8oeqSVkM"
captive_portal:
logger:
api:
ota:
output:
  - platform: ledc
    pin: GPIO19
    id: gpio_19
    frequency: 19500
    channel: 0
sensor:
  - platform: adc
    pin: GPIO34
    filters:
       - multiply: 100
    name: "Hotwater Voltage"
    id: voltage
    update_interval: 3s

 
#!lambda |-

lambda: !lambda |-
  if (analogRead(Pin34)>100)) {
  pwm_float +=0.1;
  }
  if (analogRead(Pin34)<80))  {
  pwm_float -=0.1;
  }
  ledcWrite(0,pwm_float);