Skip to content
This repository has been archived by the owner on Mar 19, 2023. It is now read-only.

Commit

Permalink
Adds automations
Browse files Browse the repository at this point in the history
  • Loading branch information
robmarkcole committed Jun 27, 2019
1 parent c6f0753 commit 264a431
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
5 changes: 5 additions & 0 deletions automations/README.md
@@ -0,0 +1,5 @@
## Automations
I am using a pair of automations with this component. The first is triggered by a PIR on my front door and kicks off a [python_script](https://www.home-assistant.io/components/python_script/) to save a time-stamped image from my blink camera and set-off image processing with this component.
The second automation is triggered by the `image_processing.file_saved` event that this component fires, and kicks off a python_script that sends the saved image via telegram. Note that I am using the `url` of the file as my Home Assistant instance is running in Docker and I cannot use a `file` path.

You might ask why I am using `python_scripts` rather than keeping all logic within `automations.yaml`. There are 2 reasons. (1) I prefer writing python over yaml, and (2) if is not necessary to do any reloading in HA as changes to `python_scripts` instantly take effect.
52 changes: 52 additions & 0 deletions automations/capture_timestamped_image.py
@@ -0,0 +1,52 @@
"""
Capture a timestamped camera image using the service camera.snapshot. This script is triggered by the following automation:
trigger:
- entity_id: binary_sensor.hue_front_porch_motion_sensor
from: 'off'
platform: state
to: 'on'
condition:
- after: sunrise
before: sunset
condition: sun
action:
- data: {}
service: python_script.capture_timestamped_image
"""
BLINK_SLEEP_TIME = 7 # seconds to wait for Blink
HA_SLEEP_TIME = 3 # seconds to wait for HA
CAMERA_ENTITY_ID = 'camera.blink_living_room'
CAMERA_NAME = 'Living_room'
IMAGE_PROCESSING_ENTITY_ID = 'image_processing.deepstack_object_blink_living_room'

now = datetime.datetime.now()
time_str = "{}_{}_{}_{}_{}_{}_{}".format(
now.year, now.month, now.day, now.hour,
now.minute, now.second, now.microsecond)

# Trigger a capture now
hass.services.call(
'blink', 'trigger_camera',
{'name': CAMERA_NAME})
time.sleep(BLINK_SLEEP_TIME)

# Update representation in HA
hass.services.call(
'blink', 'blink_update')
time.sleep(HA_SLEEP_TIME)

# Save using snapshot
folder = '/config/www/blink_{}_'.format(CAMERA_NAME)
filename = folder + time_str + '.jpg'

hass.services.call(
'camera', 'snapshot',
{'entity_id': CAMERA_ENTITY_ID,
'filename': filename})

## Call image processing
hass.services.call(
'image_processing', 'scan', {
"entity_id": IMAGE_PROCESSING_ENTITY_ID
})
26 changes: 26 additions & 0 deletions automations/process_deepstack_image.py
@@ -0,0 +1,26 @@
"""
Pass file_path into this script with automation:
- action:
- data_template:
file_path: "{{ trigger.event.data.file }}"
service: python_script.process_deepstack_image
alias: Process deepstack
condition: []
id: '1120092824611'
trigger:
- platform: event
event_type: image_processing.file_saved
"""

file_path = data.get('file_path')
file_name = file_path.split('/')[-1] # get just the file name e.g. img.jpg
web_path = "http://192.168.1.164:8123/local/deepstack_images/" + file_name
logger.info("Deepstack image web_path : {}".format(web_path))

hass.services.call(
'telegram_bot', 'send_photo', {
"caption": "{}".format(file_name),
"url": web_path}
)

0 comments on commit 264a431

Please sign in to comment.