Use helper minus 2? Can home assistant do math?

Is there a way I can make a trigger the number that results from a variable, minus two from that variable, without changing that variable? I need a trigger when it’s two degrees below a temperature, to turn on what is a heater, and a trigger for two degrees above a temperature to turn on what is a series of cooling devices. Right now I just stuck the helper name in the value of the condition, and I was wondering what the format for trying to like do math in the value of a condition is?

Heating (2 degrees below set temperature):

trigger:
  - platform: template
    value_template: "{{ states('sensor.your_temperature')|float <= states('input_number.your_variable')|float - 2 }}"

Cooling (2 degrees above set temperature):

trigger:
  - platform: template
    value_template: "{{ states('sensor.your_temperature')|float >= states('input_number.your_variable')|float + 2 }}"

You have to create the input number and then set it to the value you want using Lovelace.

Ya I got all that down but can I put all that stuff into the conditions field instead of trigger? I actually have an issue where sometimes a device turns off or on and then the state never triggers again because it is already a value that is below the threshold and even if it’s say, below 70 at 65 degrees, if it changes to 64 it won’t trigger because it’s already below 64. So even if I were to use these triggers I would need something to periodically check to see if it’s on when it’s supposed to be. Currently I use Temperature Change as trigger and being below the specified value as a condition so it always triggers if it’s below, and sometimes nothing happens because everything is already in the correct state.
Sidenote- ya it taxes the system more, but it’s really whatever because I’ve never experienced issues and it’s the only real thing running on the raspberry pi 4 with a fan running continuously. Running in a docker image on Raspbian (raspberry pi os) on a(/an) ssd. If I don’t do this sometimes other automations screw it up that also change the state of some cooling devices.

Can I make it somehow make two separate helpers that automatically are updated to two degrees above and two degrees below the temperature set and then use those? Idek how I would accomplish that tbh.

Add more triggers. Like when your device turns on and off. Also a Home Assistant start event trigger. In case you miss it while HA is offline. You have to add some conditions to ensure it only runs when the temperature is right…

Sure. You can use the templates as template conditions as well as template triggers.

Sure, create two input numbers.

Write an automation. The trigger is any change of the first input number (a state trigger with no to:). The action is to subtract two from the input number. This has to be run in single mode or the automation will trigger itself.

Repeat for the second input number.

Remove the +/-2 from the templates and update them to use the correct input numbers.

So can I use another input variable instead of the number 2 to make a buffer that I can change the length of? Is that doable? Like just replace the number with input_number.tmptoolow or input_number.tmptoohigh and have a variable buffer for the heating and cooling so I can change it from the dash?

Also thanks, and can i use a climate devices current temperature instead of a sensor? its a tuya device.

So now you want three input numbers. Two input numbers to set the heat/cool temperature trigger points and another input number as the offset (2° in previous examples)?

Sure you can do that.

Yes you can. Use "{{ state_attr('climate.your_climate_device','current_temperature') }}"

ohh thats how you specify the attribute. it was saying something about not being an number because it was on cooling. and so i just stick the input_number.tmptoohigh where the 2 was before? or do i have to do something specific? also thanks again i couldnt figure it out.

I seem to remember you asking for a condition so…

Cooling example:

condition:
  - condition: template
    value_template: "{{ state_attr('climate.your_climate_device','current_temperature')|float >= states('input_number.cool_set_point')|float + states('input_number.offset')|float  }}"

Heating example:

condition:
  - condition: template
    value_template: "{{ state_attr('climate.your_climate_device','current_temperature')|float <= states('input_number.heat_set_point')|float - states('input_number.offset')|float  }}"

Perhaps investigate the generic thermostat?

1 Like

Thought of that but it only does one of heat or cool. Not heat and cool.

Use two? One for each?

Yeah that is a possibility, an annoying one. It really needs updating for both.

The other thing is Thomas appears to already have a climate component that does both and just wants to automate when it gets turned on.

I’ve been down that path. It gets complicated.

1 Like

Oh ya I looked into that actually. And it just seemed easier to try to learn how to make it work in a more Manual way so I can adjust how it works more. Plus I just generally didn’t understand how to get it to work right and thought it would be easier to use an automation. I thought it had both cool and heat and I just couldn’t figure it out.

So something is wrong and It says I need to define something… How do I like, define input_number.tmptoolow and input_number.tmptoohigh? It exists as a helper so I think I need to so something in the Template. I’ll post the yamls and log. Do I need to remove the like quote things around the helper/variable?

alias: ashley/kitchen ac template -var bth
description: Requires both tuya and ecowitt sensors to trigger with 1 degree buffer
trigger:

  • platform: state
    entity_id: sensor.temperature_2
  • platform: state
    entity_id: sensor.ashley_h_t_temperature
  • platform: state
    entity_id: climate.02720007bcddc2a51487
    condition:
  • condition: template
    value_template: >-
    {{ states(‘sensor.temperature_2’)|float <=
    states(‘input_number.kitchen_ac_temp’)|float - ‘input_number.tmptoolow’ }}
  • condition: template
    value_template: >-
    {{ states(‘climate.02720007bcddc2a51487’,‘current_temperature’)|float <=
    states(‘input_number.kitchen_ac_temp’)|float - ‘input_number.tmptoolow’ }}
    action:
  • type: turn_off
    device_id: 9c72c9a4d9ba8cda7e44828ad2c63cf9
    entity_id: switch.main_room_ac
    domain: switch
  • type: turn_off
    device_id: 1bcdd611bd3e15f1fdf3c3fe0369592e
    entity_id: switch.main_room_dehumidifier
    domain: switch
  • type: turn_off
    device_id: 7b42f7c56de0b17227502e28fe71cdce
    entity_id: switch.kitchen_ac_2
    domain: switch
    mode: single

alias: ashley/kitchen ac template +var bth
description: ‘’
trigger:

  • platform: state
    entity_id: sensor.temperature_2
  • platform: state
    entity_id: sensor.ashley_h_t_temperature
  • platform: state
    entity_id: climate.02720007bcddc2a51487
    condition:
  • condition: template
    value_template: >-
    {{ states(‘sensor.temperature_2’)|float >=
    states(‘input_number.kitchen_ac_temp’)|float + ‘input_number.tmptoohigh’
    }}
  • condition: template
    value_template: >-
    {{ states(‘climate.02720007bcddc2a51487’,‘current_temperature’)|float >=
    states(‘input_number.kitchen_ac_temp’)|float + ‘input_number.tmptoohigh’
    }}
    action:
  • type: turn_on
    device_id: 9c72c9a4d9ba8cda7e44828ad2c63cf9
    entity_id: switch.main_room_ac
    domain: switch
  • type: turn_on
    device_id: 1bcdd611bd3e15f1fdf3c3fe0369592e
    entity_id: switch.main_room_dehumidifier
    domain: switch
  • type: turn_on
    device_id: 7b42f7c56de0b17227502e28fe71cdce
    entity_id: switch.kitchen_ac_2
    domain: switch
    mode: single

Logger: homeassistant.components.automation
Source: components/automation/init.py:728
Integration: Automation (documentation, issues)
First occurred: 2:14:32 PM (543 occurrences)
Last logged: 11:00:19 PM

Error evaluating condition in ‘ashley/kitchen ac template +1 bth’: In ‘condition’ (item 2 of 2): In ‘template’ condition: TypeError: call() takes 2 positional arguments but 3 were given
Error evaluating condition in ‘ashley/kitchen ac template +var bth (Duplicate)’: In ‘condition’ (item 1 of 2): In ‘template’ condition: UndefinedError: ‘input_number’ is undefined In ‘condition’ (item 2 of 2): In ‘template’ condition: TypeError: call() takes 2 positional arguments but 3 were given
Error evaluating condition in ‘ashley/kitchen ac template +var bth’: In ‘condition’ (item 1 of 2): In ‘template’ condition: UndefinedError: ‘input_number’ is undefined In ‘condition’ (item 2 of 2): In ‘template’ condition: TypeError: call() takes 2 positional arguments but 3 were given
Error evaluating condition in ‘ashley/kitchen ac template -var bth’: In ‘condition’ (item 1 of 2): In ‘template’ condition: TypeError: unsupported operand type(s) for -: ‘float’ and ‘str’ In ‘condition’ (item 2 of 2): In ‘template’ condition: TypeError: call() takes 2 positional arguments but 3 were given
Error evaluating condition in ‘ashley/kitchen ac template +var bth’: In ‘condition’ (item 1 of 2): In ‘template’ condition: TypeError: unsupported operand type(s) for +: ‘float’ and ‘str’ In ‘condition’ (item 2 of 2): In ‘template’ condition: TypeError: call() takes 2 positional arguments but 3 were given

Please have a read of point 11 here, then edit your post.

Look at how the first two entity values are returned using the states() function and |float filter. You need to do the same thing to the last input_number in this template.

value_template: >-
  {{ states('sensor.temperature_2')|float <= states('input_number.kitchen_ac_temp')|float - 'input_number.tmptoolow' }}

Same for the high temperature template.

condition: template
value_template: >-
  {{ states('climate.02720007bcddc2a51487','current_temperature')|float >=
  states('input_number.kitchen_ac_temp)|float +
  states('input_number.tmptoohigh')|float  }}
condition: template
value_template: >-
  {{ states('sensor.temperature_2')|float >=
  states('input_number.kitchen_ac_temp)|float +
  states('input_number.tmptoohigh')|float  }}

Like that? Or no? It’s giving me a syntax error.

Look how the forum has highlighted quoted parts in red.

  {{ states('climate.02720007bcddc2a51487','current_temperature')|float >=
  states('input_number.kitchen_ac_temp)|float +
  states('input_number.tmptoohigh')|float  }}

You missed a single quote at the end of kitchen ac temp. Should look like this:

  {{ states('climate.02720007bcddc2a51487','current_temperature')|float >=
  states('input_number.kitchen_ac_temp')|float +
  states('input_number.tmptoohigh')|float  }}