From c831770ac8f7fc574cf6ba4e4fc787de42343b3b Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 20 Apr 2025 02:30:32 +0200 Subject: [PATCH] Fixed home-assistant automations and scripts --- .../Notification to smart phone.yml | 39 ++++++ home-assistant/Person Detect Logic.yml | 97 ++++++++++++++ home-assistant/Person detect Actions.yml | 125 ++++++++++++++++++ ...nd Dynamic Notification To All Devices.yml | 107 +++++++++++++++ home-assistant/home-automation3.yml | 24 ++-- home-assistant/notification to android tv.yml | 24 ++++ 6 files changed, 404 insertions(+), 12 deletions(-) create mode 100644 home-assistant/Notification to smart phone.yml create mode 100644 home-assistant/Person Detect Logic.yml create mode 100644 home-assistant/Person detect Actions.yml create mode 100644 home-assistant/Send Dynamic Notification To All Devices.yml create mode 100644 home-assistant/notification to android tv.yml diff --git a/home-assistant/Notification to smart phone.yml b/home-assistant/Notification to smart phone.yml new file mode 100644 index 0000000..bbacdf5 --- /dev/null +++ b/home-assistant/Notification to smart phone.yml @@ -0,0 +1,39 @@ +# Automation version of notification +action: notify.mobile_app_sm_n986b +data_template: + message: A {{trigger.payload_json["after"]["label"]}} was detected. + title: Frigate + data: + image: >- + https://home-assistant.sthome.org/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/thumbnail.jpg?format=android + tag: "{{trigger.payload_json[\"after\"][\"id\"]}}" + when: "{{trigger.payload_json[\"after\"][\"start_time\"]|int}}" + actions: + - action: URI + title: Open Image + uri: >- + https://home-assistant.sthome.org/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/thumbnail.jpg?format=android +enabled: true + +# script version of notification +alias: Send Dynamic Notification To All Devices +sequence: + - data: + message: "{{ message }}" + title: "{{ title }}" + data: null + url: "{{ video }}" + tag: "{{ notifTag }}" + group: "{{ group }}" + attachment: + url: >- + {% if (importance == "default" and video != null) -%} {{ video }} {%- + else -%} {{ image }} {%- endif %} + push: + interruption-level: >- + {% if importance == "max" -%} time-sensitive {%- else -%} passive {%- + endif %} + action: notify.mobile_app_sm_n986b +mode: single +icon: mdi:bell-ring-outline +max: 10 \ No newline at end of file diff --git a/home-assistant/Person Detect Logic.yml b/home-assistant/Person Detect Logic.yml new file mode 100644 index 0000000..353773c --- /dev/null +++ b/home-assistant/Person Detect Logic.yml @@ -0,0 +1,97 @@ +alias: "FRIGATE: Person Detect Logic" +description: >- + This automation is used to send a notification when a person is detected by + Frigate. It uses the frigate MQTT event stream to trigger the notification and + includes a snapshot of the detected object. The notification will be sent to + all devices with the specified group and will include a video clip of the + detection if available. +triggers: + - topic: homeassistant/events + id: frigate-event + value_template: "{{ value_json[\"after\"][\"camera\"] }}" + variables: + after_zones: "{{ trigger.payload_json[\"after\"][\"entered_zones\"] }}" + before_zones: "{{ trigger.payload_json[\"before\"][\"entered_zones\"] }}" + camera: "{{ trigger.payload_json[\"after\"][\"camera\"] }}" + id: "{{ trigger.payload_json[\"after\"][\"id\"] }}" + label: "{{ trigger.payload_json[\"after\"][\"label\"] }}" + score: "{{ trigger.payload_json[\"after\"][\"score\"] }}" + time_clip_start: "{{ trigger.payload_json[\"after\"][\"start_time\"] - 10.0 }}" + trigger: mqtt +conditions: + - condition: or + conditions: + - condition: template + value_template: "{{ trigger.payload_json['type'] == 'new' }}" + - condition: template + value_template: "{{ before_zones | length == 0 }}" + enabled: false + - condition: template + value_template: "{{ trigger.payload_json[\"after\"][\"entered_zones\"]|length > 0 }}" + enabled: false +actions: + - choose: + - conditions: + - condition: trigger + id: frigate-event + sequence: + - action: script.person_in_driveway_detect_actions + data: + id: "{{ id }}" + title: >- + {{ label }} was detected on {{ camera | replace("_", " ") | + title }} + message: >- + {{ label }} detected in the {{ after_zones[0] | replace("_", " + ") | title }} + notifTag: "{{ id }}" + notifIcon: mdi:doorbell-video + group: frigate-notification-{{ camera }} + importance: max + image: /api/frigate/notifications/{{ id }}/snapshot.jpg + video: "" + clickAction: "" + camera: "{{ camera }}" + - repeat: + sequence: + - wait_for_trigger: + - topic: homeassistant/events + payload: "{{ trigger.payload_json[\"after\"][\"id\"] }}" + value_template: "{{ value_json[\"after\"][\"id\"] }}" + trigger: mqtt + continue_on_timeout: false + timeout: "00:02:00" + - condition: template + value_template: "{{ wait.trigger.payload_json['type'] == 'end' }}" + - action: script.person_in_driveway_detect_actions + data: + id: "{{ id }}" + title: > + {% if (wait.trigger.payload_json["after"]["sub_label"] != + None) -%} {{ + (wait.trigger.payload_json["after"]["sub_label"]) | title + }} {%- else -%} {{ label | replace("_", " ") | title }} + {%- endif %} was detected in the {{ after_zones[0] | + replace("_", " ") | title }} + message: >- + Detected with {{ int(score | round(2) * 100) }}% + confidence + notifTag: "{{ id }}" + notifIcon: mdi:doorbell-video + group: > + frigate-notification-{{ + trigger.payload_json["after"]["camera"] }} + importance: default + image: /api/frigate/notifications/{{ id }}/snapshot.jpg + video: >- + home-assistant.sthome.org/api/{{ id }}/{{ camera + }}/clip.mp4 + clickAction: > + https://home-assistant.sthome.org/api/frigate/notifications/{{id}}/{{camera}}/clip.mp4 + camera: "{{ camera }}" + until: + - condition: template + value_template: "{{ wait.trigger.payload_json[\"type\"] == \"end\" }}" + default: [] +mode: parallel +max: 10 diff --git a/home-assistant/Person detect Actions.yml b/home-assistant/Person detect Actions.yml new file mode 100644 index 0000000..ed715f6 --- /dev/null +++ b/home-assistant/Person detect Actions.yml @@ -0,0 +1,125 @@ +alias: Person detect Actions +description: "" +variables: + relay: null + counter: null +sequence: + - if: + - condition: template + value_template: "{{ camera == 'Driveway' }}" + then: + - variables: + relay: switch.sthome_ut1_relay_1 + counter: counter.driveway_person_alarm_counter + else: + - if: + - condition: template + value_template: "{{ camera == 'Patio' }}" + then: + - variables: + relay: switch.sthome_ut1_relay_2 + counter: counter.patio_person_alarm_counter + else: + - if: + - condition: template + value_template: "{{ camera == 'Westwall' }}" + then: + - variables: + relay: switch.sthome_ut1_relay_2 + counter: counter.west_wall_person_alarm_counter + else: + - if: + - condition: template + value_template: "{{ camera == 'Eastwall' }}" + then: + - variables: + relay: switch.sthome_ut1_relay_3 + counter: counter.east_wall_person_alarm_counter + else: + - if: + - condition: template + value_template: "{{ camera == 'Backyard' }}" + then: + - variables: + relay: switch.sthome_ut1_relay_4 + counter: counter.backyard_person_alarm_counter + else: + - stop: Unknown camera + - action: counter.increment + target: + entity_id: "{{ counter }}" + data: {} + - action: switch.turn_on + target: + entity_id: "{{ relay }}" + data: {} + - action: script.send_dynamic_notification_to_all_devices + data: + id: "{{ id }}" + title: "{{ title }}" + message: "{{ message }}" + notifTag: "{{ notifTag }}" + notifIcon: "{{ notifIcon }}" + group: "{{ group }}" + importance: "{{ importance }}" + image: "{{ image }}" + video: "{{ video }}" + clickAction: "{{ clickAction }}" + enabled: true +fields: + id: + selector: + text: null + name: id + required: true + title: + selector: + text: null + name: title + required: true + message: + selector: + text: null + name: message + required: true + notiftag: + selector: + text: null + name: notifTag + required: true + notificon: + selector: + text: null + name: notifIcon + required: true + group: + selector: + text: null + name: group + required: true + importance: + selector: + text: null + name: importance + required: true + image: + selector: + text: null + name: image + required: true + clickaction: + selector: + text: null + name: clickAction + required: true + video: + selector: + text: null + name: video + required: true + camera: + selector: + entity: {} + name: camera + description: Name of camera as defined in Frigate + required: true diff --git a/home-assistant/Send Dynamic Notification To All Devices.yml b/home-assistant/Send Dynamic Notification To All Devices.yml new file mode 100644 index 0000000..1f28c33 --- /dev/null +++ b/home-assistant/Send Dynamic Notification To All Devices.yml @@ -0,0 +1,107 @@ +alias: Send Dynamic Notification To All Devices +sequence: + - parallel: +# - if: +# - condition: template +# value_template: '{{ send_to_fold | default(True) }}' +# then: +# - data: +# message: '{{ message }}' +# title: '{{ title }}' +# data: +# channel: '{{ channel }}' +# clickAction: '{{ video }}' +# tag: '{{ notifTag }}' +# group: '{{ group }}' +# image: '{% if (importance == "default" and video != null) -%} {{ null +# }} {%- else -%} {{ image }} {%- endif %}' +# video: '{% if (importance == "default" and video != null) -%} {{ video +# }} {%- else -%} {{ null }} {%- endif %}' +# notification_icon: '{{ notifIcon }}' +# importance: '{{ importance }}' +# action: notify.mobile_app_fold + - data: + message: '{{ message }}' + title: '{{ title }}' + data: + url: '{{ video }}' + tag: '{{ notifTag }}' + group: '{{ group }}' + attachment: + url: '{% if (importance == "default" and video != null) -%} {{ video }} {%- else -%} {{ image }} {%- endif %}' + push: + interruption-level: '{% if importance == "max" -%} time-sensitive {%- else -%} passive {%- endif %}' + # must test above data sent to notify.mobile_app_sm_n986b and check whether the following data is needed + # actions: + # - action: URI + # title: Open Image + # uri: https://home-assistant.sthome.org/api/frigate/notifications/{{ id }}/thumbnail.jpg?format=android + action: notify.mobile_app_sm_n986b +# - if: +# - condition: template +# value_template: '{{ states("media_player.nova4k") != "off" }}' +# - condition: template +# value_template: '{{ importance == "max" }}' +# then: +# - data: +# message: '{{ message }}' +# title: '{{ title }}' +# data: +# fontsize: medium +# image: +# url: https://frigate.sthome.org/api/events/{{id}}/snapshot.jpg +# action: notify.nova_4k +mode: parallel +icon: mdi:bell-ring-outline +max: 10 +fields: + id: + selector: + text: null + name: id + required: true + title: + selector: + text: null + name: title + required: true + message: + selector: + text: null + name: message + required: true + notiftag: + selector: + text: null + name: notifTag + required: true + notificon: + selector: + text: null + name: notifIcon + required: true + group: + selector: + text: null + name: group + required: true + importance: + selector: + text: null + name: importance + required: true + image: + selector: + text: null + name: image + required: true + clickaction: + selector: + text: null + name: clickAction + required: true + video: + selector: + text: null + name: video + required: true \ No newline at end of file diff --git a/home-assistant/home-automation3.yml b/home-assistant/home-automation3.yml index 0ca1138..6baf354 100644 --- a/home-assistant/home-automation3.yml +++ b/home-assistant/home-automation3.yml @@ -1,11 +1,11 @@ -alias: Outside - Driveway & Front Yard - Frigate Event - Cat / Dog / Person Seen -description: '' +alias: Frigate Person Detect Notification +description: 'This automation is used to send a notification when a person is detected by Frigate. It uses the frigate MQTT event stream to trigger the notification and includes a snapshot of the detected object. The notification will be sent to all devices with the specified group and will include a video clip of the detection if available.' # The trigger is mqtt and will fire whenever an object is detected by YOUR_CAMERA_NAME trigger: - platform: mqtt - topic: frigate/events + topic: homeassistant/events id: frigate-event - payload: YOUR_CAMERA_NAME + payload: Driveway value_template: "{{ value_json['after']['camera'] }}" variables: after_zones: "{{ trigger.payload_json['after']['entered_zones'] }}" @@ -27,14 +27,14 @@ condition: # This ensures that the object at least has one zone, this can be removed if you don't care about zones - condition: template value_template: '{{ trigger.payload_json["after"]["entered_zones"]|length > 0 }}' - # This specifically checks for the object to be in the driveway or front_yard zone. This can be removed or those names changed to fit your use case. -for example: + # This specifically checks for the object to be in the specified zones. This can be removed or those names changed to fit your use case. +#for example: - condition: template value_template: >- - {{ ["driveway", "front_yard"] | select("in", + {{ ["driveway_z1", "Driveway_Paving"] | select("in", after_zones) | list | length > 0 }} - alias: Object is in Driveway or Front Yard - # This filters out any car objects as I have a separate automation with different behavior for car. This can be removed if not applicable to your use case. + alias: Object is in Driveway + # This filters out any car objects. This can be removed if not applicable to your use case. - condition: template value_template: '{{ trigger.payload_json["after"]["label"] != "car" }}' action: @@ -71,7 +71,7 @@ action: sequence: - wait_for_trigger: - platform: mqtt - topic: frigate/events + topic: homeassistant/events payload: "{{ trigger.payload_json[\"after\"][\"id\"] }}" value_template: "{{ value_json[\"after\"][\"id\"] }}" continue_on_timeout: false @@ -98,9 +98,9 @@ action: image: >- /api/frigate/notifications/{{ id }}/snapshot.jpg video: >- - HASS_URL_BASE/api/{{id}}/{{camera}}/clip.mp4 + home-assistant.sthome.org/api/{{id}}/{{camera}}/clip.mp4 clickAction: >- - http://HASS_URL_BASE/api/frigate/notifications/{{id}}/{{camera}}/clip.mp4 + https://home-assistant.sthome.org/api/frigate/notifications/{{id}}/{{camera}}/clip.mp4 default: [] mode: parallel max: 10 diff --git a/home-assistant/notification to android tv.yml b/home-assistant/notification to android tv.yml new file mode 100644 index 0000000..c2a3833 --- /dev/null +++ b/home-assistant/notification to android tv.yml @@ -0,0 +1,24 @@ +# NOT WORKING + +# Adding notify to Configuration.yaml file +# -------------------------------------------------------- +# open home-assistant configration.yaml file with nano +nano /mnt/SSD1/docker/data/home-assistant/config/configuration.yaml +# Add the following lines to the end of the file +notify: + - platform: androidtv + name: Android TV + host: nova4k.sthome.org + +# Checking the configuration and restarting home-assistant +# -------------------------------------------------------- +# In home-assistant, go to Developer Tools > YAML and click Check Configuration. +# If there are no errors, click Restart to apply the changes. + +# In home-assistant, go to Developer Tools > Services and select the notify service. +# In the Service Data field, enter the following JSON: +{ + "message": "Hello from Home Assistant!", + "title": "Test Notification", + "target": "Android TV" +} \ No newline at end of file