Caldin-Maldin

Состояние сущностей

Aug 30th, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. alias: Состояние датчиков и сущностей (недоступны)
  2. description: ""
  3. trigger:
  4. - platform: time_pattern
  5. hours: /2
  6. - platform: event
  7. event_type: telegram_command
  8. event_data:
  9. command: /Датчики
  10. condition:
  11. - condition: time
  12. after: "06:00:00"
  13. before: "23:00:00"
  14. - condition: template
  15. value_template: |-
  16. {% set ns = namespace(counter = 0) -%}
  17. {% set domains = ['sensor', 'binary_sensor', 'switch','light'] -%}
  18. {%- for domain in domains -%}
  19. {%- for item in states[domain] if ((item.state == "unavailable") or (item.state == "unknown")) -%}
  20. {%- set ns.counter = ns.counter + 1 -%}
  21. {%- endfor %}
  22. {%- endfor %}
  23. {{ ns.counter > 0 }}
  24. action:
  25. - service: notify.me
  26. data:
  27. message: >
  28. Следующие датчики и сущности недоступны: {%- set domains = ['sensor',
  29. 'binary_sensor', 'light', 'switch'] %}
  30. {% for domain in domains %}
  31. {%- for item in states[domain] if ((item.state == "unavailable") or (item.state == "unknown")) %}
  32. {{ item.name }}
  33. {%- endfor -%}
  34. {% endfor %}
  35. - if:
  36. - condition: template
  37. value_template: |-
  38. {{ states.automation
  39. | selectattr('attributes.current', 'eq', 1) | list | count > 0 }}
  40. - condition: template
  41. value_template: |-
  42. {% set ns = namespace(counter = 0) -%}
  43. {% for state in states.automation
  44. | selectattr('attributes.current','defined')
  45. | selectattr('attributes.current','==', 1)
  46. | selectattr('attributes.last_triggered', 'defined')
  47. | selectattr('attributes.last_triggered')
  48. | sort(attribute='attributes.last_triggered') -%}
  49. {%- set time_now = now() | as_timestamp(now()) -%}
  50. {%- set time_triggered = state.attributes.last_triggered | as_timestamp(now()) -%}
  51. {%- set t = time_now - time_triggered -%}
  52. {% if t > 3600 %}
  53. {%- set ns.counter = ns.counter + 1 -%}
  54. {%- endif -%}
  55. {% endfor %}
  56.  
  57. {{ ns.counter > 0 }}
  58. enabled: false
  59. - condition: template
  60. value_template: |2-
  61. {% set number = states.automation
  62. | selectattr('attributes.current','defined')
  63. | selectattr('attributes.current','==', 1)
  64. | selectattr('attributes.last_triggered', 'defined')
  65. | selectattr('attributes.last_triggered', '<', now() - timedelta(hours=1))
  66.  
  67. | sort(attribute='attributes.last_triggered')
  68. | map(attribute='entity_id')
  69. | list | length %}
  70. {{number > 0 }}
  71. then:
  72. - variables:
  73. auto_name: >-
  74. {% set automations = states.automation
  75. | selectattr('attributes.current','defined')
  76. | selectattr('attributes.current','==', 1)
  77. | selectattr('attributes.last_triggered', 'defined')
  78. | selectattr('attributes.last_triggered', '<', now() - timedelta(hours=1))
  79. | sort(attribute='attributes.last_triggered')
  80. | list %}
  81. {% for automation in automations %}
  82.  
  83. {% set name = automation.attributes.friendly_name -%}
  84.  
  85. {% set time = automation.attributes.last_triggered | as_timestamp |
  86. timestamp_custom('%H:%M') -%}
  87.  
  88. {{name}}, запущена в {{time}} часов
  89.  
  90. {%- endfor %}
  91. - service: telegram_bot.send_message
  92. data:
  93. message: |
  94. Запущенные более часа назад автоматизации:
  95.  
  96. {{auto_name}}
  97. target: 11111111111
  98. inline_keyboard:
  99. - Перезагрузить автоматизации {{"\U0001F503"}}:/reload
  100. - if:
  101. - condition: template
  102. value_template: >-
  103. {% set cameras = ['dahua', 'yard_side', 'yard_above', 'street',
  104. 'entrance'] %}
  105.  
  106. {% set camera_name = {'sensor.dahua_camera_fps': 'Домофон',
  107. 'sensor.yard_side_camera_fps': 'Двор сбоку',
  108. 'sensor.yard_above_camera_fps': 'Двор сверху',
  109. 'sensor.street_camera_fps': 'Улица', 'sensor.entrance_camera_fps':
  110. 'Вход'} %}
  111.  
  112.  
  113. {% set ns = namespace(list_camera=[]) %}
  114.  
  115. {% for camera in cameras %}
  116. {%- if states("sensor." + camera + "_camera_fps") == "0" and
  117. (as_timestamp(now()) - as_timestamp(states.sensor[camera + '_camera_fps'].last_updated)) > 3600 %}
  118. {%- set ns.list_camera = ns.list_camera + [camera_name['sensor.' + camera + '_camera_fps']] %}
  119. {%- endif %}
  120. {% endfor %}
  121.  
  122.  
  123. {{ ns.list_camera | length > 0 }}
  124. then:
  125. - variables:
  126. list_camera: >
  127. {% set cameras = ['dahua', 'yard_side', 'yard_above', 'street',
  128. 'entrance'] %}
  129.  
  130. {% set camera_name = {'sensor.dahua_camera_fps': 'Домофон',
  131. 'sensor.yard_side_camera_fps': 'Двор сбоку',
  132. 'sensor.yard_above_camera_fps': 'Двор сверху',
  133. 'sensor.street_camera_fps': 'Улица', 'sensor.entrance_camera_fps':
  134. 'Вход'} %}
  135.  
  136.  
  137. {% set ns = namespace(list_camera=[]) %}
  138.  
  139. {% for camera in cameras %}
  140. {%- if states("sensor." + camera + "_camera_fps") == "0" and
  141. (as_timestamp(now()) - as_timestamp(states.sensor[camera + '_camera_fps'].last_updated)) > 3600 %}
  142. {%- set ns.list_camera = ns.list_camera + [camera_name['sensor.' + camera + '_camera_fps']] %}
  143. {%- endif %}
  144. {% endfor %}
  145.  
  146.  
  147. {{ ns.list_camera }}
  148. - service: telegram_bot.send_message
  149. data:
  150. message: |
  151. {% if list_camera | length == 1 %}
  152. Камера <a href="https://xxxxxx.xxxxxxx">'{{ list_camera | join(', ') }}' </a> не работает.
  153. {% else %}
  154. Камеры: <a href="https://xxxxxxxx.xxxxxxxxx">'{{ list_camera | join(', ') }}' </a> не работают.
  155. {%- endif %}
  156. target: 1111111111
  157. mode: single
Advertisement
Add Comment
Please, Sign In to add comment