Issue with trigger.to_state in automation -> UndefinedError: 'trigger' is undefined

Hi,
I’ve got

- alias: Devices disconnected
  trigger:
    platform: state
    entity_id:
    - device_tracker.ipcam1
    - device_tracker.ipcam2
    from: 'home'
    to: 'not_home'
    for:
      minutes: 1
  action:
    service: notify.ha_telegram
    data:
      message: "{{ trigger.to_state.attributes.friendly_name }} is now {{ trigger.to_state.state }} since a few minutes"

and I am getting the error below when a device goes offline

jinja2.exceptions.UndefinedError: ‘trigger’ is undefined

I’m not triggering it manually, I just disconnected one of the devices to test the automation.

Any idea of what could be wrong ?
Thanks

1 Like

I Think its to do with the formating

- alias: Devices disconnected
  trigger:
    platform: state
    entity_id:
      - device_tracker.ipcam1
      - device_tracker.ipcam2
    from: 'home'
    to: 'not_home'
    for:
      minutes: 1
  action:

I think the

- device_tracker.ipcam1
- device_tracker.ipcam2

need 2 more spaces in the line

Hi Myle, I’ve tried and got the exact same error.

This is the full log, when the automation gets triggered

2018-06-10 18:28:16 INFO (MainThread) [homeassistant.components.automation] Executing Devices disconnected
2018-06-10 18:28:16 INFO (MainThread) [homeassistant.core] Bus:Handling <Event logbook_entry[L]: name=Devices disconnected, message=has been triggered, domain=automation, entity_id=automation.devices_disconnected>
2018-06-10 18:28:16 INFO (MainThread) [homeassistant.helpers.script] Script Devices disconnected: Running script
2018-06-10 18:28:16 INFO (MainThread) [homeassistant.helpers.script] Script Devices disconnected: Executing step call service
2018-06-10 18:28:16 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=notify, service=ha_telegram, service_data=message={{ trigger.to_state.attributes.friendly_name }} is now {{ trigger.to_state.state }} since a few minutes, service_call_id=1972315152-5>
2018-06-10 18:28:16 ERROR (MainThread) [homeassistant.core] Error executing service <ServiceCall notify.ha_telegram: message=<homeassistant.helpers.template.Template object at 0x701bd7f0>>
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/template.py", line 132, in async_render
    return self._compiled.render(kwargs).strip()
  File "/usr/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/usr/lib/python3.6/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.6/site-packages/homeassistant/core.py", line 1007, in _event_to_service_call
    await service_handler.func(service_call)
  File "/usr/lib/python3.6/site-packages/homeassistant/components/notify/__init__.py", line 134, in async_notify_message
    kwargs[ATTR_MESSAGE] = message.async_render()
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/template.py", line 134, in async_render
    raise TemplateError(err)
homeassistant.exceptions.TemplateError: UndefinedError: 'trigger' is undefined

I tried

"{{ trigger.to_state.attributes.friendly_name }}"
"{{ trigger.to_state.state }}"
"{{ trigger.to_state.attributes }}"

all 3 give

jinja2.exceptions.UndefinedError: 'trigger' is undefined

And I also tried to replace

from: 'home'
to: 'not_home'
for:
  minutes: 1

with

to: 'not_home'

Same issue


You need data_template instead of data in the action.
2 Likes

Genius ! That worked.
Thanks.

Best part of the documentation :

Remembering these simple rules will help save you from many headaches and endless hours of frustration when using automation templates.

Hi
I also have:

- alias: 'SensoresPOW'
  trigger:
    - platform: state
      entity_id: 
        - sensor.pow001
        - sensor.pow002
        - sensor.pow003
        - sensor.pow004
        - sensor.pow005
  condition:
    - condition: template
      value_template: '{{ trigger.to_state.state != trigger.from_state.state }}'
#      to: 'on'     
  action:
    service: notify.NotifySensors
    data_template: 
      message: >
          {% set data = as_timestamp(now()) | timestamp_custom("%d/%m/%y", True) %}
          {% set time = as_timestamp(now()) | timestamp_custom("%H:%M:%S", 0) %}
          {% set sens = trigger.to_state.attributes.friendly_name %}
          {% set stat = trigger.to_state.state %}
          {{ data }};{{ time }};{{ sens }};{{ stat }}

and the same error: ‘trigger’ is undefined.

What am I doing wrong Can anyone help me!
Thanks.

Did you resolve?
I have the same problem.
Thanks!

Hi

I didn’t found the solution.
I have not solved the problem yet. I had to solve other more important situations. is not forgotten is in the “to do”

1 Like

Hi

Remove the .attributes. from your message

This is my code:

  - alias: "NOTIFY: Offline GPS Device"
    initial_state: true
    trigger:
      platform: state
      entity_id:
        - group.device_tracker_location
      from: "online"
    action:
      - service: automation.trigger
        entity_id: automation.snapshot_device_location
      - service: notify.jeeves_telegram
        data_template:
          message: "{{ trigger.to_state.name }} Status change: {{ trigger.to_state.state }}"

Note: my sensor does not have a friendly_name and thus I’m using “name”

Note2: Remember to always have data_template: when using automaion with states and attributes.

This is my result:
image

Please also remember that when you manually fire off the automation the trigger values will not be available!

4 Likes