Binary Sensor not evaluating same as template in UI

I have the following logic that evaluates to on in Developer Tools > Template. However HASS evaluates it as off when set as a binary sensor. What have I missed?

- platform: template
  sensors:
    bedroom_aircon_needed_on:
      value_template: >- 
        {% if ((states.sensor.bom_perth_air_temp_c | float) < 20) and ((states.sensor.temperature_158d0001fd8502 | float) < 18) %}
          on
        {% elif (( states.sensor.bom_perth_air_temp_c | float) > 20) and ((states.sensor.temperature_158d0001fd8502 | float) > 24) %}
          on
        {% else %} 
          off
        {% endif %}

There’s a couple problems with this. First you’re not using the states correctly. Second, the template needs to evaluate to true/false, not on/off. Try this:

- platform: template
  sensors:
    bedroom_aircon_needed_on:
      value_template: >
        {% set air_temp = states('sensor.bom_perth_air_temp_c')|float %}
        {% set temp = states('sensor.temperature_158d0001fd8502')|float %}
        {{ air_temp < 20 and temp < 18 or air_temp > 20 and temp > 24 }}