On_value_range question- how to use lambda?

Consider the following:

    on_value_range:
      - below: 50.0
        then:
          - light.turn_off: indicator_led
      - above: 50.0
        then:
          - light.turn_on: indicator_led

What if the sensor = 50?

I am sure the answer is to use lambda, but how?

I think you are better off using lambda - I have had some weird experiences with on_value_range:. For example, if it was above 50 and then next update it becomes 50, it evaluates as below: 50. And vice versa, if it’s below 50 and becomes 50, it evaluates as above:

I don’t know if it’s a bug, but at least I can understand the logic of lambda…

But, where in my YAML do I put the lambda?

It’s mostly academic because in the environment where this sensor goes, 50 is not possible. It will be either below 20, or above 80. But, I am curious how someone would handle the =50 case.

Like this:

on_value:
  then:
    - if:
        condition:
          lambda: 'x = 50;'
        then:
          - logger.log: "The sensor value is 50!"
        # ... more actions
    - if:
        condition:
          lambda: 'x > 50;'
        then:
          - logger.log: "The sensor value greater than 50!"
        # ... more actions
# etc...

“x” always refers to the state of the sensor the block is in.

Thanks. I’ve added this to my notebook.