Automation trigger.entity_id template issue

I have the following automations working as expected:

- alias: 'Send IP Cam photo - Livingroom'
  trigger:
    platform: state
    entity_id: binary_sensor.live__the_residentie__livingroom_motion_active
    from: 'off'
    to: 'on'
  condition:
    condition: state
    entity_id: device_tracker.bob
    state: 'not_home'
  action:
    service: notify.galaxy_s6_edge
    data:
      title: 'Home Assistant'
      message: 'Photo'
      data:
        photo:
          url: http://10.0.0.160:8080/photo.jpg
          username: xxxx
          password: xxxx
          caption: Motion detected in de Residentie Livingroom
- alias: 'Send IP Cam photo - Bedroom'
  trigger:
    platform: state
    entity_id: binary_sensor.live__the_residentie__bedroom_motion_active
    from: 'off'
    to: 'on'
  condition:
    condition: state
    entity_id: device_tracker.bob
    state: 'not_home'
  action:
    service: notify.galaxy_s6_edge
    data:
      title: 'Home Assistant'
      message: 'Photo'
      data:
        photo:
          url: http://10.0.0.30:8080/photo.jpg
          username: xxxx
          password: xxxx
          caption: Motion detected in de Residentie Bedroom

I would like to combine these in 1 automation but the following does not work:

- alias: 'Send IP Cam photo'
  trigger:
    - platform: state
      entity_id: binary_sensor.live__the_residentie__livingroom_motion_active
      from: 'off'
      to: 'on'
    - platform: state
      entity_id: binary_sensor.live__the_residentie__bedroom_motion_active
      from: 'off'
      to: 'on'      
  condition:
    condition: state
    entity_id: device_tracker.bob
    state: 'not_home'
  action:
    service: notify.galaxy_s6_edge
    data:
      title: 'Home Assistant'
      message: 'Photo'
      data_template:
        photo:
          url: >
            {% if is_state("trigger.entity_id", "binary_sensor.live__the_residentie__livingroom_motion_active") %} http://10.0.0.160:8080/photo.jpg
            {% elif is_state("trigger.entity_id", "binary_sensor.live__the_residentie__bedroom_motion_active") %} http://10.0.0.30:8080/photo.jpg
            {% endif %}
          username: xxxx
          password: xxxx
          caption: >
            {% if is_state("trigger.entity_id", "binary_sensor.live__the_residentie__livingroom_motion_active") %} Motion detected in de Residentie Livingroom
            {% elif is_state("trigger.entity_id", "binary_sensor.live__the_residentie__bedroom_motion_active") %} Motion detected in de Residentie Bedroom
            {% endif %}  

Can someone help me out?

Give this a go:

- alias: 'Send IP Cam photo'
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.live__the_residentie__livingroom_motion_active
        - binary_sensor.live__the_residentie__bedroom_motion_active
      from: 'off'
      to: 'on'
  condition:
    condition: state
    entity_id: device_tracker.bob
    state: 'not_home'
  action:
    service: notify.galaxy_s6_edge
    data:
      title: 'Home Assistant'
      message: 'Photo'
      data_template:
        photo:
          url: >
            {% if trigger.entity_id == "binary_sensor.live__the_residentie__livingroom_motion_active" %}http://10.0.0.160:8080/photo.jpg
            {% elif trigger.entity_id == "binary_sensor.live__the_residentie__bedroom_motion_active" %}http://10.0.0.30:8080/photo.jpg
            {% endif %}
          username: xxxx
          password: xxxx
          caption: >
            {% if trigger.entity_id == "binary_sensor.live__the_residentie__livingroom_motion_active" %}Motion detected in de Residentie Livingroom
            {% elif trigger.entity_id == "binary_sensor.live__the_residentie__bedroom_motion_active" %}Motion detected in de Residentie Bedroom
            {% endif %}

@fanaticDavid

Thanks.
If I change the code and run it manually it gives the following error in my log:

17-03-26 12:12:52 ERROR (MainThread) [homeassistant.core] Invalid service data for notify.galaxy_s6_edge: extra keys not allowed @ data['data_template']. Got OrderedDict([('photo', OrderedDict([('url', '{% if trigger.entity_id == "binary_sensor.live__the_residentie__livingroom_motion_active" %}http://10.0.0.160:8080/photo.jpg {% elif trigger.entity_id == "binary_sensor.live__the_residentie__bedroom_motion_active" %}http://10.0.0.30:8080/photo.jpg {% endif %}\n'), ('username', 'xxx'), ('password', xxxx), ('caption', '{% if trigger.entity_id == "binary_sensor.live__the_residentie__livingroom_motion_active" %}Motion detected in de Residentie Livi...

I’m not sure if this will be a problem since I triggered it manually through the frontend and therefore of course it’s missing the trigger.entity_id. However, the errorlog seems to indicate there is a syntax problem (?)

Oh, I see what I missed: data and data_template need to be switched around:

- alias: 'Send IP Cam photo'
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.live__the_residentie__livingroom_motion_active
        - binary_sensor.live__the_residentie__bedroom_motion_active
      from: 'off'
      to: 'on'
  condition:
    condition: state
    entity_id: device_tracker.bob
    state: 'not_home'
  action:
    service: notify.galaxy_s6_edge
    data_template:
      title: 'Home Assistant'
      message: 'Photo'
      data:
        photo:
          url: >
            {% if trigger.entity_id == "binary_sensor.live__the_residentie__livingroom_motion_active" %}http://10.0.0.160:8080/photo.jpg
            {% elif trigger.entity_id == "binary_sensor.live__the_residentie__bedroom_motion_active" %}http://10.0.0.30:8080/photo.jpg
            {% endif %}
          username: xxxx
          password: xxxx
          caption: >
            {% if trigger.entity_id == "binary_sensor.live__the_residentie__livingroom_motion_active" %}Motion detected in de Residentie Livingroom
            {% elif trigger.entity_id == "binary_sensor.live__the_residentie__bedroom_motion_active" %}Motion detected in de Residentie Bedroom
            {% endif %}


Hmmm

17-03-26 15:54:33 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 99, in async_render
    return self._compiled.render(kwargs).strip()
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/sandbox.py", line 385, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'trigger' is undefined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.4/asyncio/tasks.py", line 237, in _step
    result = next(coro)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/automation/__init__.py", line 305, in async_trigger
    yield from self._async_action(self.entity_id, variables)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/automation/__init__.py", line 386, in action
    yield from script_obj.async_run(variables)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/script.py", line 151, in async_run
    yield from self._async_call_service(action, variables)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/script.py", line 181, in _async_call_service
    self.hass, action, True, variables, validate_config=False)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 88, in async_call_from_config
    config[CONF_SERVICE_DATA_TEMPLATE]))
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 84, in _data_template_creator
    for key, item in value.items()}
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 84, in <dictcomp>
    for key, item in value.items()}
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 84, in _data_template_creator
    for key, item in value.items()}
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 84, in <dictcomp>
    for key, item in value.items()}
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 84, in _data_template_creator
    for key, item in value.items()}
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 84, in <dictcomp>
    for key, item in value.items()}
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 86, in _data_template_creator
    return value.async_render(variables)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 101, in async_render
    raise TemplateError(err)
homeassistant.exceptions.TemplateError: UndefinedError: 'trigger' is undefined

Perhaps this is caused due to the missing trigger.entity_id. I will post back what happens when motion is detected while i’m not at home.

@fanaticDavid I can confirm your suggestion works, thanks!

1 Like

Awesome! :grinning: