I am trying to use a template to send a pushbullet notification when a device tracker status has changed. I followed the documentation but I cannot get this automation to run, it keeps giving me a ‘trigger’ is undefined error?
alias: "My Status Changed"
trigger:
- platform: state
entity_id: device_tracker.removed_key
action:
- service: notify.my_pushbullet
data_template:
title: "My Status Changed"
message: >
I am now {{ trigger.to_state.state }}
Thanks for the example, it does work but unfortunately it doesn’t help solve my issue as to why the trigger variable is undefined. I based my code on the first example from this page. The only difference I can tell is that the examples service is notify.notify and mine is notify.my_pushbullet.
Ok, I’ve update my code to more closely resemble the example. This is how it now looks:
trigger:
platform: state
entity_id: device_tracker.removed_key
action:
service: notify.my_pushbullet
data_template:
message: >
Paulus just changed from {{ trigger.from_state.state }}
to {{ trigger.to_state.state }}
It still gives the trigger is undefined error. Not sure if it makes a difference but all my automations are separate yaml files in an automation folder referenced from the configurations.yaml file.
I’ve updated my code but now I get an ‘automation’ is undefined error instead.
My Code:
alias: Test notification
trigger:
- platform: state
entity_id: device_tracker.removed_key
action:
service: notify.my_pushbullet
data_template:
message: >
Paulus just changed from {{ automation.test_notification.from_state.state }}
to {{ automation.test_notification.to_state.state }}
Error log:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/homeassistant/helpers/template.py", line 62, in render
}).render(kwargs).strip()
File "/usr/local/lib/python3.4/dist-packages/jinja2/environment.py", line 989, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/local/lib/python3.4/dist-packages/jinja2/environment.py", line 754, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.4/dist-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "<template>", line 1, in top-level template code
File "/usr/local/lib/python3.4/dist-packages/jinja2/sandbox.py", line 329, in getattr
value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'automation' is undefined
Apparently it must not like my device tracker entity, because I switched the entity to my kitchen light switch and now it works?
alias: Test notification
trigger:
- platform: state
# entity_id: device_tracker.removed_key
entity_id: switch.kitchen_lights_switch_4
action:
- service: notify.my_pushbullet
data_template:
message: i am now {{ trigger.to_state.state }}