I have HASS send me an email (via SMTP notify) with a photo from a camera when there is movement at home, but no-one from the family (a group of devices) is detected as home.
The cool thing (IMHO) is that the automation builds a list of states of devices (from a group called occupancy) and builds the list in html table format - which makes it present very nicely in the email when I get it.
code below:
- bar_snapshot shell command grabs snapshot from my generic IP camera and “converts” it to be valid for use in an email
- group occupancy (cut down for publication) is the entities I am interested in for testing occupancy
- the automation runs when no-one is home (per the family device-tracker group) and sends the status and last changed timestamps for each entity in the group in a nice html table format.
Only issue is I cant get the last_changed attribute converted to local time - it is always UTC!
Hope someone can make use of it!
If you want to play along at home, grab the loop from the automation and enter it in the templates developer section changing the group occupancy to one that exists in your HASS instance:
{%- for entity_id in states.group.occupancy.attributes.entity_id -%}
{% set parts = entity_id.split('.') -%}
<tr><td>{{ states[parts[0]][parts[1]].attributes.device_class }} </td><td>{{ states[parts[0]][parts[1]].name }} </td><td> {{states(entity_id)}} </td><td> {{(states[parts[0]][parts[1]].last_changed | timestamp_local ).strftime("%a, %d %b %Y %H:%M:%S") }}</td></tr>
{%- endfor %}
All my relevant config:
shell_command:
bar_snapshot: "wget -q -O /home/pi/.homeassistant/snapshots/bar.jpg http://10.1.1.xxx:8123/api/camera_proxy/camera.barcam?api_password=mypwd && convert /home/pi/.homeassistant/snapshots/bar.jpg /home/pi/.homeassistant/snapshots/bar.jpg"
group:
occupancy:
view: no
entities:
- binary_sensor.door_window_sensor_158d000149a219
- binary_sensor.door_window_sensor_158d00016c80f8
automation:
- alias: 'Movment at home'
trigger:
- platform: state
entity_id: binary_sensor.motion_sensor_158d0001231a61, binary_sensor.door_window_sensor_158d0001a67cef, binary_sensor.motion_sensor_158d00012319de
to: 'on'
condition:
- condition: state
entity_id: group.family
state: not_home
action:
- service: shell_command.bar_snapshot
- delay:
seconds: 2
- service: notify.phil_work
data:
title: "Movement at home"
message: >
Movement no family {{now().strftime("%a, %d %b %Y %H:%M:%S")}} <br />
<table><th> sensor status: </th>
{%- for entity_id in states.group.occupancy.attributes.entity_id -%}
{% set parts = entity_id.split('.') -%}
<tr><td>{{ states[parts[0]][parts[1]].attributes.device_class }} </td><td>{{ states[parts[0]][parts[1]].name }} </td><td> {{states(entity_id)}} </td><td> {{(states[parts[0]][parts[1]].last_changed | timestamp_local ).strftime("%a, %d %b %Y %H:%M:%S") }}</td></tr>
{%- endfor %}
</table>
data:
images:
- /home/pi/.homeassistant/snapshots/bar.jpg