I’m trying to get a ESP device do a service call from lambda.
If it can be done without lambda then that is fine too.
But from searching I found some code that is rather old but I can’t get it to work.
It first complained about servicecall not existing, should it be action, and even with that change it does not work.
sensor:
- platform: homeassistant
id: david_brightness
entity_id: light.davids_lampa
attribute: brightness
- platform: rotary_encoder
name: "Brightness Encoder"
id: brightness_encoder
pin_a: GPIO5
pin_b: GPIO6
internal: true
on_value:
- lambda: |-
HomeassistantServiceResponse resp;
resp.service = "light.turn_on";
HomeassistantServiceMap entity_id_kv;
entity_id_kv.key = "entity_id";
entity_id_kv.value = "light.davids_lampa";
resp.data.push_back(entity_id_kv);
entity_id_kv.key = "brightness";
// Calculate change from last position
int current = (int)x;
int delta = current - id(last_encoder_value);
id(last_encoder_value) = current;
float new_brightness = id(david_brightness).state + (delta * 5.0);
// Clamp to valid range
if (new_brightness < 0) new_brightness = 0;
if (new_brightness > 255) new_brightness = 255;
if (new_brightness != id(david_brightness).state) {
entity_id_kv.value = to_string(new_brightness));
resp.data.push_back(entity_id_kv);
id(api_id).send_homeassistant_action(resp);
ESP_LOGI("encoder", "Brightness adjusted to %.0f", new_brightness);
}
The rotary encoding part of the code works but it’s the action part that is the issue. It pretty much gives an error on every line of the code.
I can’t find any documentation or other posts about lamda actions.
Summary
/config/esphome/david-sang.yaml: In lambda function:
/config/esphome/david-sang.yaml:114:7: error: 'HomeassistantServiceResponse' was not declared in this scope
114 | HomeassistantServiceResponse resp;
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~
/config/esphome/david-sang.yaml:115:7: error: 'resp' was not declared in this scope
115 | resp.service = "light.turn_on";
| ^
/config/esphome/david-sang.yaml:117:26: error: no match for 'operator=' (operand types are 'esphome::StringRef' and 'const char [10]')
117 | entity_id_kv.key = "entity_id";
| ^~~~~~~~~~~
In file included from src/esphome/core/application.h:17,
from src/esphome/components/api/api_frame_helper.h:13,
from src/esphome/components/api/api_connection.h:5,
from src/esphome.h:3,
from src/main.cpp:3:
src/esphome/core/string_ref.h:26:7: note: candidate: 'constexpr esphome::StringRef& esphome::StringRef::operator=(const esphome::StringRef&)'
26 | class StringRef {
| ^~~~~~~~~
src/esphome/core/string_ref.h:26:7: note: no known conversion for argument 1 from 'const char [10]' to 'const esphome::StringRef&'
src/esphome/core/string_ref.h:26:7: note: candidate: 'constexpr esphome::StringRef& esphome::StringRef::operator=(esphome::StringRef&&)'
src/esphome/core/string_ref.h:26:7: note: no known conversion for argument 1 from 'const char [10]' to 'esphome::StringRef&&'
/config/esphome/david-sang.yaml:118:28: error: no match for 'operator=' (operand types are 'esphome::StringRef' and 'const char [19]')
118 | entity_id_kv.value = "light.davids_lampa";
| ^~~~~~~~~~~~~~~~~~~~
src/esphome/core/string_ref.h:26:7: note: candidate: 'constexpr esphome::StringRef& esphome::StringRef::operator=(const esphome::StringRef&)'
26 | class StringRef {
| ^~~~~~~~~
src/esphome/core/string_ref.h:26:7: note: no known conversion for argument 1 from 'const char [19]' to 'const esphome::StringRef&'
src/esphome/core/string_ref.h:26:7: note: candidate: 'constexpr esphome::StringRef& esphome::StringRef::operator=(esphome::StringRef&&)'
src/esphome/core/string_ref.h:26:7: note: no known conversion for argument 1 from 'const char [19]' to 'esphome::StringRef&&'
/config/esphome/david-sang.yaml:120:26: error: no match for 'operator=' (operand types are 'esphome::StringRef' and 'const char [11]')
120 | entity_id_kv.key = "brightness";
| ^~~~~~~~~~~~
src/esphome/core/string_ref.h:26:7: note: candidate: 'constexpr esphome::StringRef& esphome::StringRef::operator=(const esphome::StringRef&)'
26 | class StringRef {
| ^~~~~~~~~
src/esphome/core/string_ref.h:26:7: note: no known conversion for argument 1 from 'const char [11]' to 'const esphome::StringRef&'
src/esphome/core/string_ref.h:26:7: note: candidate: 'constexpr esphome::StringRef& esphome::StringRef::operator=(esphome::StringRef&&)'
src/esphome/core/string_ref.h:26:7: note: no known conversion for argument 1 from 'const char [11]' to 'esphome::StringRef&&'
/config/esphome/david-sang.yaml:145:54: error: no match for 'operator=' (operand types are 'esphome::StringRef' and 'std::string' {aka 'std::__cxx11::basic_string<char>'})
145 | entity_id_kv.value = to_string(new_brightness));
| ^
src/esphome/core/string_ref.h:26:7: note: candidate: 'constexpr esphome::StringRef& esphome::StringRef::operator=(const esphome::StringRef&)'
26 | class StringRef {
| ^~~~~~~~~
src/esphome/core/string_ref.h:26:7: note: no known conversion for argument 1 from 'std::string' {aka 'std::__cxx11::basic_string<char>'} to 'const esphome::StringRef&'
src/esphome/core/string_ref.h:26:7: note: candidate: 'constexpr esphome::StringRef& esphome::StringRef::operator=(esphome::StringRef&&)'
src/esphome/core/string_ref.h:26:7: note: no known conversion for argument 1 from 'std::string' {aka 'std::__cxx11::basic_string<char>'} to 'esphome::StringRef&&'
/config/esphome/david-sang.yaml: In lambda function:
/config/esphome/david-sang.yaml:517:3: warning: control reaches end of non-void function [-Wreturn-type]
517 |
| ^
*** [.pioenvs/david-sang/src/main.cpp.o] Error 1
========================= [FAILED] Took 48.75 seconds =========================

