Template notifications

Hi all,

I’m trying to create a dynamic HTML5 notification via automation templating. I’m having a tough time. What I want to do is whenever any of my windows open I want a notification saying “[window name] opened”. Simple enough if I want to use the entity ID as the window name (as in the first commented out message), but I’m not liking that as that means I would get “sensor.bathroom_window_sensor opened” (if my bathroom window was the one to have opened of course). That’s too fugly for me. I’d rather have “Bathroom Window opened”, so I tried grabbing the attribute of the entity by using .friendly_name (as in the second commented out message) and that just returns " opened". So then I tried using an if statement, which leads to the third message, but that just hits the else condition for some unknown reason.

I hope I just made sense, it’s really late. Anyone have any ideas on how to solve either the second or third methods? I’d really appreciate it.

- action:
  - data_template:
#      message: '{{trigger.entity_id}} opened'
#      message: '{{trigger.entity_id.friendly_name}} opened'
      message: >
        {% if "{{trigger.entity_id}}" == "sensor.bathroom_window_sensor" %}
          Bathroom window opened
        {% else %}
          Window opened
        {% endif %}
    service: notify.notify
  alias: Window open test
  id: '1509252015750'
  - entity_id: sensor.bathroom_window_sensor
    platform: state
    to: Open
  - entity_id: sensor.family_room_window__left_sensor
    platform: state
    to: Open
  - entity_id: sensor.family_room_window__middle_sensor
    platform: state
    to: Open
  - entity_id: sensor.family_room_window__right_sensor
    platform: state
    to: Open

Attributes are not passed along with the trigger info.
You will find a couple of implementations that work around that problem in this thread:

The best method appears to be a loop over the entity list looking for a match on your entity_id

  message: >-
    {%- for state in states.sensor if state.entity_id == trigger.entity_id -%}
      {{ state.attributes.friendly_name }} opened.
    {%- endfor -%}

I have a similar automation that alerts me when either the front or back door is open, but only at night. I also have the logic to report to me when the home security system is ON, and someone opens the door. It has the code to convert from entity_ids to friendly name in there as well.

Here is the code:

Good luck!

How is the below line working? Based on the comment above yours attributes do not pass to the trigger state.
trigger.to_state.attributes.friendly_name.replace(‘Sensor’, ‘’)

Thank you treno! Looks like that will work, I’ll give it a try. I wish attributes were passed along with the trigger info though.

No need to just wish. It’s an open source project. Do a Pull Request. Or, if coding is not your thing, do a feature request, and get the community to vote for it.

Good idea! I used to be a developer so maybe when I find time I’ll have to check it out.

It works perfectly fine.

Depending upon the trigger you use, you have different types of data that gets passed to you. Since I used a state trigger in my code, I get both from_state and to_state. They are basically State objects, and the object has attributes that you can access. friendly_name is one of the attributes of State object.

Not sure what you meant by Attributes are not passed to triggers… there are some triggers that you have access to. Here is the link if you want to know what is available for you as the trigger data.

Being that you used from_state and to_state that explains why you can use .attributes. When using trigger.entitiy_id you cannot use .attributes to grab the friendly name.

I see your code above, you are using platform: state with triggers based on state change of an entity, and you should be able to access attributes directly using the {{ trigger.to_state.attributes.friendly_name }} approach. Please try it out.

1 Like

Oh man of course! That does work, such a simple fix. Thank you!

1 Like