Struggling with templates

I’m trying to refactor and simplify some of my YAML. Templates have always been my nemesis so I’m forcing myself to use them more and more so I can better understand it.

Simple automation, let me know if doors are opening inside of the house while no one is here. Easy with separate automation or even the new Trigger conditions and trigger IDs but I think it would be neatest to use templates so I have dynamic response notifications based on what door opens. I just can’t figure out how to set up the template to get the friendly name of the door sensor.

- id: Notifyifanypedestriandoorsareopenwhennooneishome
  alias: Notify if any pedestrian doors are opened when noone is home
  initial_state: true
  trigger:
    platform: state
    entity_id:
      - binary_sensor.door_window_sensor_158d0006d30679
      - binary_sensor.door_window_sensor_158d0006d30781
      - binary_sensor.door_window_sensor_158d0006d3079c
    from: "off"  
    to: "on"
  condition:
    condition: state
    entity_id: input_select.occupancy
    state: "False"
  action:
    service: notify.pushbullet
    data_template:
      message: "The {{ trigger.attributes.friendly_name }} opened while noone was home" ### WTF happens here?
      target:
        - email/***@***.***

Thanks in advance

message: "The {{ trigger.to_state.name }} opened while noone was home"

If you want to read more about the options available:

State trigger variables:

State Objects:

1 Like

Thanks for that. I gave it a read but I do better going over practical examples. Think I have wrapped my head around it.

Probably not worth opening another thread but the same templating issue, now just trying to pull the state name out of the condition responsible.

- id: Notifyifanypedestriandoorsareleftopenwhennooneishome
  alias: Notify if any pedestrian doors are left open when noone is home
  initial_state: true
  trigger:
    platform: state
    entity_id: input_select.occupancy
    from: "True"
    to: "False"
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: "binary_sensor.door_window_sensor_158d0006d30679"
        state: "on"
       - condition: state
         entity_id: "binary_sensor.door_window_sensor_158d0006d30781"
         state: "on"
       - condition: state
         entity_id: "binary_sensor.door_window_sensor_158d0006d3079c"
         state: "on"
  action:
    service: notify.pushbullet
    data_template:
      message: "The {{ condition.state.name }} was left open" #Templates are hard. 
      target:
        - ***@***

The page State Objects - Home Assistant is one I have tried to understand many timed but nothing makes sense

state.state ??
state.name ??

Etc

These do not exist. There is some background assumptions you need to interpret that page. And the page has zero actual yaml examples

And I cannot contribute any examples because I do not understand how to interpret the page content. It would help many if an expert user would PR some actual real examples of the use

There are no condition variables, like for triggers.

Create a group of your sensors, let’s call it group.doors_and_windows, then you can use this:

message: "Open doors or windows: {{ expand('group.doors_and_windows')|selectattr('state', 'eq', 'on')|map(attribute='name')|list | join(',') }}"
1 Like

Have a read of this post by petro and see if it sheds any light on the matter:

That line causes all the members of the group to be listed in the message if any one of them are in an open state as the house becomes unoccupied. I need it to state only the doors which are open or “on”.

Yep I left out an important part of the template. I just updated it.

Perfect thanks. Learn a lot.

Indeed it does. Much better than the official documentation. The official doc talks about something named “state” and if you try to use that in a template you get an error. There is nothing a user can use that starts with “state”. There is however “states” but not the way it is described in the documentation. In my view this State page is a developer document. It confuses more than it helps. All my template based sensors I have ever made are based on forum posts, not the official documentation.