Help with templates and automations

Hey guys,

I’m trying to control the heater (turn on and off) if the temperature is below 18C or above 22C and if someone is at home.

Any help? Thank you

#Climate
- action:
  - data:
    service_template: >
      {% if is_state('sensor.ulisses', 'home') or is_state('sensor.daniela', 'home') %}
        {% if states('sensor.pws_temp_c' | float) < 18 %} 
      - turn_on:
        service: switch.turn_on
        data:
          entity_id: switch.heater_switch
        {% elif states('sensor.pws_temp_c' | float) > 22 %} 
      - turn_off:
        service: switch.turn_off
        data:
          entity_id: switch.heater_switch
        {% else %}
      - turn_on:
        service: switch.turn_on
        data:
          entity_id: switch.heater_switch
        {% endif %}
      {% else %}
      - turn_off:
        service: switch.turn_off
        data:
          entity_id: switch.heater_switch
      {% endif %}
  alias: 'Climate'
  trigger:
  - above: '22'
    entity_id: sensor.pws_temp_c
    platform: numeric_state
  - below: '18'
    entity_id: sensor.pws_temp_c
    platform: numeric_state

Why not use the generic thermostat component if your heater is a switch and a temp-sensor:

climate:    
  - platform: generic_thermostat
    name: Room
    heater: switch.heater_switch
    target_sensor: sensor.pws_temp_c
    min_temp: 18
    max_temp:22

And then set climate.room to “heat” when someone is home?

2 Likes

Thank you @vegard!

1 Like