Math in service_template

This is my boiler control automation. The codes works well but if i add +/-3 math it doesnt work. No compile error but relay doesnt work. I want to add extra tolerance for set temperature.

alias: Boiler
trigger:
  - platform: state
    entity_id: input_number.settemp
action:
  - service_template: >
      {% if states("sensor.livingroom_temperature") >=
      states("input_number.settemp") + 3 %}
        switch.turn_on
      {% endif %}  {% if states("sensor.livingroom_temperature") <=
      states("input_number.settemp") - 3 %}
        switch.turn_off
      {% endif %}
    entity_id: switch.kombi_relay

States are always strings. To use them in mathematical expressions you need to use a filter to make them into a number. Usually either float or int

So for example your first if statement needs to be this:

{% if states("sensor.livingroom_temperature") | float >=
      states("input_number.settemp") | float + 3 %}
1 Like

and you don’t use “service_template” anymore. just use “service”.

Thank you very much. Problem solved… :heart_eyes:

Why not use the Generic Thermostat integration?

Set the hot_tolerance and cold_tolerance options to 3 and it will satisfy your requirements for a +/-3 degree deadband.

1 Like

Thank you for your reply. I am very new with HomeAssistant and trying to discover it’s functions. I’ll try your advice as soon as possible… :upside_down_face:

I got this message. How can i start for adding the Generic Theremostat?
Thanks…

image

The instructions are in the displayed message: "add this device by adding it to your configuration.yaml". Refer to the documentation link I posted above for more information.

For your case, the configuration of the Generic Thermostat will look something like this:

climate:
  - platform: generic_thermostat
    name: Living Room
    heater: switch.kombi_relay
    target_sensor: sensor.livingroom_temperature
    min_temp: 15
    max_temp: 25
    ac_mode: false
    target_temp: 21
    cold_tolerance: 3
    hot_tolerance: 3
    min_cycle_duration:
      seconds: 60
    keep_alive:
      minutes: 3
    initial_hvac_mode: "off"
    away_temp: 16

Ok i added the codes to the configuration.yaml file but now? There is no card about Generic Thermostat for adding to the dashboard. Sorry for my dumb question but i really new in this system… :frowning_face:

Just to confirm, did you restart Home Assistant after you made the changes to the configuration.yaml file?

Yes i did…

Then you should have a new entity called climate.living_room which you can add to a Dashboard using a Thermostat card.

Thank you for your help. I did it… :star_struck: