How to properly use value templates with math to compare states?

If bathroom humidity < bedroom humidity + 5, do something

How do I write this condition? This doesn’t work as it appears to ignore the value template entirely, and also does not let me write it without above/below:

condition: numeric_state
entity_id: sensor.bath_humidity
below: sensor.bed_humidity
value_template: |-
  {{ states('sensor.bath_humidity') | float <=
     states('sensor.bath_humidity') | float - 10}}

Oh, apparently template is an option and I don’t need to use numeric state to invoke it. My value_template yml above is correct as is. So the solution is just this:

condition:
  - condition: template
    value_template: >-
      {{ states('sensor.bath_humidity') | float <=
      states('sensor.bed_humidity') | float - 10}}

Just for anyone looking for an answer to the original question who wants to use a Numeric state condition.

You can structure it a couple ways, but in either case your template should return a numeric value which is then compared to the value you use in the above or below fields.

condition: numeric_state
entity_id: sensor.bath_humidity
value_template: "{{ state.state | float - 5 }}"
above: sensor.bedroom_humidity

or

condition: numeric_state
entity_id: sensor.bath_humidity
value_template: "{{ states.state | float -  states('sensor.bed_humidity') | float }}"
above: 5
1 Like