Help with template in automation to control netatmo valves with a thermostat

Hello, I am new to the wonderful world of HA I am moving forward with my automations, but it has me blocked.
I found this case and tried to adapt it to my case:

https://community.home-assistant.io/t/thermostat-to-control-switches/279494/6

I want a netatmo thermostat to control 3 netatmo valves since the thermostat temperature is much more reliable. This thermostat is:

And this is my automation, the sintaxis is ok

- alias: prueba_termostato_atico
  trigger:
    platform: template
    value_template: >
     {{
       state_attr('climate.termostato_atiko', 'current_temperature')|float <= state_attr('climate.termostato_atiko', 'temperature')|float
       or
       state_attr('climate.termostato_atiko', 'current_temperature')|float >= state_attr('climate.termostato_atiko', 'temperature')|float 
      }}' 
  action:
    service: climate.turn_{{ 'on' if state_attr('climate.termostato_atiko', 'current_temperature')|float
      <= state_attr('climate.termostato_atiko', 'temperature')|float else 'off' }}
    target:
      entity_id:
      - climate.valvula_atico_jolasak
      - climate.valvula_atico_sofa
      - climate.valvula_atico_grande

in developer tools when I lower or raise the thermostat temperature the service changes but is not triggered

What am I doing wrong? Thanks in advance

Double check your action in the actual automation. The action you have in the editor is “switch.turn_”, but climate entities require “climate.turn_”.

Also, be aware that not all climate devices support being turned on/off.

Are you saying that you are trying to test the triggers in the Template Editor (like shown in your screenshot)? Because you cannot test them like that. The Template Editor is only for testing templates, not entire automations and scripts.

Double check your action in the actual automation. The action you have in the editor is “switch.turn_”, but climate entities require “climate.turn_”.

Also, be aware that not all climate devices support being turned on/off.[quote="Didgeridrew, post:2, topic:363233, full:true"]

Hello, thanks for the answers.
In a previous automation I did use “climate.turn_on” with the same valves and it worked. My problem is with the template because it never triggers the automation

Are you saying that you are trying to test the triggers in the Template Editor (like shown in your screenshot)? Because you cannot test them like that. The Template Editor is only for testing templates, not entire automations and scripts.

I tested the isolated template in the editor and since it gave the value “true”, as I was a bit desperate, I tested the entire automation to see what would happen.
I’m sorry, I don’t know how to name the message.
Regards

Your Template Trigger fires when its template evaluates to true after previously having been false. It’s the transition from false to true that causes it to trigger.

Once it’s true, if any of the temperatures in the template change but the template continues to report true then it will not trigger again. It first has to evaluate to false before changing to true in order to trigger again.

You are absolutely right, even if the target temperature varies, the result is never “false”. I have no idea where the bug is.
I have been with HA alone for two months and this is very big for me. Previously I tried with scripts but I immediately realized that the solution is the templates, but I am unable

The bug is that this is the wrong trigger for your automation. Its value will never change to false and without changes automations don’t trigger. You could use a simple State trigger based on the “current_temperature” attribute. By leaving the “to” and “from” fields blank it would trigger on every state change. This can be useful for some automations, but it can cause the device to bounce between on and off which is generally not a good idea electro-mechanical devices. Instead, try using a template in a Numeric State trigger. To avoid turning the valves on and off too often, the example below looks for a difference of at least 2˚ which is sustained for 5 minutes.

- alias: prueba_termostato_atico
  trigger:
    - platform: numeric_state
      entity_id: climate.termostato_atiko
      value_template: "{{ (state.attributes.current_temperature - state.attributes.temperature) | abs }}"
      above: 2
      for: "00:05:00"
  condition: []
  action:
    service: "climate.turn_{{ 'on' if state_attr('climate.termostato_atiko', 'current_temperature')|float <= state_attr('climate.termostato_atiko', 'temperature')|float else 'off' }}"
    target:
      entity_id:
      - climate.valvula_atico_jolasak
      - climate.valvula_atico_sofa
      - climate.valvula_atico_grande
1 Like

Thank you very much, what you have done is very logical but it has not worked, the automation has not been triggered. I have tried to check in the template editor and it says this (I don’t even know if I’m checking it well, sorry)

Curious, that automation is pretty much straight from the Numeric State trigger docs so I assumed it would work… but I’ve now tried multiple variations, and I can’t get it to trigger either.

There is always the option of creating a state-based template binary sensor, and using that in a state trigger.

###configuration.yaml

template:
  - binary_sensor:
      - name: "Termostato Atico Diferencia"
        state: >
          {{ (state_attr('climate.termostato_atiko', 'current_temperature')|float(0) - state_attr('climate.termostato_atiko', 'temperature')|float(0)) | abs > 2}}'

And then the automation would be:

- alias: prueba_termostato_atico
  trigger:
    - platform: state
      entity_id: binary_sensor.termostato_atico_diferencia
      to: "on"
      for: "00:05:00"
  condition: []
  action:
    - service: >
        climate.turn_{{ 'on' if state_attr('climate.termostato_atiko', 'current_temperature')|float(0) <= state_attr('climate.termostato_atiko', 'temperature')|float(0) else 'off' }}"
      target:
        entity_id:
          - climate.valvula_atico_jolasak
          - climate.valvula_atico_sofa
          - climate.valvula_atico_grande

You can’t check that specific template in the Template Editor because of the way it references entities.

This doesn’t explicitly refer to any entity:

state.attributes.current_temperature

It implicitly refers to climate.termostato_atiko when used together like this:

    - platform: numeric_state
      entity_id: climate.termostato_atiko
      value_template: "{{ (state.attributes.current_temperature - state.attributes.temperature) | abs }}"

Why it fails to trigger is probably because the Numeric State Trigger is monitoring changes to the climate entity’s state value, not its attributes. The state value of a climate entity isn’t numeric, it’s a string like heat, cool, auto, off, etc.

1 Like

Thank you very much, you are patient, thank you very much. The sensor is created and changes from true to false when I put a difference of 2 in the target temperature but it still does not trigger the automation. I think the sensor template is fine but for something it does not trigger it

shouldn’t the name of the binary_sensor be

termostato_atico_diferencia

and not

“Termostato Atico Diferencia”

Is there a way that I refer to the attribute instead of the mode (heating, off, …)?
Thank you very much for the answers and the attempts. With the normal integrations and automations I have done relatively well but the templates without knowing how to program seem impossible and the documentation that I find does not get me out of trouble, I am totally lost.
On top of that I am going on a trip and I will not be able to do any tests until Monday, I hope to come inspired.
Thank you very much again

It also doesn’t work by renaming the sensor with _ in the configuration.yaml.
I have to leave it. On Monday I can start testing again. Thanks

When using the old template sensor format you would have to name it like that, but the current method automatically converts the name to its equivalent entity id turning " " into “_” and making everything lowercase.

When you get back to it, make testing easier on yourself by reducing the “for:” to 30 seconds or a minute… that way you don’t have to sit there waiting.

Thanks. I have changed it to 5seconds, jjejeje

Hello again.
I’m stuck in the same place, I’ve been testing all week and the worst thing is that in one of those tests the automation was triggered, I made some changes in the action and it never worked again. The code I used was the first one that Didgeridrew put in:

- alias: prueba_termostato_atico
  trigger:
    - platform: numeric_state
      entity_id: climate.termostato_atiko
      value_template: "{{ (state.attributes.current_temperature - state.attributes.temperature) | abs }}"
      above: 1
  condition: []
  action:
    service: "climate.turn_{{ 'on' if state_attr('climate.termostato_atiko', 'current_temperature')|float <= state_attr('climate.termostato_atiko', 'temperature')|float else 'off' }}"
    target:
      entity_id:
      - climate.valvula_atico_jolasak
      - climate.valvula_atico_sofa
      - climate.valvula_atico_grande 

Does anyone see something wrong? In the template editor it says
“UndefinedError: ‘state’ is undefined”

I have added the line

mode: single

to the code and the same thing happened to me, the automation has been triggered 1 time and it does not do it again.

I may be completely off base here but shouldn’t the trigger value template be this?

value_template: "{{ ((state_attr('climate.termostato_atiko', 'current_temperature') | float) - (state_attr('climate.termostato_atiko', 'temperature') | float)) | abs }}"

1 Like