Lambda Order of Operations

Having issues getting the value I want from what seems to be a simple lambda.

I have these two actions to set the text opacity of some Labels. I want some labels a bit dimmer than others so I thought I could just subtract 15 from the “clock_text_opacity” value before applying the scaling factors for text_opa.

As it sits, it compiles and deploys with no issues. The problem is the second one doesn’t return the correct value. What I want is to subtract 15 from “clock_text_opacity” before applying the scaling factors. Due to order of operations, what’s happening is it’s first dividing 15 by 100, then multiplying by 255, then subtracts that value from the “clock_text_opacity” value.

So I figured it just needed some parentheses in the correct place to force it to subtract 15 from “clock_text_opacity” and then apply the scaling. Problen is every place I add them it won’t compile.

If I change the equation to

!lambda return id((clock_text_opacity).state - 15) / 100 * 255;

it returns the error shown below the code snippet.

Any ideas?

number:
  - platform: template
    name: Text Opacity
    optimistic: true
    id: clock_text_opacity
    unit_of_measurement: "%"
    initial_value: 100
    restore_value: true
    min_value: 1
    max_value: 100
    step: 1
    mode: box
    on_value: 
      then:
        - lvgl.label.update:
            id: 
              - header_display_time
              - page_prev
              - page_home
              - page_next
              - display_time
              - display_temperature
              - display_humidity
              - display_pressure
              - display_pressure_trend
              - display_pm25
              - display_pm10
              - display_co2
            text_opa: !lambda return id(clock_text_opacity).state / 100 * 255;
                      # Desired value needs to be scaled from a percent, then to a 0-255 range
        - lvgl.label.update:
            id: 
              - display_pm25_id
              - display_pm25_units
              - display_pm10_id
              - display_pm10_units
              - display_co2_id
              - display_co2_units
            text_opa: !lambda return id(clock_text_opacity).state - 15 / 100 * 255;
                      # Desired value needs to be scaled from a percent, then to a 0-255 range
/config/esphome/livingroom-clock.yaml: In lambda function:
/config/esphome/livingroom-clock.yaml:764:74: error: request for member 'state' in 'clock_text_opacity', which is of pointer type 'esphome::template_::TemplateNumber*' (maybe you meant to use '->' ?)
       then:
                                                                          ^    
/config/esphome/livingroom-clock.yaml:765:77: error: request for member 'state' in 'clock_text_opacity', which is of pointer type 'esphome::template_::TemplateNumber*' (maybe you meant to use '->' ?)
         - lvgl.label.update:
                                                                             ^    
/config/esphome/livingroom-clock.yaml:766:74: error: request for member 'state' in 'clock_text_opacity', which is of pointer type 'esphome::template_::TemplateNumber*' (maybe you meant to use '->' ?)
             id:
                                                                          ^    
/config/esphome/livingroom-clock.yaml:767:77: error: request for member 'state' in 'clock_text_opacity', which is of pointer type 'esphome::template_::TemplateNumber*' (maybe you meant to use '->' ?)
               - header_display_time
                                                                             ^    
/config/esphome/livingroom-clock.yaml:768:73: error: request for member 'state' in 'clock_text_opacity', which is of pointer type 'esphome::template_::TemplateNumber*' (maybe you meant to use '->' ?)
               - page_prev
                                                                         ^    
/config/esphome/livingroom-clock.yaml:769:76: error: request for member 'state' in 'clock_text_opacity', which is of pointer type 'esphome::template_::TemplateNumber*' (maybe you meant to use '->' ?)
               - page_home
                                                                            ^    
*** [.pioenvs/livingroom-clock/src/main.o] Error 1

you have 15 inside the id() function. 15 is not part of the id. Also your % calculation is incorrect you need to multiply by 100 not divide. Try this:

!lambda return 100 * (id(clock_text_opacity).state - 15) / 255;

You probably also need to convert this number to an integer.

id(clock_text_opacity).state / 100 * 255 is the correct equation. I just incorrectly said it needs to be converted to a percent when what actually needs to happen is it needs to be converted from a percent since “clock_text_opacity” is the desired opacity percent. So the divide by 100 is correct and does work.

Changed it to (id(clock_text_opacity).state - 15) / 100 * 255; and it works as needed.

Thanks. These lambdas always get me.