Skip to content

Instantly share code, notes, and snippets.

@pavolholes
Last active March 13, 2023 20:10
Show Gist options
  • Save pavolholes/ad1b03013e0059ad0c797065a4f6c515 to your computer and use it in GitHub Desktop.
Save pavolholes/ad1b03013e0059ad0c797065a4f6c515 to your computer and use it in GitHub Desktop.
Home Assistant Notifikacia Senzory maju problem
- id: '1673221013583'
alias: Home Assistant Notifikacia Senzory maju problem
description: ''
trigger:
- platform: state
entity_id:
- sensor.senzory_s_problemom_pocet
condition:
- condition: template
value_template: '{{ trigger.from_state.state != ''unavailable'' }}'
action:
- choose:
- conditions:
- condition: numeric_state
entity_id: sensor.senzory_s_problemom_pocet
above: 0
sequence:
- service: notify.mobile_app_iphonepavol
data:
title: ⚠️Senzory majú problém
message: '{{states(''sensor.senzory_s_problemom'')}}'
- conditions:
- condition: numeric_state
entity_id: sensor.senzory_s_problemom_pocet
below: 1
sequence:
- service: notify.mobile_app_iphonepavol
data:
message: ✅Problémy sú vyriešené
title: ⚠️Senzory majú problém
mode: single
sensor:
# https://home-assistant.io/components/sensor.template/
# https://www.home-assistant.io/integrations/template#legacy-sensor-configuration-format
- platform: template
sensors:
senzory_s_problemom:
friendly_name: Senzory s problémom
value_template: >-
{% set threshold_hours = 2 %}
Senzory s problémom dlhšie než {{threshold_hours}} hodiny:
{%- for state in states.binary_sensor | selectattr('attributes.device_class','defined') | selectattr('attributes.device_class', '==', 'problem') -%}
{%- if state.state == 'on' -%}
{% if (as_timestamp(now()) - as_timestamp(state.last_changed)) | int(0) | round(0) > (threshold_hours*60*60) %}
- {{ state.name }}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
# https://www.home-assistant.io/integrations/template/
template:
- sensor:
- name: "Senzory s problémom (počet)"
unique_id: senzory_s_problemom_pocet
state: >-
{% set threshold_hours = 2 %}
{%- set dev = namespace(finalCount = 0) -%}
{%- for state in states.binary_sensor | selectattr('attributes.device_class','defined') | selectattr('attributes.device_class', '==', 'problem') -%}
{%- if state.state == 'on' -%}
{%- if (as_timestamp(now()) - as_timestamp(state.last_changed)) | int(0) | round(0) > (threshold_hours*60*60) -%}
{%- set dev.finalCount = (dev.finalCount + 1) -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{{- dev.finalCount -}}
@pavolholes
Copy link
Author

Changes in revision 4 & 5:

  • Fixed error in condition which was comparing the last_changed or the sensor with the treshold.
  • Added threshold to variable to the beginning of the template.
  • Added line breaks between sensors.

@pavolholes
Copy link
Author

Changes in revision 6:

  • Changed threshold_seconds to threshold_hours in both sensors.
  • Added threshold_hours to the notification message.

@pavolholes
Copy link
Author

CHanges in revision 7:

  • Finaly fixed the line breaks between sensors in notification (In developer tools > template it acts differently than in automation) - now every sensor is on new line.

@pavolholes
Copy link
Author

Changes in revision 8:

  • fixed threshold_hours condition math

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment