Keep temperature above outside temperature

Hello folks,

I would be very grateful if someone could help me with automation script that would do following:

I would like to have one switch on lovelace panel what will activate this automation rule called “Away temperature”.
When activated, this rule should then control my heater so that it will switch it on when temperature difference between inside and outside is below lets say 3 degrees. And stop heating when temperature difference will increase to 5 degrees.

I have following entities:
outside temp: sensor.sonoff_1000bed77c_temperature
inside temp: sensor.mokki_patteri_mh_si7021_temperature
heater switch: switch.mokki_patteri_mh

And I also would like to be able control heater switch manually on top of automation. So that I could switch heater on even this rule is activated.

Many thanks in advance for any replies!

Let’s assume you create an input_boolean to indicate when away temperature should run, and it’s called input_boolean.enable_away_temperature. To make this an “easy” single automation, we’ll use a choose block.

- alias: Away temperature
  trigger:
  # Any time the temperature inside or outside changes, check the conditions
  - platform: state
    entity_id: sensor.outside_temperature
  - platform: state
    entity_id: sensor.inside_temperature
  # Also check on HA startup, or when you reload automations
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded
  condition:
  # Only run when enabled
  - condition: state
    entity_id: input_boolean.enable_away_temperature
    state: 'on'
  # Only run if it's below 10 degrees outside
  - condition: numeric_state
    entity_id: sensor.outside_temperature
    below: 10
  action:
  - choose:
    - conditions:
      # The heater must be off
      - condition: state
        entity_id: switch.heater
        state: 'off'
      # And inside is less than three degrees warmer than inside
      - condition: template
        value_template: "{{ states('sensor.inside_temperature')|float - states('sensor.outside_temperature')|float < 3}}"
      sequence:
      # Turn on the heater
      - service: switch.turn_on
        entity_id: switch.heater
    - conditions:
      # The heater is on
      - condition: state
        entity_id: switch.heater
        state: 'on'
      # It's more than 5 degrees warmer inside than out
      - condition: template
        value_template: "{{ states('sensor.inside_temperature')|float - states('sensor.outside_temperature')|float > 5}}"
      sequence:
      # Turn off the heater
      - service: switch.turn_off
        entity_id: switch.heater

Of course, if it’s -5 outside, this would let the house get below freezing. You could add some more logic to the templates to handle that. For example

        value_template: "{{ (states('sensor.inside_temperature')|float - states('sensor.outside_temperature')|float < 3) or (states('sensor.inside_temperature')|float < 5) }}"

and

        value_template: "{{ (states('sensor.inside_temperature')|float - states('sensor.outside_temperature')|float > 5) and (states('sensor.inside_temperature')|float > 5) }}"

That will ensure that the inside temperature never gets below 5 degrees.

1 Like

wow, that was more than I expected. Avoiding below zero was also on my list but I though I can figure it out after some help on this. But great that you red my mind on that :slight_smile:

Thanks a lot, I will try it soon and let you know.

I just realised I missed a bit, I should have added the state of the boolean to the initial logic

- alias: Away temperature
  trigger:
  # Any time the temperature inside or outside changes, check the conditions
  - platform: state
    entity_id: sensor.outside_temperature
  - platform: state
    entity_id: sensor.inside_temperature
  # Also check on HA startup, or when you reload automations
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded
 # Run when turned on
  - platform: state
    entity_id: input_boolean.enable_away_temperature
    to: 'on'
  condition:
  # Only run when enabled
  - condition: state
    entity_id: input_boolean.enable_away_temperature
    state: 'on'
  # Only run if it's below 10 degrees outside
  - condition: numeric_state
    entity_id: sensor.outside_temperature
    below: 10
  action:
  - choose:
    - conditions:
      # The heater must be off
      - condition: state
        entity_id: switch.heater
        state: 'off'
      # And inside is less than three degrees warmer than inside, or it's less than 5 degrees inside
      - condition: template
        value_template: "{{ (states('sensor.inside_temperature')|float - states('sensor.outside_temperature')|float < 3) or (states('sensor.inside_temperature')|float < 5) }}"
      sequence:
      # Turn on the heater
      - service: switch.turn_on
        entity_id: switch.heater
    - conditions:
      # The heater is on
      - condition: state
        entity_id: switch.heater
        state: 'on'
      # It's more than 5 degrees warmer inside than out, and above 5 degrees inside
      - condition: template
        value_template: "{{ (states('sensor.inside_temperature')|float - states('sensor.outside_temperature')|float > 5) and (states('sensor.inside_temperature')|float > 5) }}"
      sequence:
      # Turn off the heater
      - service: switch.turn_off
        entity_id: switch.heater

This will still leave the heating on when you turn off the boolean, so maybe this instead:

- alias: Away temperature
  trigger:
  # Any time the temperature inside or outside changes, check the conditions
  - platform: state
    entity_id: sensor.outside_temperature
  - platform: state
    entity_id: sensor.inside_temperature
  # Also check on HA startup, or when you reload automations
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded
 # Run when turned on or off
  - platform: state
    entity_id: input_boolean.enable_away_temperature
  condition:
  - condition: or
    conditions:
    # Only run when enabled
    - condition: state
      entity_id: input_boolean.enable_away_temperature
      state: 'on'
    # Or if we've just been turned off
    - condition: template
      value_template: "{{ trigger.to_state and (trigger.to_state.entity_id == 'input_boolean.enable_away_temperature') }}"
  # Only run if it's below 10 degrees outside
  - condition: numeric_state
    entity_id: sensor.outside_temperature
    below: 10
  action:
  - choose:
    - conditions:
      # Only run when enabled
      - condition: state
        entity_id: input_boolean.enable_away_temperature
        state: 'on'
      # The heater must be off
      - condition: state
        entity_id: switch.heater
        state: 'off'
      # And inside is less than three degrees warmer than inside, or below 5 degrees inside
      - condition: template
        value_template: "{{ (states('sensor.inside_temperature')|float - states('sensor.outside_temperature')|float < 3) or (states('sensor.inside_temperature')|float < 5) }}"
      sequence:
      # Turn on the heater
      - service: switch.turn_on
        entity_id: switch.heater
    - conditions:
      # The heater is on
      - condition: state
        entity_id: switch.heater
        state: 'on'
      - conditions: or
        conditions:
        # We've just been turned off
        - condition: template
          value_template: "{{ trigger.to_state and (trigger.to_state.entity_id == 'input_boolean.enable_away_temperature') }}"
        # Or enabled and it's warm enough
        - condition: and
          conditions:
          # Only run when enabled
          - condition: state
            entity_id: input_boolean.enable_away_temperature
            state: 'on'
          # It's more than 5 degrees warmer inside than out, and more than 5 degrees inside
          - condition: template
            value_template: "{{ (states('sensor.inside_temperature')|float - states('sensor.outside_temperature')|float > 5) and (states('sensor.inside_temperature')|float > 5) }}"
      sequence:
      # Turn off the heater
      - service: switch.turn_off
        entity_id: switch.heater
1 Like

This works like a charm when tested it. Really appreciate your complete answer with working example!

Thanks!

1 Like

Hi again,

is there possibility to make switch status persistent so that it remember status after restart?

Thanks!