Automate heater with conditions

Wondering if someone can help me with this automation?

What I want is to have a switch turn on between 8:30pm and 7:00am, but only if the average household temp is below 20 deg.

What I have:

alias: Heater On
description: "Turn on heater if temp is below 20"
trigger:
  - platform: time_pattern
    minutes: "10"
condition:
  - condition: and
    conditions:
      - condition: time
        before: "07:00:00"
        after: "20:30:00"
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
          - sat
          - sun
      - condition: numeric_state
        entity_id: sensor.average_house_temp
        below: 20
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.heater_switch
mode: single

Problem: After 8:30, regardless of the temp the heater won’t turn on

Any help would be appreciated.

Cheers
smelly grunger

The following automation will turn on the switch when the temperature decreases below 20 and the current time is between 20:30 and 07:00.

If the temperature falls below 20 before 20:30 then nothing happens. However the automation is designed to trigger at 20:30 and that’s when its condition will confirm the temperature is already below 20 and proceed to turn on the switch.

alias: Heater On
description: "Turn on heater if temp is below 20"
trigger:
  - platform: numeric_state
    entity_id: sensor.average_house_temp
    below: 20
  - platform: time
    at: "20:30:00"
condition:
  - condition: time
    before: "07:00:00"
    after: "20:30:00"
  - condition: numeric_state
    entity_id: sensor.average_house_temp
    below: 20
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.heater_switch
mode: single

To make the automation just a little bit smarter, you can do this:

alias: Heater On
description: "Turn on heater if temp is below 20"
trigger:
  - platform: numeric_state
    entity_id: sensor.average_house_temp
    below: 20
  - platform: time
    at: "20:30:00"
  - platform: homeassistant
    event: start
condition:
  - condition: time
    before: "07:00:00"
    after: "20:30:00"
  - condition: numeric_state
    entity_id: sensor.average_house_temp
    below: 20
  - condition: state
    entity_id: switch.heater_switch
    state: 'off'
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.heater_switch
mode: single

This version mitigates the situation where Home Assistant is restarted at the moment when the temperature decreases below 20 (thereby missing the event) or at 20:30.

It also turns on the switch only if the switch is currently off (avoiding the needless step of commanding the switch to turn on when it’s already on).

Lastly, I suggest you consider using the Generic Thermostat integration to control the heater. Together with the Schedule integration, you can create an automation that provides even more flexibility and control.


EDIT

Correction. Changed second example’s State Condition to off.

Thanks mate,

They didn’t seem to work but led me on the right path and I got it working with the following:
ps, I think your second automation didn’t work because you had the switch condition as on by mistake?

alias: Heater On
description: Turn on heater if temp is below 20
trigger:
  - platform: time_pattern
    minutes: "10"
  - platform: numeric_state
    entity_id: sensor.average_house_temp
    below: 21
  - platform: homeassistant
    event: start
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: switch.heater_switch
        state: "off"
      - condition: time
        before: "06:00:00"
        after: "20:30:00"
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
          - sat
          - sun
      - condition: numeric_state
        entity_id: sensor.average_house_temp
        below: 21
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.heater_switch
mode: single

The first one does in fact work; it’s based on a well-established design pattern (and is employed by many examples in the community forum). It’s your testing procedure that’s probably suspect.

A Numeric State Trigger only triggers at the moment the value crosses the threshold. If the value (temperature) is already below the threshold (20), it won’t trigger. That’s why there’s a Time Trigger present to check the temperature at the start of the desired time period (20:30). If the automation was created/tested after 20:30 and the temperature is already below the threshold, it won’t do anything until the following day. However that’s just due to flawed initial test conditions; it’ll work when the Time Trigger fires at 20:30 (the next day).

I agree the second one contains a mistake I made. Although I explained its operation correctly, I entered on instead of off in its State Condition. I’ve corrected the second example.

The example you posted contains a Time Pattern Trigger. It’s one of the most misunderstood and misused triggers by novices who believe the automation must be forced to perform some form of periodic polling. There are valid use-cases for Time Pattern Trigger but this isn’t one of them. Home Assistant is event-based and all of the needed events are available to make this automation work efficiently without a Time Pattern Trigger.

FWIW, there’s no need to explicitly indicate condition: and because logical ANDing is the default (i.e. it’s implicit). Similarly, there’s no need to list all of the weekdays in a Time Condition because that’s the default.

For future reference, it’s not the custom of this community for a user to mark their own post as the Solution if it contains a majority of the work provided by another user. It’s the other user’s work that forms the Solution.

Here’s the second automation I had suggested employing your new time (06:00) and temperature value (21).

alias: Heater On
description: Turn on heater if temp is below 21
trigger:
  - platform: time
    at: "20:30:00"
  - platform: numeric_state
    entity_id: sensor.average_house_temp
    below: 21
  - platform: homeassistant
    event: start
condition:
  - condition: state
    entity_id: switch.heater_switch
    state: "off"
  - condition: time
    before: "06:00:00"
    after: "20:30:00"
  - condition: numeric_state
    entity_id: sensor.average_house_temp
    below: 21
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.heater_switch
mode: single
1 Like

No worries mate, sorry if I offended you, was not my intention.
Appreciate you taking the time to explain it all to me.

I will do some further testing with the last example and see how I go.

No worries; no offense taken. Good luck with the test and let me know if you need help to fine-tune it or to answer any other questions.

Any progress to report?

BTW, another user recently faced the same situation as you. They wanted to turn on a switch based on a sensor’s value crossing a threshold but only within a specific time period. It’s a common challenge with a common solution: use a Numeric State Trigger and Time Trigger with matching Numeric State Condition and Time Condition. The user reported their automation is now working correctly.

Yep, works a treat thanks again

1 Like