Dynamic (template) entity_id for iOS camera notification?

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.

How can this be done? Thanks in advance :slight_smile:

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.

I guess no one knows about this?
@robbiet480 - can you comment on whether this is possible and/or what I need to do?
Thanks.

I’m starting to think I might be the only one with this problem… or no one has any ideas? :slight_smile:

Did you try custom ui, perhaps you can template the entity picture, you should be able to. If not, I think you could change the theme.

light.kitchen:
  templates:
    # Uses global variable - might be slow.
    entity_picture: >
      if (entities['light.bedroom'].state === 'on') return '/local/picture.gif'; else return '/local/picture2.gif';

Thanks heaps for being the first to reply :+1:
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.

Every few days I check here to see if anyone else can help with this…
@robbiet480 @balloob…?

Did you try moving your data_template up a level?

  action:
  - service: notify.ios_lindsaysiphone
    data_template: # <--------CHANGED------------
      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 %}
        data: # <--------CHANGED------------
          entity_id: >-
            {% if is_state('binary_sensor.garage_tablet_motion_active', 'on') %}
              camera.garage_tablet
            {% else %}
              camera.kitchen
            {% endif %}

or

  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_template: # <--------CHANGED------------
        push:
          category: camera
          entity_id: >-
            {% if is_state('binary_sensor.garage_tablet_motion_active', 'on') %}
              camera.garage_tablet
            {% else %}
              camera.kitchen
            {% endif %}
        data: # <--------CHANGED------------
          entity_id: >-
            {% if is_state('binary_sensor.garage_tablet_motion_active', 'on') %}
              camera.garage_tablet
            {% else %}
              camera.kitchen
            {% endif %}
1 Like

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.

Thanks again… I hope we’re getting closer :slight_smile:

This works for my iOS notification, the triggering sensor has the same name as the camera:

  • service: notify.ios_iphone
    data_template:
    message: “{{ states(‘sensor.local_time’) }} Garage {{ trigger.to_state.state }}”
    data:
    push:
    category: camera
    entity_id: camera.{{ trigger.entity_id.split(’.’)[1] }}

Thanks. Can you use code blocks so I can see your indenting please?

Here it is.

  - service: notify.ios_johnnys_iphone
    data_template: 
      message: "{{ states('sensor.local_time') }} Garage {{ trigger.to_state.state }}"
      data:
        push:
          category: camera
        entity_id: camera.{{ trigger.entity_id.split('.')[1] }}

I’ve been trying to put all this together from this help and various examples, including https://home-assistant.io/docs/ecosystem/ios/notifications/content_extensions/
It turns out entity_id needs to be under data, not under push (which seems odd to me).

I had a similar issue.

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 %}
1 Like