Window open/close detection with climate.set_temperature cached by input_number

Dear Community,

after a few weeks working with this thrilling operating system I am almost pleased with my window detection. I use a schedule of three different basic operating temperatures but want to have individual temperatures controlled by presence or manual action. So I think it makes sense to let a window detection work like this. It might not be very elegant. But not yet I have integrated the usage of variables and I am not firm in jinja2. It was a lot of intensive work though. There is a minimal issue, I can’t explain yet. So would please somebody have a look at my automation to help me?

This automation should do the following:
When I open a window in a specific room the current target temperature value of the specific climate is temporarily written into a specific input number. Each room needs one. The new target temperature is set to an individual low (energy saving) temperature of another input_number. When I close the window again the memorized input number is set back as new target temperature again.

What the automation does:
When I open a window it actually writes the current target temperature in the correct input number and sets the new individual low temperature. But when I close the window again, it will always pick the first input number of the template, here ‘input_number.schlafzimmer_temp_soll’.

The template syntax:

{% if 'trigger.to_state.object_id', 'binary_sensor.schlafzimmer_fenster_kontakt' %}
{{ state_attr('climate.schlafzimmer', 'temperature') }}

is working absolutely correct in the first part of the automation.

So I believe it makes a difference if I use a data template for a value > or a temperature > but I can’t make a sense of it.

Thanks a lot for any help!
Best,
Torsten

- alias: 'fenster_offen_input_number.set_value'
  trigger: 
    platform: state
    from: 'off'
    to: 'on'
    entity_id:
      - binary_sensor.schlafzimmer_fenster_kontakt    
      - binary_sensor.wohnzimmer_fenster_kontakt
      - binary_sensor.kinderzimmer_fenster_kontakt
      - binary_sensor.bad_fenster_kontakt
  action:
    service: input_number.set_value
    data_template:
      entity_id: > 
          {% set raum = trigger.to_state.object_id.split('_')[0] %}
          input_number.{{raum}}_temp_soll
      value: >
          {% if 'trigger.to_state.object_id', 'binary_sensor.schlafzimmer_fenster_kontakt' %}
          {{ state_attr('climate.schlafzimmer', 'temperature') }}
          {% elif 'trigger.to_state.object_id', 'binary_sensor.wohnzimmer_fenster_kontakt' %}
          {{ state_attr('climate.wohnzimmer', 'temperature') }}
          {% elif 'trigger.to_state.object_id', 'binary_sensor.kinderzimmer_fenster_kontakt' %}
          {{ state_attr('climate.kinderzimmer', 'temperature') }}
          {% elif 'trigger.to_state.object_id', 'binary_sensor.bad_fenster_kontakt' %}
          {{ state_attr('climate.bad', 'temperature') }}
          {% endif %}
        
- alias: 'fenster_offen_climate.set_temperature'
  trigger: 
    platform: state
    from: 'off'
    to: 'on'
    entity_id:
      - binary_sensor.schlafzimmer_fenster_kontakt    
      - binary_sensor.wohnzimmer_fenster_kontakt
      - binary_sensor.kinderzimmer_fenster_kontakt
      - binary_sensor.bad_fenster_kontakt
  action:
    service: climate.set_temperature
    data_template:
      entity_id: >
          {% set raum = trigger.to_state.object_id.split('_')[0] %}
          climate.{{raum}}
      temperature: >
          {{ states("input_number.temp_fenster_offen") }}
            
- alias: 'fenster_geschlossen_climate.set_temperature'
  trigger: 
    platform: state
    from: 'on'
    to: 'off'
    entity_id:
      - binary_sensor.schlafzimmer_fenster_kontakt    
      - binary_sensor.wohnzimmer_fenster_kontakt
      - binary_sensor.kinderzimmer_fenster_kontakt
      - binary_sensor.bad_fenster_kontakt
  action:
    service: climate.set_temperature
    data_template:
      entity_id: >
          {% set raum = trigger.to_state.object_id.split('_')[0] %}
          climate.{{raum}}
      temperature: >
          {% if 'trigger.to_state.object_id', 'binary_sensor.schlafzimmer_fenster_kontakt' %}
          {{ states('input_number.schlafzimmer_temp_soll') }}
          {% elif 'trigger.to_state.object_id', 'binary_sensor.wohnzimmer_fenster_kontakt' %}
          {{ states('input_number.wohnzimmer_temp_soll') }}
          {% elif 'trigger.to_state.object_id', 'binary_sensor.kinderzimmer_fenster_kontakt' %}
          {{ states('input_number.kinderzimmer_temp_soll') }}
          {% elif 'trigger.to_state.object_id', 'binary_sensor.bad_fenster_kontakt' %}
          {{ states('input_number.bad_temp_soll') }}
          {% endif %}

{% if 'trigger.to_state.object_id', 'binary_sensor.schlafzimmer_fenster_kontakt' %}

This is not the right syntax. You’re just getting unlucky that this evaluates to ‘True’…always. Open the template editor and try it yourself.

# This will print 'STRANGE'
{% if 'string', 'nonsense' %}
  STRANGE
{% endif %}

You need:

{% if trigger.to_state.entity_id == 'binary_sensor.schlafzimmer_fenster_kontakt' %}

object_id is stripping off the domain, so if you wanted to keep using that, you’d just compare trigger.to_state.object_id == ‘schlafzimmer_fenster_kontakt’. Makes no difference, just need the correct comparison syntax.

The same is true for all of your elif statements.

Or, since it looks like you have consistent naming, why even have the if/elif statements?

  action:
    service: input_number.set_value
    data_template:
      entity_id: > 
          {% set raum = trigger.to_state.object_id.split('_')[0] %}
          input_number.{{raum}}_temp_soll
      value: >
          {% set entity = "climate."+{{trigger.to_state.object_id.split('_')[0]}} %}
          {{ state_attr(entity, 'temperature') }}

Thank you very much for help!
I was hoping to learn how to make it better.
Best regards,
Torsten

I have exchanged my if/elif part by your template now and got an Error when executing.
Not sure yet where it stucks exactly in the value >

Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token ':', got '}') for dictionary value @ data['action'][0]['data_template']['value']. Got None. (See /config/configuration.yaml, line 208). Please check the docs at https://home-assistant.io/integrations/automation/

…and there we go:

action:
    service: input_number.set_value
    data_template:
      entity_id: > 
          {% set raum = trigger.to_state.object_id.split('_')[0] %}
          input_number.{{raum}}_temp_soll
      value: >
          {% set entity = "climate."+ trigger.to_state.object_id.split('_')[0] %}
          {{ state_attr(entity, 'temperature') }}

two pairs of brackets too much…
I will now transform it to the other part and exchange if/elif there as well.

I love this automation…
Thanks again for help!

1 Like