Use condition in notification message

I am trying to create notifications if i left the lights and other stuff on when leaving the house.
I am trying to trigger on zone goes 0. Then i know nobody is there. But it could be cool to reference what light was left on, i know you can do this with triggers, like {{ trigger.from_state.attributes.friendly_name }}. Anyway that is it possible to do the same with conditions.

Example:

alias: Test notification
description: ""
trigger:
  - platform: state
    entity_id:
      - zone.home
    for:
      hours: 0
      minutes: 5
      seconds: 0
    to: 0
condition:
  - condition: or
    conditions:
      - condition: state
        entity_id: light.example1
        state: "on"
      - condition: state
        entity_id: light.example2
        state: "on"

action:
  - service: notify.mobile_app_peter
    data:
      message: >-
        The light {{ condition.attributes.friendly_name }} has been
        left on - what do you want to do?
      data:
        custom_entity: "{{ condition.entity_id }}"
        actions:
          - action: "1"
            title: Turn On
          - action: "0"
            title: Turn Off
mode: single

Or should i turn it upside down and do some magic with triggers ??

Unlike trigger, there is no condition variable that is created when an automation’s trigger fires. You could use trigger variables

alias: Test notification
description: ""
trigger:
  - platform: state
    entity_id:
      - zone.home
    for:
      hours: 0
      minutes: 5
      seconds: 0
    to: 0
    variables:
      lights_on: >
        {{ expand('light.example1', 'light.example2')
        | selectattr('state', 'eq', 'on') 
        | map(attribute='entity_id')  | list }}
      count: "{{ lights_on | count > 0 }}"
condition:
  - condition: template
    value_template: "{{ count }}"
action:
  - service: notify.mobile_app_peter
    data:
      message: >-
        The light {{ lights_on | map(attribute='attributes.friendly_name') | join }} has been
        left on - what do you want to do?
      data:
        custom_entity: "{{ lights_on }}"
        actions:
          - action: "1"
            title: Turn On
          - action: "0"
            title: Turn Off
mode: single
1 Like

Thank you so much for the inspiration. I ended up doing it like this:

alias: Test notification
description: ""
trigger:
  - platform: state
    entity_id:
      - zone.home
    for:
      hours: 0
      minutes: 5
      seconds: 0
    to: 0
    variables:
      lights_on: >
        {{ ['light.example1', 'light.example2'] | select('is_state', 'on')
        | list }}
      count: "{{ lights_on | count > 0 }}"
condition:
  - condition: template
    value_template: "{{ count }}"
action:
  - service: notify.mobile_app_peter
    data:
      message: >-
        The light has been left on {% for entity in lights_on %} {{
        state_attr(entity, 'friendly_name') }} {% if not loop.last %},{% endif
        %} {% endfor %} - what do you want to do?
      data:
        custom_entity: "{{ lights_on }}"
        actions:
          - action: "1"
            title: Turn On
          - action: "0"
            title: Turn Off
mode: single

Only reason i went i that direction was that if the entity is a group, the expand will show all its children, which in my case i dont care about :slight_smile:

Again, thank you :slight_smile:

1 Like