Hi, I am trying to format a slack notification in an automation. I have implemented the device tracker, person and slack component. They all work as expected.
My configuration:
- devicetracker.yaml
- person.yaml
- notify.yaml (slack)
The idea is to send a slack notification ones someone leaves or enters a zone.
I am able to send a slack notification and this message already contains:
- date & time of the event
- name of the person
- name of the zone
- an attachment with:
- a color (currently green)
- the latitude & longitude (when every available)
- a link with the location: http://maps.google.com/maps?q=latitude,longitude
See code & screenshot below:
automation:
# tracking notifications
- alias: name entered zone
trigger:
platform: state
entity_id: person.ted, person.annelies, person.stijn, person.naud
action:
service: notify.slack
data_template:
message: '{{now().strftime("%Y-%m-%d %H:%M:%S")}}: {{ trigger.to_state.name }} is in zone {{
trigger.to_state.state }}'
target: '#zones'
data: {
"attachments": [
{
"color": "#36a64f",
"title": "latitude: {{ trigger.to_state.attributes.latitude }}, longitude: {{
trigger.to_state.attributes.longitude }}.",
"title_link": "http://maps.google.com/maps?q={{ trigger.to_state.attributes.latitude }},{{
trigger.to_state.attributes.longitude }}",
}
]
}
This is how it currently looks like:
I am working on some improvements. I have the following questions:
-
I would like to change the color of the attachment, depending on the state (home or not_home or some other zone). How can I do that withing the json data message?
-
I would like to leave out the attachment from the slack message in case there is no latitude/longitude available. How can I do that?
-
I would like to add a secret default latitude value {{ trigger.to_state.attributes.latitude or 99.99999 }} like this: {{ trigger.to_state.attributes.latitude or !secret latitude }}. The example with 99.99999 works, but with the
!secret latitude
does not:
Invalid config for [automation]: invalid template
(TemplateSyntaxError: unexpected char '!' at 53)
for dictionary value @ data['action'][0]['data_template']['data'].
Got None. (See /config/configuration.yaml, line 2).
Please check the docs at https://home-assistant.io/components/automation/
Would a secret value inside a jinja value possible? How can I do that?
- Notifications keep coming in, even when a person stays in the same zone (see screenshot). What can I do to prevent that?
Any suggestions are welcome!