Automation condition template - beginner is confused

Hi,

I am a HomeAssistant beginner and I am trying to learn the automations. I want to create an automation that checks the frost protection status of my weather station. If the frost status is pending, all shutters should be checked if they are closed more than 65%. If so, they should be raised to 65%.

I wanted to solve this with a template and search all shutters via the device_class and current_position attribute. But I don’t understand how I can set the desired position for all found shutters (see ‘then:’ => target entity_id).

Or should I solve this completely different ? Thanks for any help

Shutter-Example:

- name: "UG-Whz | Besch.-Roll.-SW-1DK-Door"
  device_class: "shutter"
  move_short_address: "3/0/14"
  move_long_address: "3/0/15"
  position_address: "3/0/16"
  position_state_address: "3/0/17"
  #travelling_time_down: 51
  #travelling_time_up: 61
  invert_position: true

Automation:

alias: New Auto.
description: ""
trigger:
  - platform: time_pattern
    minutes: "10"
condition:
  - condition: state
    entity_id: binary_sensor.aussen_wetterstation_frostalarm
    state: "on"
action:
  - if:
      - condition: template
        value_template: >-
          "{{ states.cover | selectattr('attributes.device_class', 'eq',
          'shutter') |  selectattr('current_position', '>=', '65') }}"
    then:
      - service: cover.set_cover_position
        data:
          position: 65
        target:
          entity_id: ???
mode: single

Hi :wave:t2:
Do you need to check the position at all?

I reckon you can just set it to 65. If the shutter is already at 65 nothing will happen.

Then you can just create a group with all the shutters and set the value for the group.

Also, perhaps your frost status should be the trigger i.e. when it changes to pending?

Try this version:

alias: Example 
description: ""
variables:
  covers: "{{ states.cover | selectattr('attributes.device_class', 'eq', 'shutter') |  selectattr('current_position', '>=', 65) | map(attribute='entity_id') | list }}"
trigger:
  - platform: state
    entity_id: binary_sensor.aussen_wetterstation_frostalarm
    from: 'off'
    to: 'on'
condition:
  - condition: template 
    value _template: "{{ covers | count > 0 }}"
action:
  - service: cover.set_cover_position
    data:
      position: 65
    target:
      entity_id: "{{ covers }}"
mode: single

thank you for the quick reply.

I do not want the shutter fixed to 65% drives as soon as the frost protection is on, but that in case of frost danger is checked whether a shutter is already closed above 65% and that is then corrected. I am still testing

It may be that the frost status is already “on” and then someone closes the shutter completely (100%). Then, at the latest after 10 minutes, the automation is triggered again and the shutter is raised to the safe 65%.

Once you figure out the exact requirements you want, let us know and we’ll help you create the automation.

1 Like

Hello again,
I am still struggling with the automation. Thanks to @123 for the corrected YAML code, I learned something again!

I still have the time-pattern trigger installed, because I still think that is the only way to detect when a shutter is closed after the frost protection has already changed from off to on. Let’s assume the frost protection changes to “on” and an hour later someone moves the shutter completely down. I can only get a handle on this if I keep checking cyclically, or am I making a mistake in my thinking?

alias: Beschattung.Alarm.Frostschutz
description: ""
trigger:
  - platform: time_pattern
    minutes: "10"
condition:
  - condition: state
    entity_id: binary_sensor.aussen_wetterstation_frostalarm
    state: "on"
  - condition: template
    value_template: "{{ covers | count > 0 }}"
action:
  - service: cover.set_cover_position
    data:
      position: 65
    target:
      entity_id: "{{ covers }}"
  - service: notify.telegram
    data:
      title: Automation Beschattung.Alarm.Frostschutz is triggered
      message: "Message: Automation Beschattung.Alarm.Frostschutz is triggered"
variables:
  covers: >-
    {{ states.cover | selectattr('attributes.device_class', 'eq', 'shutter') | 
    selectattr('current_position', '>', 65) | map(attribute='entity_id') | list
    }}
mode: single

My bigger problem is that there is an error when executing the template:

Error rendering variables: UndefinedError: ‘homeassistant.helpers.template.TemplateState object’ has no attribute 'current_position’

I get the same error in the template editor - but this is not true, each of the shutters has the attribute “current_position”:

Example:

current_position: 0
device_class: shutter
friendly_name: EG-Kitchen | Besch.-Roll.-NW-1DK-Fen.
supported_features: 127

template:

{{ states.cover | selectattr('attributes.device_class', 'match', 'shutter') | list }}

nicely shows the listing of all shutters, a small extract:

[<template TemplateState(<state cover.eg_essz_besch_roll_no_2dk_fen=closed; current_position=0, device_class=shutter, friendly_name=EG-Essz. | Besch.-Roll.-NO-2DK-Fen., supported_features=127 @ 2022-11-10T07:40:27.683313+01:00>)>,

Can you please help again :hugs:
thank you!

It’s


|selectattr('attributes.current_position', '>', 65) 

oh no, I totally missed that.
Thank you very much. Now it works.

It’s not necessary to use a Time Pattern Trigger to repeatedly check the shutters. The automation can use two triggers:

  1. Monitor the frost sensor and trigger when it turns on.
  2. Monitor the shutters for a change in position to a value greater than 65.

The automation then needs two conditions:

  1. Confirm the frost sensor is on.
  2. Confirm the shutter position is greater than 65.

What I have described (triggers with similar conditions) is a common technique and is more efficient and responsive than polling the frost sensor and covers.

ok, great. From a logic point of view, I get it. Every time a shutter is “moved”, the query is also triggered/updated (if position > 65% etc.).

({{ states.cover | selectattr('attributes.device_class', 'eq', 'shutter') | 
    selectattr('attributes.current_position', '>', 65) |
    map(attribute='entity_id') | list }})

Perfect to trigger the automation - however I fail to set the trigger. How do I use the ‘covers’ variable as trigger ? I do not understand yet.

trigger:
  - platform: state
    entity_id: binary_sensor.aussen_wetterstation_frostalarm
    from: 'off'
    to: 'on'
  - ?????????  "{{ covers | count > 0 }}" ??????????
condition:
  - condition: state
    entity_id: binary_sensor.aussen_wetterstation_frostalarm
    state: "on"
  - condition: template
    value_template: "{{ covers | count > 0 }}"

thanks again !

  1. How many cover entities do you have?
  2. How many of them are shutters?
  3. How many more shutters do you plan to add?

thx …!

Create a Template Sensor that reports the number of shutters whose position exceeds 65 and has an attribute called shutters which lists the entity_ids of the shutters.

template:
  - sensor:
      - name: Open Shutters
        state: >
          {{ states.cover | selectattr('attributes.device_class', 'eq', 'shutter')
            | selectattr('attributes.current_position', '>=', 65)
            | list | count }}
        attributes:
          shutters: >
            {{ states.cover | selectattr('attributes.device_class', 'eq', 'shutter')
              | selectattr('attributes.current_position', '>=', 65)
              | map(attribute='entity_id') | list }}

Create the following automation that triggers when either the frost sensor is on or when sensor.open_shutters changes state (the empty to: option is required; it will trigger on state-changes only, not attribute changes.). When triggered by either one, the conditions check if the frost sensor is on and the value of sensor.open_shutters is greater than zero.

alias: Example 
trigger:
  - platform: state
    entity_id: binary_sensor.aussen_wetterstation_frostalarm
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: sensor.open_shutters
    to:
condition:
  - "{{ states('sensor.open_shutters') | int(0) > 0 }}"
  - "{{ is_state('binary_sensor.aussen_wetterstation_frostalarm', 'on') }}"
action:
  - service: cover.set_cover_position
    target:
      entity_id: "{{ state_attr('sensor.open_shutters', 'shutters') }}"
    data:
      position: 65
mode: single

This is untested (I don’t have shutters) so if you encounter any problems just let me know and I’ll help you fix them.

2 Likes

it works perfectly … thank you for showing me the right way! The only thing I still noticed: the automation is triggered several times in quick succession (seconds apart). The shutters need a certain time to move - so I need a delay which “bridges” the runtime of the shutters. So I built in a delay after the action.

  • delay: ‘00:02:00’

thx…!

1 Like

i get this error message
What can I do?

template:
  - sensor:
      - name: Open Shutters
        state: >
          {{ states.cover | selectattr('attributes.device_class', 'eq', 'shutter')
            | selectattr('attributes.current_position', '>=', 65)
            | list | count }}
        attributes:
          shutters: >
            {{ states.cover | selectattr('attributes.device_class', 'eq', 'shutter')
              | selectattr('attributes.current_position', '>=', 65)
              | map(attribute='entity_id') | list }}

Add this to the two templates:

selectattr('attributes.device_class', 'defined')

Like this:

template:
  - sensor:
      - name: Open Shutters
        state: >
          {{ states.cover | selectattr('attributes.device_class', 'defined')
            | selectattr('attributes.device_class', 'eq', 'shutter')
            | selectattr('attributes.current_position', '>=', 65)
            | list | count }}
        attributes:
          shutters: >
            {{ states.cover | selectattr('attributes.device_class', 'defined')
              | selectattr('attributes.device_class', 'eq', 'shutter')
              | selectattr('attributes.current_position', '>=', 65)
              | map(attribute='entity_id') | list }}
1 Like