Automated valvecontrol with PWM

Hi folks,

I’m currently trying to use a PWM-driven valve to control water flow from our distillery.
On the cooling tower are 5 Dalls-temperature-sensors.
4 From the top to the bottom and one to check the temperature of the alcohol.

Now the problem:
I created a ledc-Output on the ESP32 and used the light-tool to create a “valve”.

 output:
   - platform: ledc
     pin: 13
     frequency: 3000Hz
     id: ventcontrol
 light:
   - platform: monochromatic
     output: ventcontrol
     name: "Control"
     default_transition_length: 0s

I can use this control to change the valve from homeassistant but not within a temperature-driven automation.

Here my attempt:

 - if:
   condition: #cooling not active
     and:
       - sensor.in_range:
         id: temp_02
         above: 40
       - lambda: return id(varcooling) <= 0;
     then:
       - logger.log: "cooling off"
       - lambda: id(ventcontrol).set_level(0.3);
       - globals.set:
           id: varcooling
           value: '30'

Unfortunately the lambda won’t set the PWM-Output to 30%.

Is there a solution to my problem or does anybody knew a better solution?

greeting Stephan

Does this work?

 - if:
   condition: #cooling not active
     and:
       - sensor.in_range:
         id: temp_02
         above: 40
       - lambda: return id(varcooling) <= 0;
     then:
       - logger.log: "cooling off"
       - output.set_level:
           id: ventcontrol
           level: "30%"
       - globals.set:
           id: varcooling
           value: '30'

Hi,

sorry, I din’t post the complete condition-set.

This was the first one,
Her my complete yaml.

esphome:
  name: brennerei
  platform: ESP32
  board: nodemcu-32s

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

# Enable logging
logger:
  #level: NONE

web_server:
  port: 80

# Enable Home Assistant API
api:
  password: "password"

ota:
  password: "password"

globals:
  - id: varkuehlung
    type: int
    restore_value: no
    initial_value: '0'
  - id: varwait
    type: int
    restore_value: no
    initial_value: '0'
  - id: varumgebung
    type: float
    restore_value: no
    initial_value: '0' 

captive_portal:

dallas:
  - pin: 26
    update_interval: 2s

sensor:
  - platform: dht
    pin: 14
    temperature:
      id: "brennerei_temp"
      name: "Brennerei Temperatur" 
      on_value:
        then:
          - globals.set:
              id: varumgebung
              value: !lambda |-
                return id(brennerei_temp).state += 10;
    humidity:
      id: "brennerei_luftfeuchtigkeit"
      name: "Brennerei Luftfeuchtigkeit"
    update_interval: 2s
    model: 'DHT22'

  - platform: dallas
    address: GUID1
    name: "Kühlturm01"
    id: temp_01
    accuracy_decimals: 1

  - platform: dallas
    address: GUID2
    name: "Kühlturm02"
    id: temp_02
    accuracy_decimals: 1
    on_value:
      then:
        - if:
            condition:
              switch.is_off: override
            then:
              - logger.log: "override aus"
              - logger.log:
                  format: "%i"
                  args: [ 'id(ventilsteuerung)' ]
              - if:
                  condition:
                    - lambda: return id(varwait) > 0;
                  then:
                    - logger.log:
                        format: "wait %i"
                        args: [ 'id(varwait)' ]
                    - lambda: id(varwait) -= 1;
                  else:
                    - logger.log: "nowait"
                    - if:
                        condition: #Kuehlung noch nicht aktiv
                          and:
                            - sensor.in_range:
                                id: temp_02
                                above: 40
                            - lambda: return id(varkuehlung) <= 0;
                        then:
                          - logger.log: "Kühlung aus"
                          - output.set_level:
                              id: ventilsteuerung
                              level: 30%
                          #- lambda: id(ventilsteuerung).set_level(0.3);
                          - globals.set:
                              id: varkuehlung
                              value: '30'
                          - globals.set:
                              id: varwait
                              value: '3'
                          - logger.log:
                              format: "wait %i"
                              args: [ 'id(varwait)' ]
                        else:
                          - logger.log: "Temp unter 40°C oder Kühlung an"
                          - if:
                              condition:
                                and:
                                  - sensor.in_range:
                                      id: temp_01
                                      above: 45
                                  - lambda: return id(varkuehlung) >= 25;
                              then:
                                - logger.log: "Temp über 45°C und Kühlung aktiv"
                                - globals.set:
                                    id: varkuehlung
                                    value: !lambda |-
                                              return id(varkuehlung)+0.05;
                                - lambda: id(ventilsteuerung).set_level(id(varkuehlung));
                                - globals.set:
                                    id: varwait
                                    value: '3'
                                - logger.log:
                                    format: "wait %i"
                                    args: [ 'id(varwait)' ]
                              else:
                                - logger.log: "Temp unter 45°C oder Kühlung aus"
                                - if:
                                    condition:
                                      and:
                                       - sensor.in_range:
                                           id: temp_01
                                           below: 45
                                       - lambda: return id(ventilsteuerung) > 0; 
                                    then:
                                      - globals.set:
                                          id: varkuehlung
                                          value: !lambda |-
                                                    return id(varkuehlung)-0.05;
                                      - lambda: id(ventilsteuerung).set_level(id(varkuehlung));
                                      - globals.set:
                                          id: varwait
                                          value: '2'
                                      - logger.log:
                                          format: "Wait: %i"
                                          args: [ 'id(varwait)' ] 
                                    else: #sensor.in_range: below 40
                                     - logger.log : "Kuehlung aus"
            else: #switch.is_on: override
             - logger.log : "Override an"

output:
  - platform: ledc
    pin: 13
    frequency: 3000Hz
    id: ventilsteuerung
switch:
  - platform: gpio
    pin: 2
    id: buzzer
    name: "Buzzer"
  - platform: gpio
    pin: 4
    id: relais
    name: "Relais"
  - platform: gpio
    pin: 16
    id: override
    name: "Override"
  - platform: gpio
    pin: 12
    id: auslassalarm
    name: "Auslass-Alarm"

light:
  - platform: monochromatic
    output: ventilsteuerung
    name: "Ventilsteuerung"
    default_transition_length: 0s

this is the complete automation.

As you cann see I check the temperature and want to change the valve if it is raising above 45°C.

Temp above 45° -> +5%.

Is the output.set_level usable with lambda-template?

found a solution.

I’m using

- light.dim_relative:
    id: ventcontrol
    relative_brightness: 5%

to change the value of the valve.

Everything is now working as intended.