ESPHome configuration for PWM to 0..10V output, sensor and control

You can already do that. You can define a service in your ESPHome yaml that you can that call as service in HA automations. See: Native API Component — ESPHome

2 Likes

@danielw :star_struck: I’ll have a look later this week.

I have to be honest, I totally underestimated how tightly integrated ESPHome is with Home Assistant. I didn’t even think about the possibility that you might be able to access HA entities, let alone, expose a service to it.

Thanks both, I now have exactly the solution I wanted

  • A sensor reporting the current setting
  • A HA service to change the setting
  • Memory restore across reboots of my ESP

For others following along, the service definition:

api:
  services:
    - service: set_heating_pwm_level
      variables:
        percentage: int
      then:
        - lambda: |-
            id(heat_set_percent) = percentage;
            id(heat_set_value_sensor).publish_state(percentage);
2 Likes

Great :slight_smile:

How did you do that?

No big tricks :slight_smile:

esphome:
  name: test
  platform: ESP8266
  board: d1_mini
  esp8266_restore_from_flash: yes  # <--

globals:
  - id: heat_set_percent
    type: int
    restore_value: yes  # <--

OK keep in mind if the value is changing a lot it will kill your flash sooner or later.

1 Like

Hi, I’m also busy with a valve from Honeywell to control 0-10v For underfloor heating.

First 0-10v, then in HA PID regulator programming…

Can you please show us you complete ESP yaml…
Because I saw a difference in globals… from float to int…

Thanks allot

Good point and I would have accepted that. However thinking about it: The application will be static (no regular reboots) and when it does I’d rather have a safe state to recover via HA from. I’ve removed the option.

@mupsje you need to find your own configuration that serves your needs. Mine includes other sensors and settings. Hence the following is not the complete yaml and beware that I have changed a few things.

globals:
  - id: heiz_sollwert_percent_pwm
    type: int
    #restore_value: yes
    initial_value: '30'  # 0..100

api:
  services:
    - service: set_heiz_sollwert_percent_pwm
      variables:
        percentage: int
      then:
        - lambda: |-
            id(heiz_sollwert_percent_pwm) = percentage;
            id(heiz_sollwert_sensor).publish_state(percentage);

output:
  - platform: esp8266_pwm
    pin: D1
    frequency: 1000 Hz
    id: heiz_sollwert_pwm_output
    inverted: False
    min_power: 0.00
    max_power: 1.00

# output:
#   - platform: ledc
#     pin: GPIO16
#     frequency: 1220 Hz
#     id: heiz_sollwert_pwm_output
#     inverted: False
#     min_power: 0.00
#     max_power: 1.00

sensor:
  - platform: template
    name: "Heizung Sollwert Sensor (PWM level)"
    id: heiz_sollwert_sensor
    lambda: 'return id(heiz_sollwert_percent_pwm);'
    update_interval: 60s
    unit_of_measurement: "%"
    accuracy_decimals: 0
    icon: "mdi:pulse"
    on_value:
      then:
        - lambda: |-
            id(heiz_sollwert_pwm_output).set_level(x/100);

Additionally, In Home Assistant I’ve defined a couple of input_number entities to get from my wanted set temperature to voltage to PWM level. In testing that looks like this:

grafik

6 Likes

Hello ThomDietrich,

i think i have the same plan as you already solved :slight_smile:

I have a lot of temperature sensors on my heating system. One of my pumps has an integrated heat meter function, but requires an analog input 0-10V from the return.

I have already integrated some DS18B20 and read them out via an ESP32. Now I am looking for a solution how I can generate a 0-10v output sensor via the ESP.

I still don’t understand in your solution how you generate the percentage output from another temperature sensor in Hassio and return it to the ESP? If I overwrite the 30% in hassio, will it be set back to 30% after 60 seconds and not the value in the ESP changed? What am i forgetting?

thank you

Hey!
Above I only showed the code for ESPHome. This device exposes a service (through the code in the api section), which can be called by a Home Assistant automation. The sliders that I showed in the screenshot have this automation behind:

automation:
  - id: "1618577571740"
    alias: Regel Heizungsregelung Sollwert über Prozent Schieberegler
    trigger:
      - platform: state
        entity_id: input_number.heizungsregelung_sollwert_prozent
    action:
      - service: esphome.heizungsregelung_eigenbau_set_heiz_sollwert_percent_pwm
        data:
          percentage: "{{ trigger.to_state.state | int }}"

Hope this helps!

2 Likes

Dear @ThomDietrich,

I know it’s been a while, but I hope I can ping you still. I’d like to solve a very similar issue: control my Wolf CWL400 ventillation system with 0-10V input based on various existing HA sensors data (presence, humidity, temperature, etc).

Would you be willing to share with me/us your complete config - both ESP yaml, and HA automation setup - and your electronic setup as well (what devices did you use as ESP and as PWM), so I can have it as a guide to build my own setup?

What I’d like to achieve:

  • be able to read from ESP what is the current level of ventillation
  • be able to instruct my ESP to set PWM to certain level based on HA sensor changes (eg. humidity drops below certain value, then drop ventillantion to X%, overrule this based on time of day, eg- have a constant low PWM during the night, and completely shut down ventillation when outside air quality sensor reports bad aur quality near air intake to avoid spreading bad smells in the house).

Many thanks in advance!

Hey! Sorry I didn’t answer. Reason being, that we postponed the implementation of this new heating control. I wanted to get it finished for this winter but we decided to hold off and install, test and take online for the winter to come.

Everything I can provide to learn from is posted above. Hope it helped

1 Like

Hi @ThomDietrich

Could you tell me what kind of hardware you’re using? I plan on doing something similar as you, I want to control my heat pump via a 0-10V Input when I have excess power from my solar panels.

Cheers
Dani

Hey Daniel,
I used something like this: 1-3KHZ 0-10V PWM Signal to Voltage Converter Module

Hope this helps.

When I finally got around finishing this project I HAVE to post the full project here :slight_smile:

You may also look at this Hardware from kincony - KC8568-A4 (ESP32 Board with 2x 0-10V Outputs and several other stuff)

I used this in my project and it worked with the ESP32 DAC - I get between DC 0V and 9.52V
https://www.kincony.com/forum/showthread.php?tid=1093

ESP32 has TWO DAC (Digital to Analog) channels built in – these will generate 0 to 3.3v. Then you can use an op-amp to boost it to 0-10v. The only thing is you also need a voltage source at or above 10v for the opamp to control. You can find cheap buck or boost converter modules to provide that.

BUT READ YOUR DEVICE MANUAL FIRST! Some 0-10v devices can be put into different modes, for example to be controlled by 0 to 3v. So then you can control it directly from the ESP32 without any extra nonsense. I wish I had known this before I bought my actuators as I would have tried to find the models that do this (they are referenced in the manual for the 0-10v only model I did purchase). Check out Siemens GDE163.1P and GDE164.1P which can be adjusted for anything between 0-5v start to 0-30v finish… so you can do 0-2v, 0-3v, 1-5v, 4-30v etc.

Bellimo MFT actuators can supposedly be reprogramed in many ways, but I am not sure if you need proprietary/licensed $ software to do it?

"Belimo damper actuators and control valves with Multi-Function Technology (MFT) are an excellent way to standardize your product line while reducing the number of different actuators needed. MultiFunction Technology offers a wide variety of programmable control inputs and feedback signals. Parameters can be set for voltage control (VDC), time proportional control (PWM), floating point, on-off, feedback signal, or torque output. Parameters can be changed on-site to optimize/enable application. You can also set, modify or read position, running time, mechanical working range, address,
status, and diagnostics. "

4 posts were split to a new topic: Need Help creating pwm signal voltage converter

Hey friends, this has been working well for over a year. Here’s a recap:

I am working with a standard ESP32 and this converter connected to a power supply and the actual heating control system:

https://www.aliexpress.com/item/32965606376.html

And here is my latest version of my ESPHome configuration (in packages > include format).

output:
  - platform: ledc
    pin: GPIO27
    frequency: 1220 Hz
    id: heiz_sollwert_pwm_output
    inverted: False
    min_power: 0.00
    max_power: 1.00

esphome:
  on_boot:
    - priority: 500
      # When hardware initialized
      # Failsafe temperature, approx. 30°C = 20% PWM
      then:
        - lambda: |-
            id(heiz_sollwert_pwm_output).set_level(0.2);

sensor:
  - platform: template
    name: "Gastherme Sollwert ESP (Temperatur)"
    id: gastherme_sollwert_degc_sensor
    icon: "mdi:thermometer-chevron-up"
    unit_of_measurement: "°C"
    accuracy_decimals: 1
    update_interval: never

api:
  services:
    - service: gastherme_solltemperatur_set
      variables:
        temperature: float
      then:
        # The formulas for volt and pwm_pct have to be found by experiment
        - lambda: |-
            double degc = 1.0 * temperature;
            double volt = degc * 0.1253 - 1.2751;  
            double pwm_pct = (volt - 0.4535) * 10.05;  

            id(gastherme_sollwert_degc_sensor).publish_state(
              min(90.0, max(0.0, degc))
            );
            id(heiz_sollwert_pwm_output).set_level(
              min(1.0, max(0.0, pwm_pct / 100.0))
            );

In comparison, most logic is now inside the ESP and HA receives final values and has temperature control via a HA service.


Beware. @darren-ha has raised a good point. The combination of service and sensor could be implemented via a Template Number. Be sure to check out the discussion below.

Cheers

1 Like

This thread has been very helpful - my initial experience integrating an ESPHome device with Home Assistant had me feeling things were circular/“inside out” (similar to OP’s early comments on this thread). Thanks to all here for the resources and exposition that helped reorient me!

@ThomDietrich , a follow-up question on your latest config: Is there a reason you don’t use ESPHome’s number component (specifically a template number in optimistic mode) as the input control entity? (I gather you are using an input_number on the HA side?)

Context: My use case is also managing an ESP-based PWM from HA; my rationale for using ESPHome’s template number is that it allows me to push even more of the “business logic” (e.g. min/max allowed input values) onto the ESP, leaving HA to serve only higher-level functions (e.g. UI, scheduling) - but I’m curious if you see reasons to prefer not using the ESPHome component.

Hey Darren, great question about an interesting detail.

I would say the number entity is definitely an option and not a bad one. I thought about the pros and cons for some time just now. After dismissing most of them I think the only real reason lies within what you’ve raised yourself:

I gather you are using an input_number on the HA side?

No and a way to unintentionally provide a set temperature directly by a user is something I want to avoid. In Home Assistant I am using a rather complex set of input fields in a dashboard, summarizing template sensors and finally automations to decide on the heating set temperature. This one is then send as a command via the service.
The service approach has two advantages:

  1. A service can accept multiple parameters besides the set temperature. Something I’ve used with other use cases.
  2. The service call does not create it’s own entry in the Home Assistant logbook. That is something you might not care about but for debugging purposes I prefer that.

So long story short. I think I could have done it either way, but the service approach is cleaner.

allows me to push even more of the “business logic” (e.g. min/max allowed input values) onto the ESP

That is an excellent argument for the number entity.

Edit: Over the course of this message I did remove the intermediary sensors from the code above. These were there for debugging only.

1 Like