Temperature based switch control

Hi!

I’ve been trying to make my first custom automation with temperature based switch control.
I.e. if temperature drops below a certain value, i would like to turn a switch on - and if it goes above the same number it should turn the switch off.

There are a lot of examples here, and it seems fairly simple - but most of them seem to be slightly deprecated syntax wise, and I can’t seem to get them to run.

All i get when i reload my automations is this error:
“Invalid config for [automation]: [service] is an invalid option for [automation]. Check: automation->service. (See /config/configuration.yaml, line 9).”

Can you help me out? This is what i got so far:
(Like to skip the trigger above/below value, but that seems to be mandatory now as well)

- id: '1638214495338'
  alias: Pump Heater
  description: ''
  trigger:
    - platform: numeric_state
      entity_id: sensor.temperature_2
      below: 100
  condition: []
  action:
    - service: >
        {% set temp = states('sensor.temperature_2') | int %}
        {% if temp <= 12 %}
          switch.turn_on
        {% else %}
          switch.turn_off
        {% endif %}
      device_id: 36dcb46fe4a1e4c823a379bdc26a51ae
      entity_id: switch.heater_outlet
      domain: switch

Thanks!

Got it to work. Don’t really know why, but i removed the conditional and altered the yml to.


- id: '001'
  alias: Pump Heater
  description: ''
  trigger:
    - platform: numeric_state
      entity_id: sensor.temperature_2
      below: 100
  action:
    service: >
      {% if states('sensor.temperature_2') | float <= 7.5 %}
        switch.turn_on
      {% else %}
        switch.turn_off
      {% endif %}
    entity_id: switch.heater_outlet

I think this will execute every minute as sensor.temperature_2 is set conditionally at 7.5 but triggers at 100. I think you could save some resources by going

below: 20

Just an optimization.

No. There is no time pattern trigger. It has a numeric_state trigger that will only trigger when the state changes from above 100° to below 100°. Changing from 90 to 80 or 50 to 60 for example will not trigger this automation.

The way the automation is written it is unlikely to ever trigger.

This whole automation could be replaced with a generic thermostat.

Thanks for the clarification @tom_I. Agree the generic thermostat is the better tool.

Got it. I thought it triggered on every temp change below 100, i.e. on every temp change from the sensor since the number was so high.

I completely missed the generic thermostat option. I’ll give that a try.

Thanks a lot!

Nope. That is a common misconception.

You can trigger on every change (and every attribute change) like this:

  trigger:
    - platform: state
      entity_id: sensor.temperature_2
  action:

Thank you so much for your help!

This is what i ended up with, before switching over to using the generic thermostat instead - which is indeed a much better solution, especially with the thermostat dashboard card.

Figure it’s best to post the solution here if someone else is having the same initial issues as me:

- id: '001'
  alias: Pump Heater
  description: ''
  trigger:
    - platform: state
      entity_id: sensor.temperature_2
  action:
    service: >
      {% if states('sensor.temperature_2') | float < 8.0 %}
        switch.turn_on
      {% elif states('sensor.temperature_2') | float > 12.0 %} 
        switch.turn_off
      {% endif %}
    entity_id: switch.heater_outlet

This was a good starter tutorial for the generic thermostat btw:

He forgets to include the config file in configuration.yaml in the video though:

climate: !include climate/generic_thermostat.yaml
1 Like

I’m trying to do something very similar here. I have a fan controlled by a switch, and a temp sensor in that room, and I’m trying to basically say:
“if the temp in the room has been above 72 for more than 5 minutes, turn the fan on”, and the functionality I’m looking for is for HA to turn the fan off again once the temp is below 72.
It looks like we still can’t put the ‘for x amount of time’ stanza inside an if statement. This is the automation I have right now for it, but I could totally use a second set of eyes if anyones willing to see if they can spot any issues with the logic.

alias: Autopilot kitchen fan
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.robodonk3_inside_temp
    for:
      hours: 0
      minutes: 5
      seconds: 0
    above: 72
    id: temp high
  - platform: numeric_state
    entity_id:
      - sensor.robodonk3_inside_temp
    below: 71
    id: temp low
condition: []
action:
  - if:
      - condition: trigger
        id: temp low
    then:
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.kitchen_fan
  - if:
      - condition: trigger
        id: temp high
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.kitchen_fan
mode: single

As suggested above, you could use a generic thermostat and it would just work. You could also use scheduler if you wished to change the temperature throughout the day.

1 Like