Trigger Template

Hi,

I’m trying to create an automation that sends a notification when a window is open for a longer period of time in winter.

This is what works so far:

alias: Fenster auf (Generic)
description: Sends a notification if a window is open for a longer period of time in winter
trigger:
  - platform: state
    entity_id: binary_sensor.zb2_b_wz_door
    from: 'off'
    to: 'on'
    for: '00:15:05'
  - platform: state
    entity_id: binary_sensor.zb2_c_bo_window
    from: 'off'
    to: 'on'
    for: '00:15:05'
  - platform: state
    entity_id: binary_sensor.zb2_f_window_flur_ias_zone
    from: 'off'
    to: 'on'
    for: '00:15:05'
  - platform: state
    entity_id: binary_sensor.zb2_a_fu_door_ias_zone
    from: 'off'
    to: 'on'
    for: '00:15:05'
  - platform: state
    entity_id: binary_sensor.zb1c_window
    from: 'off'
    to: 'on'
    for: '00:15:05'
condition:
  - condition: numeric_state
    entity_id: sensor.openweathermap_temperature
    below: '15'
action:
  - service: notify.mobile_app_inspektor_gadget
    data:
      message: >-
        {{ sensors[trigger.entity_id].name}} im {{
        sensors[trigger.entity_id].room}} ist auf.
mode: restart
variables:
  sensors:
    binary_sensor.zb2_f_window_flur_ias_zone:
      room: Flur
      name: Das Fenster
    binary_sensor.zb2_a_fu_door_ias_zone:
      room: Flur
      name: Die Tür
    binary_sensor.zb2_b_wz_door:
      room: Wohnzimmer
      name: Die Tür
    binary_sensor.zb1c_window:
      room: Bad unten
      name: Das Fenster
    binary_sensor.zb2_c_bo_window:
      room: Bad oben
      name: Das Fenster

I got the notification generated dynamically based on the sensor - defined in a variable. I’m now trying to get the trigger (state change from off to on for 15min) into a template but I just can’t get it working.

Any ideas how to tackle this?

Thanks for your help,

Robert

1 Like

What I understand is, that this is basically working, but you want to refactor your trigger to not be so redundant and instead use templating. However, that seems to be quote hard for me. Instead I would suggest to use a group.

You could then trigger on the group change and have the service notify you on the elements true within that group.

Yes, exactly. Since I have the sensors already defined as variables for the notifications it would be great if I can simply use this also for a trigger. So I don’t have to define it twice.

Currently, you cannot use variables in a Template Trigger.

You may wish to cast a vote for the following Feature Request, to allow for variables in a Template Trigger:


Here is another way to achieve your goal of minimizing duplication. Instead of using variables to store each binary_sensor’s name and room, store this information as a custom attribute.

By creating custom attributes, called name and room, they become part of the entity, will appear in the entity’s list of attributes (shown in the Attributes column in Developer tools > States), and are accessible by any template (using the state_attr() function).

You can create custom attributes by adding them to the customize.yaml file or via the UI with Configuration > Customizations. Here is the documentation for Customization.

For your purposes, the customize.yaml would contain this:

binary_sensor.zb2_f_window_flur_ias_zone:
  room: Flur
  name: Das Fenster
binary_sensor.zb2_a_fu_door_ias_zone:
  room: Flur
  name: Die Tür
binary_sensor.zb2_b_wz_door:
  room: Wohnzimmer
  name: Die Tür
binary_sensor.zb1c_window:
  room: Bad unten
  name: Das Fenster
binary_sensor.zb2_c_bo_window:
  room: Bad oben
  name: Das Fenster

The automation can be reduced to this:

alias: Fenster auf (Generic)
description: Sends a notification if a window is open for a longer period of time in winter
trigger:
  - platform: state
    entity_id:
      - binary_sensor.zb2_b_wz_door
      - binary_sensor.zb2_c_bo_window
      - binary_sensor.zb2_f_window_flur_ias_zone
      - binary_sensor.zb2_a_fu_door_ias_zone
      - binary_sensor.zb1c_window
    from: 'off'
    to: 'on'
    for: '00:15:05'
condition:
  - condition: numeric_state
    entity_id: sensor.openweathermap_temperature
    below: '15'
action:
  - service: notify.mobile_app_inspektor_gadget
    data:
      message: >-
        {{ state_attr(trigger.entity_id, 'name')}} im {{
        state_attr(trigger.entity_id, 'room')}} ist auf.
1 Like

Hi @123,

thanks a lot for your clarification. I left a vote. Meanwhile I implemented your suggestion. This makes it already a lot cleaner.

Thanks for your help,

Robert

1 Like

If you are interested, I explained other uses for custom attributes here:

1 Like

Thank you! I like the approach. It’s quite useful :smiley: