Placing the device that triggered an automation in the notification sent

I have worked with other software that handles notification and in doing so, there was always a way to add variables into the notification message to indicate what triggered the message. I got to assume this function is built into HA.

How would I craft that message body or title with the device that triggered the automation along with the state of the device. For example, if the kitchen window triggered my automation, I would like to know my kitchen window did it because the window was opened.

A variable for “time” and “date” would be awesome but I would think the time I received the notification should be close. But, it would be nice to have it in the message in case it was a while before I got to it.

In the other software I have messed with, it was always something like %devicename% or %trigger% or %time%, etc.

Thank you in advance.

Then you can build your notification with Jinja (if trigger variable contains what you need)

- service: notify.mobile_app_yourphone
  data:
    title: Some meaningfull title
    message: '{{ trigger.entity_id }}, Date: {{ now().strftime('%Y-%m-%d') }}, Time: {{ now().strftime('%H:%M') }}'

You can also put an id to each trigger and use the trigger.id then.

P.S.: Not tested, I’m at work :slight_smile:

I think you are suing that YAML in the file. I build my notifications in the newer automation setup.

I do too.
But using yaml for one action did not kill anyone :slight_smile:

1 Like

Well, it did not like what I put in on the screen shot I posted…very mad at me for that.

It is a different template (and in French) but this is from my config

1 Like

Is there anyway someone can tell me how to get this done? In the automation, I have a box for Target and another one for Data. I just do not understand where to add this.

Paste this into the Data field in your screenshot (doesn’t matter that it’s two rows):

title: Your title
message: '{{ trigger.entity_id }}, Date: {{ now().strftime('%Y-%m-%d') }}, Time: {{ now().strftime('%H:%M') }}'

Should work. :slight_smile:

It did not like that:

I’m sorry, I should really work with the UI editor some time…

So, let’s go!

Put this

{{ trigger.entity_id }}, Date: {{ now().strftime('%Y-%m-%d') }}, Time: {{ now().strftime('%H:%M') }}

under message.

Check the box in front of Title and add your wanted title.

Now I’m sure it should work. :+1:

2 Likes

I don’t know how you tested it but be advised that you cannot test an automation using its Run command if it contains a reference to the trigger object (such as trigger.entity_id).

The trigger object is defined only when the automation is triggered by one of its triggers (and will be undefined if you execute the automation via the Run command).

2 Likes

I am guessing I should ignore the “isual editor is not supported for this configuration” error. Here is what it gave me. Step 1 was add the title and then step 2 your code. It looks like it created the title entry in there once I added the message text. Based on Taras’s reply, I may have to wait for one to trigger. Thank you all, until I can confirm…and then I am sure it is still Thank you, all.

Yep, this error message you can ignore, these kind of things can’t be done via UI, it needs some YAML code. And Taras is right (as usual :rofl:), you need to wait for a trigger.

And btw. this wasn’t my code, it’s from @Olivier1974s post from above. :slight_smile: I only showed how to get it into the UI, with one missed “shot”. :rofl: :rofl:

Let us know, if it works ok, or if there are more things to do. :+1:

PS: if it works, please mark Oliviers first answer from above as solution, so others can find it in the future. Thanks! :slight_smile:

1 Like

Thank you, all. I set my “Alarm:ON” and “Alarm:OFF” automations that place my home into those modes based on whether or not I am home to use the notification you gave me and both indicated that my cell phone left the area at the date and time it did so I can safely assume that should I get an intrusion or smoke/CO trips, I will be getting those notices.

I am an engineer…not a programmer. But, I am learning.