create_device_class_groups.yaml 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. blueprint:
  2. name: Create device class groups by room and class itself
  3. description: >-
  4. A script that sends an actionable notification with a confirmation before
  5. running the specified action.
  6. domain: script
  7. input:
  8. areas:
  9. selector:
  10. object:
  11. default: []
  12. device_classes:
  13. selector:
  14. object:
  15. default: []
  16. mode: restart
  17. sequence:
  18. - variables:
  19. areas: !input areas
  20. device_classes: !input device_classes
  21. num_areas: >-
  22. {{ areas|length if areas is not string and areas is iterable else 0 }}
  23. num_device_classes: >-
  24. {{ device_classes|length if areas is not string and device_classes is iterable else 0 }}
  25. - service: system_log.write
  26. data:
  27. level: "warning"
  28. message: >-
  29. Creating groups for:
  30. - {{ device_classes | join("\n\n- ")}}
  31. and areas:
  32. - {{ areas | join("\n\n- ")}}
  33. - repeat:
  34. while: "{{ repeat.index <= num_device_classes }}"
  35. sequence:
  36. - variables:
  37. device_class_name: "{{ device_classes[ repeat.index - 1 ] }}"
  38. devices: >-
  39. {{ states|selectattr('attributes.device_class', 'eq', device_class_name)|map(attribute='entity_id')|list }}
  40. - choose:
  41. conditions: "{{ devices|length > 0 }}"
  42. sequence:
  43. - service: group.set
  44. data:
  45. object_id: "{{ device_class_name }}"
  46. add_entities: "{{ devices }}"
  47. - repeat:
  48. while: "{{ repeat.index <= num_areas }}"
  49. sequence:
  50. - variables:
  51. area: "{{ areas[ repeat.index - 1 ] }}"
  52. area_group_name: "{{ area.replace(' ', '_')|lower + '_' + device_class_name }}"
  53. area_entities: >-
  54. {% set entities = namespace(list=[]) %}
  55. {% for entity in expand('group.' + device_class_name) if area_name(entity.entity_id) == area %}
  56. {% set entities.list = entities.list + [ entity.entity_id ] %}
  57. {% endfor %}
  58. {{ entities.list }}
  59. - choose:
  60. conditions: "{{ area_entities|length > 0 }}"
  61. sequence:
  62. - service: group.set
  63. data:
  64. object_id: "{{ area_group_name }}"
  65. add_entities: "{{ area_entities }}"