Presence Notification Automation

Hey, I’ve had this automation for a while not and its just stopped working and i don’t remember changing anything.

Below is the yaml and the error. Help would be appreciated.

Thanks

#April just changed state
  - id: 'april_changed'
    alias: April Changed Notification
    trigger:
      platform: state
      entity_id: person.april
    condition:
      condition: template
      value_template: '{{ trigger.from_state.state != trigger.to_state.state }}'
    action:
    - service: notify.mobile_app_stephen_s20
      data:
        title: April just changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}.
        message: ''
        data:
          clickAction: /lovelace/device_tracking
    - service: notify.alexa_media
      data_template:
        target: 
          - media_player.everywhere_echo
        data:
          type: announce
        message: April just changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}.
2020-06-18 19:47:44 ERROR (MainThread) [homeassistant.components.automation] April Changed Notification: Error executing script. Error rendering template for call_service at pos 1: UndefinedError: 'trigger' is undefined

In the two service calls change data: to data_template:.

I just noticed that there should be a data_template BUT i changed it and it still doesn’t work. I now get a different error.

action:
    - service: notify.mobile_app_stephen_s20
      data:

new error

2020-06-18 20:05:16 ERROR (MainThread) [homeassistant.components.automation] April Changed Notification: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: 'trigger' is undefined
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 228, in async_render
    return compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.7/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.7/site-packages/jinja2/sandbox.py", line 407, 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/src/homeassistant/homeassistant/helpers/service.py", line 132, in async_prepare_call_from_config
    template.render_complex(config[CONF_SERVICE_DATA_TEMPLATE], variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 72, in render_complex
    return {key: render_complex(item, variables) for key, item in value.items()}
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 72, in <dictcomp>
    return {key: render_complex(item, variables) for key, item in value.items()}
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 74, in render_complex
    return value.async_render(variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 230, in async_render
    raise TemplateError(err)
homeassistant.exceptions.TemplateError: UndefinedError: 'trigger' is undefined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 153, in _async_step
    self, f"_async_{cv.determine_script_action(self._action)}_step"
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 656, in _async_call_service_step
    *self._prep_call_service_step(), blocking=True, context=self._context
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 252, in _prep_call_service_step
    return async_prepare_call_from_config(self._hass, self._action, self._variables)
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 135, in async_prepare_call_from_config
    raise HomeAssistantError(f"Error rendering data template: {ex}") from ex
homeassistant.exceptions.HomeAssistantError: Error rendering data template: UndefinedError: 'trigger' is undefined

Are you executing this from the ui? If so, how do you expect the trigger object to be populated?

1 Like

Yep, besides the two places where data_template is needed, what @anon43302295 said.

To properly test, you’d have to get person.april to change state, either normally, or by forcing a change via the STATES tab of the Developer Tools page.

so to clarify the below should now work if it were to happened as a result of actual state change but not manually triggered through me executing the automation?

  - id: 'april_changed'
    alias: April Changed Notification
    trigger:
      platform: state
      entity_id: person.april
    condition:
      condition: template
      value_template: '{{ trigger.from_state.state != trigger.to_state.state }}'
    action:
    - service: notify.mobile_app_stephen_s20
      data_template:
        title: April just changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}.
        message: ''
        data:
          clickAction: /lovelace/device_tracking
    - service: notify.alexa_media
      data_template:
        target: 
          - media_player.everywhere_echo
        data_template:
          type: announce
        message: April just changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}.

One too many changes of data to data_template.

:sob:

so…

  - id: 'april_changed'
    alias: April Changed Notification
    trigger:
      platform: state
      entity_id: person.april
    condition:
      condition: template
      value_template: '{{ trigger.from_state.state != trigger.to_state.state }}'
    action:
    - service: notify.mobile_app_stephen_s20
      data_template:
        title: April just changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}.
        message: ''
        data:
          clickAction: /lovelace/device_tracking
    - service: notify.alexa_media
      data_template:
        target: 
          - media_player.everywhere_echo
        data:
          type: announce
        message: April just changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}.

That looks better. And try changing the state of person.april via the STATES page to see if it works.

Thanks you kindly!

1 Like