Condition for one temperature being lower than another?

I am a little stuck on how to create triggers and conditions for an automation I’d like that is depending on temperatures of two sensors.

In pseudo-code I’d like the automation to be triggered when:

IF [inside temperature] > 24 AND 
   [outside temperature] < [inside temperature] AND 
   [clerestory window state] = 'Closed'

What I’ve currently got is as below. However the second condition is where I’d like to compare the inside temperature to the outside temperature so that the second condition is only satisfied when outside is cooler than inside.

Any ideas, help, pointers to more reading / examples would be very much appreciated

- id: '1586585163400'
  alias: Clerestory - Auto - In over 24 and outside less
  trigger:
  - above: '24'
    entity_id: sensor.dining_temperature
    platform: numeric_state
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: input_select.clerestory_window
      state: Closed
    - below: '24'
      condition: numeric_state
      entity_id: sensor.outside_temperature
  action:
  - data: {}
    entity_id: switch.openclerestory
    service: switch.turn_on

Hmm… I think I might have found my answer in:

I’ll test that.

well, your [inside temperature] > 24 and [clerestory window state] = 'Closed' are state conditions but [outside temperature] < [inside temperature] is a template one as more than one entity is involved. considering that I’d create just one template condition for all.

As a side note, I’d suggest always using official docs as a starting point and be careful with older forum posts because HA changes rapidly and many older solutions might no longer work.

1 Like

The second post is still accurate, I’ve done something similar and running a time based trigger, with conditions that compare temperatures seems to be the most reliable.

The way you initially started it, would result in the automation only running once when true, and then not again until the temperature changed so that the automation went false, and the back up again to true.

Thanks AhmadK… Had a good read of the docs you linked to and have adjusted accordingly.
My template condition now reads:

{{ states('sensor.dining_temperature') > states('sensor.outside_temperature') }}

I presume that you changed only one condition and the first one remains, is that correct?
One of my points was to have only one condition, not 2 or 3 as soon as you use template.
Something like this

condition:
  condition: template
  value_template: >
    {{
      states('sensor.dining_temperature') > states('sensor.outside_temperature') and
      is_state('input_select.clerestory_window', 'Closed')
    }}

Glad to hear you have a solution. You might post the working config here for future reference.

If it works for you, please consider marking a post as a Solution (by ticking Solution box next to it) as it will show your topic as Solved and also add link to the solution below your original post, which will make it easy for future readers to find the solution.

I ‘sort of’ agree with @callifo in that changing temperatures make poor conditions.
But I also dislike time based triggers that basically check every minute (though I do have a few of them).
Is security a concern here ? If so you may need to incorporate ‘occupancy’ into the equation.
I think I’d simplify this to a single binary sensor, all the necessary factors would be self evident from the template to the state machine so any change would update the result (depending on updates this ‘may’ update more frequently than callifo’s suggestion) this sensor can then be used to both open and close the windows, concentrating all the processing in the sensor, with the (single) automation fired only on edge triggers.
Oh and unanticipated reboots would always resolve correctly too.

You should have enough here to work out how to do it but an extra clue is that the automation would use a service template in the action. Let us know if you run into difficulties.

Thanks AhmadK, Didn’t tweak that I would have to have all conditions in the template. Good to know.

I think you meant ‘twig’ (become aware of)

Creating a binary sensor sounds interesting. Haven’t done that yet… so more learning :slight_smile:

Trying to find some documentation on how to “code” such a binary sensor. I have yet find the groove on how to best search on home-assistant.io. For example typing in “binary sensor” into the search suggests results to me based on Z-Wave and MQTT sensors returning binary values.

Any hint on where the documentation is that will help me create a binary sensor?

Go first to integrations, then search from there.

I have also had issues finding stuff that I used to take for granted. I generally fall back on going to Google and “home assistant binary sensor” and choosing from there.

Glad to hear it’s not just me… :smiley:

Anyway, I think I found a good start for creating a template binary sensor at:

10 out of 10 so far :rofl:

Here is a ‘fairly complex’ binary sensor and an automation that flip flops depending on a result
Just as hints (we don’t want to make it toooo easy for you :rofl: otherwise you won’t learn and youll still be asking the same questions a year from now :smile: )

binary_sensor:
  - platform: template
    sensors:
      ## day segment bs
      bs_fan_run:
        entity_id: sensor.time, binary_sensor.bs_global_noise_permissive
        value_template: >
          {% set time = states('sensor.time') %}
          {% set slt1en = is_state('binary_sensor.bs_global_noise_permissive', 'on') %}
          {% set slt1start = states('input_datetime.id_fans_run_daily_on') [0:5] %}
          {% set slt1stop = states('input_datetime.id_fans_run_daily_off') [0:5] %}
          {% set slt1on = slt1start <= time < slt1stop %}
          {{ slt1on and slt1en }}
        friendly_name: Fan Run Slot
        icon_template: "{{ 'mdi:clock-fast' }}"

automation:
  - alias: au_fans_run_on
    trigger:
      - platform: state
        entity_id: binary_sensor.bs_fan_run
    action:
      - service_template: switch.turn_{{trigger.to_state.state}}
        entity_id: switch.fibsw_bthf_sw, switch.fibsw_ensf_sw, switch.fibdblrly_lftf_sw2

Edit: You never said whether an open window was a security concern if you were out.
Could someone (even if they need a ladder) get it through these windows ?
Do you have occupancy worked out ?

Edit 2: changed the sensor as then these two are linked

Edit 3: even though I recognise the hypocrascy in that it’s a time based trigger

:rofl:

Another point to consider is the response of the temperature sensors in say a gust of wind, you might find yourself on a knife edge where the window starts to close, then opens again (or visa versa)
I don’t think this will happen as most temperature sensors report after a set delay or a given delta T. But its wise to be aware and have a plan in place.
You might be wise looking into delay_on: and delay_off: for your binary_sensor or applying hysteresis to your temperature.

Wow Mutt… That is super helpful. You really went out of your way to help. Thanks a lot.

Security is not a concern at this time. Windows are quite small and very high up.

Occupancy is something I haven’t bothered with yet. I need something that will pass the wife test, so something my wife doesn’t have to think about or be conscious off. BLE “tracking” phones wouldn’t work for her, as she leaves hers in her study at times while working out in the garden… :wink:

Temperature wise being on the edge would not cause rapid flip-flopping. The dining_temperature reports once every minute and outside_temperature is every five minutes.

Additionally the situation in this thread is only part of the equation. I will have a hysteresis of about 4 degrees. I.e. aim is to keep inside temperature between 20 and 24 degrees.

Anyway, with all the help / examples you’ve provided I’ll be able to come up with something… I’ll report back with my end solution or with follow up questions :wink:

Cheers again

1 Like

I’ve just looked at the topic you’re talking about. Could you explain me what is the reason of moving comparison to condition and triggering automation every minute? What’s wrong with the automation that triggers when temperature of any of 2 sensors changes?

For general house occupancy I use the life 360 integration written by Phil (pnbruckner) it allows you you to define a home radius (set same radius on both HA and in the life 360 app) (life 360 has a minimum radius of 250 feet (it’s an American app)) from that I set all sorts of stuff (lights, heating, music …). If you include your wife then you can both see where the other is (on the app) and they also show on the HA map (unless you are at home. (it’s a good/easy first step on such things) but if it’s not for you, don’t worry about it.

I think I’ve got my binary sensor and automation worked out now. See below.

I’ll give it 24 hours to see how it acts when it cools down in the evening and warms up again next morning. If it all works. I’ll mark it as the solution:

binary_sensor:
  - platform: template
    sensors:
      should_clerestory_be_open:
        entity_id: sensor.dining_temperature, sensor.outside_temperature
        value_template: >
          {% set in = states('sensor.dining_temperature')|int %}
          {% set out = states('sensor.outside_temperature')|int %}
          {% set too_cool = in < 20 %}
          {% set too_warm = in > 24 %}
          {% set cooling = out < in %}
          {% set warming = out > in %}
          {{ (too_cool and warming) or (too_warm and cooling) }}
        friendly_name: Should Clerestory Window be Open

automation:
  - alias: Clerestory - Auto - Open
    trigger: 
    - platform: state
      entity_id: binary_sensor.should_clerestory_be_open
      to: 'on'
    condition:
    - condition: state
      entity_id: input_select.clerestory_window
      state: Closed
    action:
    - data: {}
      entity_id: switch.openclerestory
      service: switch.turn_on
  - alias: Clerestory - Auto - Close
    trigger: 
    - platform: state
      entity_id: binary_sensor.should_clerestory_be_open
      to: 'off'
    condition:
    - condition: state
      entity_id: input_select.clerestory_window
      state: Open
    action:
    - service: switch.turn_on
      entity_id: switch.closeclerestory
      data: {}

Of course, I’d be happy for any feedback or potential improvements.

1 Like

Looks pretty good.
Just a couple of questions

  1. Rather than cast to int why not cast to float for slightly better tracking of the values?
  2. Why have the condition in the automations ?
  3. (okay I lied) why not use the flip-flop automation ? You could just as easily use a similar flip-flop in the condition

Regardless that looks like it’ll work, test it and feed back

Well done