Trouble getting String global to be used inside lambda function

Hi all.

I’m quite new on esphome. Still learning and try to understand how all works.
Everything was going fine, until now. I need some help.

I’m playing with a ESP32_CAM. It’s already working, but still need some tuning.
But what I want to do is make a card on HomeAssistant to set some parameters of the camera.

I set brightness, saturation and contrast already, making globals and getting the values from homeassistant. This way:

globals:
  - id: brightness_atual
    type: int
    initial_value: "0"
  - id: contraste_atual
    type: int
    initial_value: "0"
  - id: saturacao_atual
    type: int
    initial_value: "0"

sensor:
  - platform: homeassistant
    id: ha_brightness
    entity_id: input_number.camera_aquario_sala_brightness
    on_value:
      then:
        if:
          condition:
              lambda: 'return id(brightness_atual) != int(x);'
          then:
            - logger.log:
                level: INFO
                format: 'Mudança no Brightness : de %d para %d'
                args: ['id(brightness_atual)', 'int(x)']
            - globals.set:
                id: brightness_atual
                value: !lambda 'return int(x);'
            - lambda: |-
                id(camera).set_brightness(x);
  - platform: homeassistant
    id: ha_contraste
    entity_id: input_number.camera_aquario_sala_contraste
    on_value:
      then:
        if:
          condition:
              lambda: 'return id(contraste_atual) != int(x);'
          then:
            - logger.log:
                level: INFO
                format: 'Mudança no Contraste : de %d para %d'
                args: ['id(contraste_atual)', 'int(x)']
            - globals.set:
                id: contraste_atual
                value: !lambda 'return int(x);'
            - lambda: |-
                id(camera).set_contrast(x);
  - platform: homeassistant
    id: ha_saturacao
    entity_id: input_number.camera_aquario_sala_saturacao
    on_value:
      then:
        if:
          condition:
              lambda: 'return id(saturacao_atual) != int(x);'
          then:
            - logger.log:
                level: INFO
                format: 'Mudança na Saturacao_atual : de %d para %d'
                args: ['id(saturacao_atual)', 'int(x)']
            - globals.set:
                id: saturacao_atual
                value: !lambda 'return int(x);'
            - lambda: |-
                id(camera).set_saturation(x);

This way, I can set thos adjustments directly from HA to reflect on the fly.
But Now, I’d like to also change resolution, whitout having to compile a new code in esphome.
So I did the same thing, adding:

- id: resolucao_atual
   type: std::string
   initial_value: '"800x600"'

- platform: homeassistant
   id: ha_resolucao
   entity_id: input_select.resolucao_da_camera_aquario_sala
   on_value:
     then:
       if:
         condition:
             lambda: 'return id(resolucao_atual) != x.c_str();'
         then:
           - logger.log:
               level: INFO
               format: 'Mudança na Resolução : de %s para %s'
               args: ['id(resolucao_atual)', 'x']
           - globals.set:
               id: resolucao_atual
               value: !lambda 'return x;'

I know the resolution cannot be changed on the fly, need a restart. But my intention here is set the resolution I want and push a button to restart ESPHOME device and apply the new resolution.

But here starts my problems:
This way I get a compiler errors about the type of my global variable and the X value on the lambda function:

/config/camera-aquario-sala.yaml:156:44: error: request for member 'c_str' in 'x', which is of non-class type 'float'
               lambda: 'return id(resolucao_atual) != x.c_str();'
                                            ^~~~~

I have tried to remove the .c_str() from x, and algo got erros.

/config/camera-aquario-sala.yaml:156:39: error: no match for 'operator!=' (operand types are 'std::__cxx11::basic_string<char>' and 'float')
               lambda: 'return id(resolucao_atual) != x;'

It gives me Tons of warnings and also the following error:

src/esphome/core/automation.h:33:46: error: no matching function for call to 'std::function<std::__cxx11::basic_string<char>(float)>::function(setup()::<lambda(float)>&)'
   TemplatableValue(F f) : type_(LAMBDA), f_(f) {}
                                              ^

I have no clue whats wrong.

Home assistant sensor is only for numerical values.
Try with text sensor.

Thanks !

I’ll try later to test. I am trying another aproach, that’s trhough API services.

So another question: which aproach is best to use (or more recommended): api services or through sensors with platform homeassistant ?

Regards.

I don’t know. HA sensors are more commonly used to get data from HA to esphome.

1 Like

looks like the HA integration is more reliable than API services. I’ll migrate then.

But I’m givving up for a while on trying to change resolution on the fly.

My intention was setting the asked resolution on a global variable and order a boot on the device. On boot, I restore that global value on the camera configuration, on boot and use it on camera settings.
But looks like I cannot use lambdas or another reference to global when defining this resolution config. So, I’ll focus on other problems and come back to this later.

Thanks.