12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- blueprint:
- name: Create device class groups by room and class itself
- description: >-
- A script that sends an actionable notification with a confirmation before
- running the specified action.
- domain: script
- input:
- areas:
- selector:
- object:
- default: []
- device_classes:
- selector:
- object:
- default: []
- mode: restart
- sequence:
- - variables:
- areas: !input areas
- device_classes: !input device_classes
- num_areas: >-
- {{ areas|length if areas is not string and areas is iterable else 0 }}
- num_device_classes: >-
- {{ device_classes|length if areas is not string and device_classes is iterable else 0 }}
- - service: system_log.write
- data:
- level: "warning"
- message: >-
- Creating groups for:
- - {{ device_classes | join("\n\n- ")}}
- and areas:
- - {{ areas | join("\n\n- ")}}
- - repeat:
- while: "{{ repeat.index <= num_device_classes }}"
- sequence:
- - variables:
- device_class_name: "{{ device_classes[ repeat.index - 1 ] }}"
- devices: >-
- {{ states|selectattr('attributes.device_class', 'eq', device_class_name)|map(attribute='entity_id')|list }}
- - choose:
- conditions: "{{ devices|length > 0 }}"
- sequence:
- - service: group.set
- data:
- object_id: "{{ device_class_name }}"
- add_entities: "{{ devices }}"
- - repeat:
- while: "{{ repeat.index <= num_areas }}"
- sequence:
- - variables:
- area: "{{ areas[ repeat.index - 1 ] }}"
- area_group_name: "{{ area.replace(' ', '_')|lower + '_' + device_class_name }}"
- area_entities: >-
- {% set entities = namespace(list=[]) %}
- {% for entity in expand('group.' + device_class_name) if area_name(entity.entity_id) == area %}
- {% set entities.list = entities.list + [ entity.entity_id ] %}
- {% endfor %}
- {{ entities.list }}
- - choose:
- conditions: "{{ area_entities|length > 0 }}"
- sequence:
- - service: group.set
- data:
- object_id: "{{ area_group_name }}"
- add_entities: "{{ area_entities }}"
|