create_device_class_groups.yaml 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. - repeat:
  26. while: "{{ repeat.index <= num_device_classes }}"
  27. sequence:
  28. - variables:
  29. device_class: "{{ device_classes[ repeat.index - 1 ] }}"
  30. - choose:
  31. conditions: "{{ states|selectattr('attributes.device_class', 'eq', device_class)|list|length > 0 }}"
  32. sequence:
  33. - service: group.set
  34. data:
  35. object_id: "{{ device_class }}"
  36. add_entities: "{{ states|selectattr('attributes.device_class', 'eq', device_class)|map(attribute='entity_id')|list }}"
  37. - repeat:
  38. while: "{{ repeat.index <= num_areas }}"
  39. sequence:
  40. - variables:
  41. area: "{{ areas[ repeat.index - 1 ] }}"
  42. area_entities: >-
  43. {% set entities = namespace(list=[]) %}
  44. {% for entity in expand('group.' + device_class) if area_name(entity.entity_id) == area %}
  45. {% set entities.list = entities.list + [ entity.entity_id ] %}
  46. {% endfor %}
  47. {{ entities.list }}
  48. - choose:
  49. conditions: "{{ area_entities|length > 0 }}"
  50. sequence:
  51. - service: group.set
  52. data:
  53. object_id: "{{ area.replace(' ', '_')|lower + '_' + device_class }}"
  54. add_entities: "{{ area_entities }}"