Hi. I’ve tried lots of different versions and looking here…
I have a single automation with multiple motion detectors that sends an iOS notification, and I want to include the camera image based on which detector detected. I can do the text, not the camera entity.
I can send the camera entity hard-coded (either one works).
But I can’t figure out how to dynamically set the entity_id of the correct camera.
Here’s what I’ve got:
- alias: 'Notify for unexpected motion'
trigger:
# - platform: state
# entity_id: binary_sensor.motion_living
# to: 'on'
- platform: state
entity_id: binary_sensor.motion_hallway
to: 'on'
- platform: state
entity_id: binary_sensor.garage_tablet_motion_active
to: 'on'
condition:
condition: and
conditions:
# - condition: state
# entity_id: group.all_devices
# state: not_home
- condition: state
entity_id: input_boolean.send_notifications
state: 'on'
- condition: state
entity_id: timer.wait_motion
state: idle
action:
- service: notify.ios_lindsaysiphone
data:
title: ""
message: >-
Motion detected in the
{%- if is_state('binary_sensor.motion_living', 'on') %} living room
{% elif is_state('binary_sensor.motion_hallway', 'on') %} hallway
{% else %} garage
{% endif %}
data:
push:
category: camera
# entity_id: camera.garage_tablet # JUST like this without the lines below works fine, by the way
data_template:
entity_id: >-
{% if is_state('binary_sensor.garage_tablet_motion_active', 'on') %}
camera.garage_tablet
{% else %}
camera.kitchen
{% endif %}
I’ve gotten various issues from various attempts, but currently I get No entity_id found in payload!
When I was using template code but not data_template, I got something about not a valid URL.
I’m wondering if it’s not templatable?
I’ve tried just entity_id without the data_template and I’ve tried it all on one line…
Also, is there a way to find out what the actual result is? Like how you can view source on a web page to see what code your code produced, not just the rendering of it? I’ve verified that the template bit itself does what I want in the frontend template viewer dev tool.
Thanks heaps for being the first to reply
I wish to include a camera image in my notification. I’m not sure that this can be achieved with custom UI or entity_picture. They seem unrelated to me, but let me know if I’m missing something.
Thanks for your reply!
I have tried your suggestions…
The first one still results in a message with “no entity_id in payload”.
The second one gives me an error in the logs: Invalid service data for notify.ios_lindsaysiphone: extra keys not allowed @ data['data_template]
I’m not sure what your second data/entity_id is for… Can you explain why you’ve suggested putting the camera entity_id in twice (once in push and once in data)?
In both versions, I’ve tried with and without the extra data block.
Automations can tell you what entity triggered them using the trigger variable which is available to the automation. I use it in the below example to identify what triggered the automation and note it in my email notification (if trigger is null, I set off the automation manually from the dashboard).
Then the message gets a snapshot image added (I only have 1 camera) and a list of all sensors from a group that are in the on state.
early version of the automation discussed here (but before I learned about the trigger variable)
- alias: 'Movment at home'
trigger:
- platform: state
entity_id: binary_sensor.motion_sensor_1, binary_sensor.motion_sensor_2
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_template:
title: >
Movement at home.
{% if trigger is defined %}
{% set trigent = trigger.entity_id.split('.') %}
Triggered by: {{states[trigent[0]][trigent[1]].name}}
{% else %}
Triggered manually?
{% endif %}
data:
images:
- /home/pi/.homeassistant/snapshots/bar.jpg
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('.') -%}
{% if states[parts[0]][parts[1]].state == 'on' %}
<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>
{%- endif %}
{%- endfor %}
</table>
So here’s the final action part. I moved data_template up, as suggested by @petro and I unindented the entity_id as implied by @JTPublic’s example.
Thanks to all who helped!
action:
- service: notify.ios_lindsaysiphone
data_template:
title: ""
message: >-
Motion detected in the
{%- if is_state('binary_sensor.motion_living', 'on') %} living room
{% elif is_state('binary_sensor.motion_hallway', 'on') %} hallway
{% else %} garage
{% endif %}
data:
push:
category: camera
entity_id: >-
{% if is_state('binary_sensor.garage_tablet_motion_active', 'on') %}
camera.garage_tablet
{% else %}
camera.kitchen
{% endif %}