docker-apps/home-assistant/home-automation2.txt

172 lines
7.7 KiB
Plaintext

https://www.youtube.com/watch?v=gA67NCcTwms&t=621s
# Automation yaml file
--- snip ---
alias: "FRIGATE: Notify driveway zone1 person detection"
description: FRIGATE notify - driveway
triggers:
- topic: homeassistant/events
trigger: mqtt
conditions:
- condition: template
value_template: "{{ trigger.payload_json['after']['label'] == 'person' }}"
- condition: template
value_template: "{{ 'driveway_z1' in trigger.payload_json['after']['entered_zones']}}"
actions:
- 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
mode: single
--- snip ---
# Install home-assistant app on your android phone from the play store
# After installation and connection to you home-assistant server, on home-assistant server (on computer), navigate to Settings -> Devices & services -> Integrations. Look for your Mobile App in the list of integrations.
# To change the name of the device, click on the 3 dot menu next to the device name under Integration Entities and select Rename.
# open app on phone and from the Overview screen, click on hamburger menu -> Settings -> Companion app -> Notification channels
# With the device name now known, edit the yaml file above to reflect the device name, e.g.
# if device name is SM-N986B, then
- action: notify.mobile_app_samsung_sm_n986b
To create a script using GUI
----------------------------
Navigate to Settings -> Automations & Scenes -> Scripts
Click "+ CREATE SCRIPT"
Click Create New Script
To create script directly using yaml
------------------------------------
In truenas shell, go to config/scripts folder
# Create custom scripts folder to keep scripts organised
mkdir custom_scripts
# Create script file using nano editor, for example:
nano custom_scripts/send_dynamic_notification_to_all_devices.yaml
# Then edit scripts.yaml to include newly created script file
nano scripts.yaml
# Enter following line in scripts.yaml file, save and exit
script: !include_dir_merge_named custom_scripts/
-------------------------
alias: Outside - Driveway & Front Yard - Frigate Event - Cat / Dog / Person Seen
description: ''
# The trigger is mqtt and will fire whenever an object is detected by YOUR_CAMERA_NAME
trigger:
- platform: mqtt
topic: frigate/events
id: frigate-event
payload: YOUR_CAMERA_NAME
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 }}"
# Conditions must be used to filter out objects / detections that are not of interest
condition:
# This condition is used to ensure that I am only notified of objects that are new or only just entered a zone. This is to avoid getting loads of updated notifications for the same dog in my backyard (as an example).
- condition: or
conditions:
- condition: template
value_template: "{{ trigger.payload_json['type'] == 'new' }}"
- condition: template
value_template: "{{ before_zones | length == 0 }}"
# 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:
- condition: template
value_template: >-
{{ ["driveway", "front_yard"] | 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.
- condition: template
value_template: '{{ trigger.payload_json["after"]["label"] != "car" }}'
action:
- choose:
- conditions:
- condition: trigger
id: frigate-event
sequence:
# Send the initial notification with snapshot and info.
- service: script.send_dynamic_notification_to_all_devices
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
clickAction: ''
# Wait until the end of the notification has occurred.
- repeat:
until:
- condition: template
value_template: '{{ wait.trigger.payload_json["type"] == "end" }}'
sequence:
- wait_for_trigger:
- platform: mqtt
topic: frigate/events
payload: "{{ trigger.payload_json[\"after\"][\"id\"] }}"
value_template: "{{ value_json[\"after\"][\"id\"] }}"
continue_on_timeout: false
timeout: '00:02:00'
- condition: template
value_template: '{{ wait.trigger.payload_json[''type''] == ''end'' }}'
- service: script.send_dynamic_notification_to_all_devices
data:
id: '{{ id }}'
# Uses double take for title and message to include face info if a match is detected.
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: >-
HASS_URL_BASE/api/{{id}}/{{camera}}/clip.mp4
clickAction: >-
http://HASS_URL_BASE/api/frigate/notifications/{{id}}/{{camera}}/clip.mp4
default: []
mode: parallel
max: 10