I recently embarked on a project to integrate a custom Kenwood receiver using an ESPHome setup with a digital potentiometer. While the initial setup worked smoothly, I encountered a roadblock when trying to implement volume control buttons. The issue revolved around incrementing and decrementing the volume level, which led to some perplexing errors in my code.
Here’s a snippet of my YAML configuration:
yaml
button:
- platform: template
name: Anlage lauter
id: lautstaerke_plus
on_press:- logger.log: Anlage lauter
- output.set_level:
id: lautstaerke_digipoti
level: !lambda ‘return id(lautstaerke_digipoti) + 1;’
- platform: template
name: Anlage leiser
id: lautstaerke_minus
on_press:- logger.log: Anlage leiser
- output.set_level:
id: lautstaerke_digipoti
level: !lambda ‘return id(lautstaerke_digipoti) - 1;’
The error I encountered was:
Compiling .pioenvs\verstaerker\src\main.cpp.o
verstaerker.yaml: In lambda function:
verstaerker.yaml:89:35: error: cannot convert ‘esphome::output::FloatOutput*’ to ‘float’ in return
After some research and debugging, I realized that the issue stemmed from how I was accessing the state of the output. Instead of directly manipulating the output ID, I needed to correctly reference the state variable. This led me to modify my lambda functions to properly retrieve and update the current state.
One helpful resource I stumbled upon was a discussion on the ESPHome community forum, where a user shared their experience with a similar issue. They mentioned that accessing the state requires using the .state property, which I had overlooked in my initial implementation.
Here’s the corrected approach I adopted:
yaml
button:
- platform: template
name: Anlage lauter
id: lautstaerke_plus
on_press:- logger.log: Anlage lauter
- output.set_level:
id: lautstaerke_digipoti
level: !lambda ‘return id(lautstaerke_digipoti).state + 1;’
- platform: template
name: Anlage leiser
id: lautstaerke_minus
on_press:- logger.log: Anlage leiser
- output.set_level:
id: lautstaerke_digipoti
level: !lambda ‘return id(lautstaerke_digipoti).state - 1;’
This adjustment resolved the compilation errors, and the volume control buttons now function as intended. It was a valuable learning experience, emphasizing the importance of correctly referencing state variables in ESPHome configurations.
For anyone else working on similar projects, I recommend thoroughly reviewing the ESPHome documentation and actively participating in community forums. These resources have been invaluable in troubleshooting and refining my setup. Happy coding! ![]()