Automation that has on below a certain temp and off above a certain temp

I have been making separate automations for on and off. For instance, If I want a switch to turn on below a certain temp I have one automation. If I want it to turn off above a certain temp I have another automation. The trigger is always Time Pattern /15 minutes so that it can run at any 15 minute period the temp threshold has been met.

How do I create a single automation with both on and off action based on the temperature of a sensor with the trigger set to check the temp every 15 minutes? I have a space heater connected to the switch which I have named as switch.zooz. The low temp to turn on the switch is 60 and the high temp to turn off the switch is 70. The temp sensor is sensor.office_temp.

Sound like you want a thermostat

How do I hook up a switch to a thermostat?

I hope it works, I can’t test it out… If it doesn’t, let me know the logs. You don’t need the 15 minutes polling, it’ll check every time your sensor is updated

- id: whatever
  alias: whatever
  trigger:
  - platform: numeric_state
    entity_id: sensor.office_temp
  action:
  - service_template: >
      {% if states.sensor.office_temp.state|int <= 60 %}
        homeassistant.turn_on
      {% elif states.sensor.office_temp.state|int >= 70 %}
        homeassistant.turn_off
      {% endif %}
    entity_id: switch.zooz
1 Like

To have a better response that follows the advice in the docs:


I suggest modifying the template to (I also added an extra elif statement and an else statement to cover the temps between 60 and 70 - otherwise, no service would be specified and you’d get an error). Finally, I added set statements so you only get those states once (saving a micro-second or something so you don’t call the same thing twice):

- id: whatever
  alias: whatever
  trigger:
  - platform: numeric_state
    entity_id: sensor.office_temp
  action:
  - service_template: >
      {% set temp = states('sensor.office_temp') | int %}
      {% set switch = states('switch.zooz') %}
      {% if temp <= 60 %}
        homeassistant.turn_on
      {% elif temp >= 70 %}
        homeassistant.turn_off
      {% elif  'off' in switch %}
        homeassistant.turn_off
      {% else %}
        homeassistant.turn_on
      {% endif %}
    entity_id: switch.zooz

I added the third elif statement to cover the times when the temp is between 60 and 70. The only way to get this far is if the temp is between 60 and 70, so I don’t need to check for that. If the switch is already off, that means you are cooling off from 70 down to 60, so leave it off. The else statement covers for the only remaining case where you are heating up to 70, so that keeps the switch on.

1 Like