Display slow_pwm actual value to a Text Sensor

Hi,
I have an Output Slow PWM, and I would like to display on Home Assistant and on Web Interface the actual value of slow_pwm.

This is code to create slow_pwm:

output:
  - platform: slow_pwm
    pin: D4
    id: slow_pwm_h
    period: 1s

I tried this:

sensor:
  - platform: template
    name: PWM Value
    id: pwm_value
    lambda: |-
            return to_string(id(slow_pwm_h).SlowPWMOutput);
    update_interval: 1s

But code does not compile.
Any Help would be appreciated.

Marco

Try

    lambda: |-
      return {std::to_string(id(slow_pwm_h).state)};
1 Like

Hi,
it does not work, this is the error:

Compiling .pioenvs/reflow-oven-controller/FrameworkArduino/Esp-frag.cpp.o
/config/esphome/reflow-oven-controller.yaml: In lambda function:
/config/esphome/reflow-oven-controller.yaml:90:42: error: 'class esphome::slow_pwm::SlowPWMOutput' has no member named 'state'; did you mean 'float esphome::slow_pwm::SlowPWMOutput::state_'? (not accessible from this context)
   90 |             return {std::to_string(id(slow_pwm_h).state)};
      |                                          ^~~~~
In file included from src/esphome.h:55,
                 from src/main.cpp:3:
src/esphome/components/slow_pwm/slow_pwm_output.h:53:9: note: declared protected here
   53 |   float state_{0};
      |         ^~~~~~
/config/esphome/reflow-oven-controller.yaml:90:48: error: could not convert '{<expression error>}' from '<brace-enclosed initializer list>' to 'esphome::optional<float>'
   90 |             return {std::to_string(id(slow_pwm_h).state)};
      |                                                ^
      |                                                |
      |                                                <brace-enclosed initializer list>
Compiling .pioenvs/reflow-oven-controller/FrameworkArduino/Esp-version.cpp.o
Archiving .pioenvs/reflow-oven-controller/libf5a/libSPI.a
Compiling .pioenvs/reflow-oven-controller/FrameworkArduino/Esp.cpp.o
*** [.pioenvs/reflow-oven-controller/src/main.cpp.o] Error 1

It doesn’t seem to allow it to be accessed like that. What component actually uses the output? If it is a light or a fan, the value of the output will be represented by a slider.

The component using it should have a way to display it’s value.

1 Like

Hi,
it is a climate. I found an alternative way, I leave it here for future reference. I turn on and off a template switch based on Slow PWM status:

switch:
  - platform: template
    id: fake_heater_h
    name: PWM Status
    optimistic: true

output:
  - platform: slow_pwm
    pin: D4
    id: slow_pwm_h
    period: 2s
    turn_on_action:
      - switch.turn_on: fake_heater_h
    turn_off_action:
      - switch.turn_off: fake_heater_h

In this way the switch reflect the status of the PWM output that control an Oven’s heater.

Marco

1 Like