Q: sensor.in_range dynamic below-value via lambda?

Hello together,
I’m wondering if its possible to set a dynamic below: value at sensor.in_range?

working code snipped:

interval:                                                                               
  - interval: 2s                                                                        
    then:                                                                               
      if:                                                                               
        condition:                                                                      
          sensor.in_range:                                                              
            id: ntc_1                                                                   
            below: 23                                                                   
        then:                                                                           
          fan.turn_off: fan_1      

I would like to have the below limit dynamical. It should be the value of a 2nd sensor plus an offset.
Here is my try:

interval:                                                                               
  - interval: 2s                                                                        
    then:                                                                               
      if:                                                                               
        condition:                                                                      
          sensor.in_range:                                                              
            id: ntc_1                                                                   
            below: !lambda "return id(ntc_2).state + 1.0;"
        then:                                                                           
          fan.turn_off: fan_1      

But during compilation, i get the error: “expected float.”

Failed config

interval: [source ESP32_3_NTC.yaml:238]
  - [source ESP32_3_NTC.yaml:238]
    interval: 2s
    then:  [source ESP32_3_NTC.yaml:240]
      if:  [source ESP32_3_NTC.yaml:241]
        condition:  [source ESP32_3_NTC.yaml:242]
          sensor.in_range:  [source ESP32_3_NTC.yaml:243]
            id: ntc_1
            
            expected float.
            below: !lambda |-
              return id(ntc_2).state + 1.0; [source ESP32_3_NTC.yaml:245]

Any hint would be helpful, thank you in advance.

best regards, thomas

After a hint, i removed the sensor.in_range and add a lambda to solve the issue.

lambda: !lambda |-
if (id(ntc_3).state < id(ntc_2).state + 1.0) { 
 return true; 
} 
else { 
 return false; 
 }  

I’m having the same issue, but I do not completely understand your solution. can you post you this solution fits in you code?
my code is as follow:

    on_value:
      then:
        - if:
            condition:
              sensor.in_range:
                id: buffer_onder
                above: 75
            then:    
              - script.execute: start_cooling    
        - if:
            condition:
              sensor.in_range:
                id: buffer_onder
                above: 65
                below: 75
            then:    
              - script.execute: start_cooling

and I want to replace the 65 degree with a variable.

I think I figured it out:

        - if:
            condition:
              # buffer onder tussen min buffer uit front_end en 65 graden
              lambda: |-
                if ((id(buffer_onder).state < 65) and
                (id(buffer_onder).state > id(min_buffer).state)){
                  return true;
                } else {
                  return false;
                }
2 Likes