Condition for one temperature being lower than another?

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

It’s going to be a case by case basis obviously depending on what you are doing. But I’ve used this to dynamically adjust my aircon for example. I want to make changes when temperature is above, and then still above; just comparing one to the other would only fire once, until it falls below and then again rises above, not much use in my case. I also dont trigger every minute, its only every 10, but same point.

Just firing once, might be exactly what you want, again case by case basis.

  1. Yeah, I guess I could cast to float. Would probably make a small difference on the “warm” end of the scale with 24.1 to 24.9 (one digit accuracy on temperature measurements) counting as “too_warm” where it isn’t right now.

  2. I don’t want the electric window winders on our clerestory windows to be actuated when they are already in the desired position. They are finicky and not cheap. Already had to replace two when I over actuated them using manual buttons in the past.

  3. Not exactly sure what you mean by the flip-flop automation… if you are talking about service_template: switch.turn_{{trigger.to_state.state}} in your earlier example. That won’t work in my case. I’ve got two separate, physical switches, one to open and another one to close the windows.

Our window winders have three cables/connections; one neutral connection, one active connection to actuate for opening, and another active connection to actuate for closing. So I installed two Sonoff Minis to be able to active the corresponding active connection for the winders.
In other words if you look at the entity_ids in my automation action, you’ll see I have different switches to turn on :slight_smile:

from the code it just turns on a switch. if you use the same (or similar code without any acrobatic in the action section), you also just turn on something, is that correct?

yeah. could you post your config?

yes, it’s still the same. I’m trying to get what is the advantage of relying on time and then comparing temperatures rather than monitoring temperatures’ changes directly.
I understand that it does not suit you when the automation is executed only once and all I want to know is why you need it and what you do in your automation that suits your needs.

Without posting something you are likely to not understand without context (as its perhaps my most complex single automation), I’m basically adjusting the aircon set temperature according to the current temperature.

My aircon temperature sensor is poorly located (way up in the duct), and on cold days it sees the room temperature as much lower than it really is, and on hot days, much much hotter than it really is. So you can’t just set the temperature to 24C for example, as it will just run until the room temp is <20C on a hot day, or 26+C on a cold day.

My automation gradually tunes the set temp on the aircon to keep the rooms at a temperature much closer to the actual ‘desirable temperature’. A bit of a sh#t way of fixing a sh#t aircon in a rental I have no control over.

1 Like

ok, got it.
the question is - how (roughly) do you “tune” your temperature provided the temperatures reported hasn’t changed and it’s just another 10 mins interval? something like “It’s already running for N minutes, it should get colder soon so I’ll tweak (increase) the target temp”?

Ouch ! Understood

Yeah, I saw that, and sometimes you can’t use the same automation to both turn on / off (in this case we’d turn A on or turn B on) but it’s not super critical and I sometimes spend ages getting “this just right” to absolutely no benefit at all.

I’m impressed with how you’ve picked this up and the wrinkles of implementation you have already got thoughts on, you wouldn’t happen to be an engineer would you ?

Good luck with it

Lol… No. I have an IT background though. Been ‘playing’ with computers since grade 6 (am getting close-ish to retirement)
‘Gave up’ professional IT work and am now farming and driving a local school bus. However can’t completely give up IT… :slight_smile:
Just doing it for myself now. Not having to deal with a project manager is heaven!

Ah … You mean “used to having to get things to work even if it takes a really big hammer”
:rofl:

Yeah and learning that good enough is sometime all that’s needed :wink:
Pretty sometimes doesn’t make a difference. There are some bits of fencing that I definitely wouldn’t call “professional looking” but even these bits of fencing keep animals in and out as needed :slight_smile:

Just had a thought, if they break with overuse.
You might want to rotate duties.
I’m assuming you have multiple windows with individual controls ?
So day 1 window A, day 2 window B, day 3 window C, day 4 back to window A etc.
Spread the wear
But that may mean they all fail in the same week
:rofl:

hay bro
you automation look a bit wrong

    - below: '24'
      condition: numeric_state
      entity_id: sensor.outside_temperature

you have a condition: numeric_state and yet you have below: ‘24’ the 24 is a string as you have a quote around it

try

    - below: 24
      condition: numeric_state
      entity_id: sensor.outside_temperature

Hi StePhan

The automation has evolved since the initial post. I am currently using the automation as per reply at: https://community.home-assistant.io/t/condition-for-one-temperature-being-lower-than-another/185718/19?u=faramir

But thanks for checking it out and giving feedback :slight_smile:

Interesting idea… right now they are all ganged up, actuating all at the same time off the same power. They are quite high up and a bit of a pain to get to, so I’ll probably leave them alone as long as they work :wink: