Trigger that compares numbers

Good morning. I would need help with an automation that compares the external temperature with the value set in a “slider” entity.

platform: numeric_state
entity_id: climate.bagni
attribute: temperature
value_template: |2-
        value_template: "{{ states('input_number.slider1')|float >
        state_attr('weather.casa', 'temperature')|float }}" 
        above: 2

This is my trigger and testing it in developer mode works but when I try to implement it the automation is not saved and this error comes out

Message malformed: must contain at least one of below, above.

What am I doing wrong?

You used the value_template option incorrectly. In fact, you used it twice (incorrectly).

You can only have one value_template option. It’s for computing the value that will be used by the above (and/or below) option. You didn’t do that; you used it to perform a comparison.

You may be better off using a Template Trigger.


If you explain what you are trying to do, I can help you achieve it. As it stands, it’s unclear to me how you want to compare the thermostat’s temperature with the input_number and external temperature and the threshold value of 2.

thank you for your willingness to help me. I would like to obtain a “true or false” result by comparing the value set in “slider” and the outside temperature.
I need it to be able to turn on a stove when the outside temperature goes below 2 degrees c ° from the value set in “slider”

  trigger:
  - platform: template
    value_template: "{{ state_attr('weather.casa', 'temperature')|float < states('input_number.slider1')|float - 2 }}" 

What’s unclear to me is where does the temperature attribute of climate.bagni fit into all of this? You used it in the Numeric State Trigger but your requirements only mention comparing the input_number and external temperature.

Yes you are right. I also tried to get a number for the thermostat set point but it is far beyond my skills. The typo in evidence remained.

I’m sorry if I still bother you. Your suggestion works but I can’t (just can’t) figure out how to extract the value “true” “false” to execute the trigger that will activate my user.
I have read the HA tutorials but to no avail.
I was thinking of creating a boolean input where to see the state but this road has also closed.

Glad to hear it works.

?

You will have to provide more details about what you want to do because this makes no sense to me:

Extract a boolean value to execute a trigger to activate a user.

alias: 'Heat'
description: 
trigger:
  - platform: template
    value_template: "{{ state_attr('weather.casa', 'temperature')|float < states('input_number.slider1')|float - 2 }}" 
    id: input_boolean.termometer
condition:
  - condition: state
    entity_id: input_boolean.termometer
    state: 'true'
action:
  - service: switch.turn_on
    target:
      entity_id: switch.minimal temperature
mode: single

This is the code that should be executed.
I created a boolean input which should receive true / false from value template.
I understand that the code is garbage but I am trying them all.
I just have to turn on and off a miserable stuff with the result of the template you wrote me and that works with the simulation.

?

If you mean the entity input_boolean.termometer will be assigned true or false by the value_template in the automation’s Template Trigger then, no, it will not. There’s nothing in Home Assistant that works like that.

If input_boolean.termometer is an existing entity, its state value will be either on or off (not true/false).

The following automation can work but it’s still unclear to me if that’s what you want.

alias: 'Heat'
description: 
trigger:
  - platform: template
    value_template: "{{ state_attr('weather.casa', 'temperature')|float < states('input_number.slider1')|float - 2 }}" 
condition:
  - condition: state
    entity_id: input_boolean.termometer
    state: 'on'
action:
  - service: switch.turn_on
    target:
      entity_id: switch.minimal_temperature
mode: single

ok, be patient, I try to explain myself. The trigger works but the problem is that I don’t know how to connect the true \ false state with the on \ off value of the boolean input. Basically I don’t know how to make them meet.
All entities are present and visible.

Sorry but that still makes no sense. There’s simply no connection between what the Template Trigger produces and the input_boolean’s state. They don’t “meet”.

Let’s try another approach: explain what you want the automation to achieve in simple terms. For example, here’s what the one posted above does:

  • When the external temperature is at least two degrees less than an input_number AND an input_boolean is on, it turns on a switch.

Basically I just want that from the template you provided me,

value_template: "{{ state_attr('weather.casa', 'temperature')|float < states('input_number.slider1')|float - 2 }}" 

the condition of true can make me the turn_on of the switch

entity_id: switch.minimal temperature

and that turns on the stove and vice versa

I think half the forum is laughing at my confusion.

That’s what the example I posted does except it also tests if input_boolean.termometer is on. If you don’t want that additional test then remove the condition.

alias: 'Heat'
description: 
trigger:
  - platform: template
    value_template: "{{ state_attr('weather.casa', 'temperature')|float < states('input_number.slider1')|float - 2 }}" 
condition: []
action:
  - service: switch.turn_on
    target:
      entity_id: switch.minimal_temperature
mode: single

You should know that you cannot have a space in the entity_id

entity_id: switch.minimal temperature
                         ^
                         |
  You cannot have a space here

Check the name of the switch in Developer Tools > States. It probably has an underscore character between the two words (not a space).

1 Like

I solved it after several tests and thanks also to your code. In practice I was wrong to write the boolean entity and I had to restart the raspberry …
I understood that the trigger is activated when the model is true (read on the tutorials) and based on this I have also regularized the Boolean entity.
I just need to do the automation that turns off the heater and I’m done.

1 Like

I don’t know if it is correct to continue here or to open another post. When in doubt, I raise the question here.
The thermostat is working properly.

alias: temperature_up
sequence:
  - service: climate.set_temperature
    target:
      entity_id: climate.bagni
    data_template:
      temperature: '{{ state_attr(''climate.bagni'', ''temperature'') + value | float }}'
mode: single

I have a button that increases the temperature set

type: button
tap_action:
  action: call-service
  service: script.temperature_up
  service_data:
    climate_id: climate.bagni
    value: 0.5
  target: {}
show_state: true
icon: hass:plus
icon_height: 50px
name: Aumenta set °C

And an analog, similar, for the decrease.
The defect is that the increase (and decrease) of the set point does not stop, but despite having set the max_temp to 28 it continues to increase.
I have been trying for hours to stop the increase at 28 ° c by copying models found in the forum but to no avail.
Do you have any suggestions. Thanks in advance

Which integration are you using that has max_temp? Is it Generic Thermostat?

yes, this one

climate:
  - platform: generic_thermostat
    name: Bagni
    heater: switch.out_pin_d1_wemos_2
    target_sensor: sensor.esp01_temperature
    min_temp: 15
    max_temp: 28
    ac_mode: false
    target_temp: 18
    cold_tolerance: 0.5
    hot_tolerance: 0
    min_cycle_duration:
      seconds: 5
    keep_alive:
      minutes: 3
    initial_hvac_mode: "off"
    #away_temp: 16
    precision: 0.5

The only model I’ve been able to get to work is

value_template: >-
    {{ state_attr('climate.bagni', 'temperature')|float >=
    state_attr('climate.bagni', 'max_temp')|float }}

Which gives me the value TRUE or false. The public if someone can be useful.

max_temp limits the maximum temperature a user can set using the Thermostat card. It does not limit the maximum temperature you can set using the climate.set_temperature service call.

You will have to enhance script.temperature_up so it rejects attempts to increase the temperature above 28 (similarly script.temperature_down will need to ignore requests to decrease below 15).

For example:

alias: temperature_up
sequence:
  - variables:
      temp: "{{ state_attr('climate.bagni', 'temperature') }}"
  - condition: template
    value_template: "{{ temp < 28 }}"
  - service: climate.set_temperature
    target:
      entity_id: climate.bagni
    data:
      temperature: "{{ temp + value | float }}"
mode: single

Thanks for your time. I have already fixed my scripts.
Home assistant is great but template development, to my knowledge, is really complicated.