Display which motion sensor triggered alarm in UI

Hi all,

I use several motion sensors to trigger the alarm component, but it’d be really useful to see which sensor triggered the alarm.

Anyone have any thought on how to do this?

looking for an automation to notify, or a sensor to show the last triggering sensor? Both are available :wink:

Have a look at Conditional Cards You could perhaps trigger on a group state and have a glance card displayed with all the sensors on it, one of which will be lit.

I have set up a glance card with all the sensors on it (not conditional), but this isn’t that useful as it is very much real-time data. It doesn’t actually tell me which motion sensor triggered the alarm.

So if the kitchen sensor activated the alarm, but after a few seconds there is no longer any motion in the kitchen, I wouldn’t know that this caused the alarm.

Is there a way to add a last activated to the item in the front end?

yes, I use @rogro82 's CC variable for that:

variable:
  last_motion:
    value: 'Not set'
    restore: true
    attributes:
      icon: mdi:map-marker
      friendly_name: 'Last Motion'

and automation:

automation:
# Update Last Motion variable
  - alias: 'Update Last Motion'
    id: 'Update Last Motion'
    initial_state: 'on'
    trigger:
      platform: state
      entity_id:
        - binary_sensor.laundry_motion_sensor
        - binary_sensor.dining_table_motion_sensor
        - etc
        - etc
      to: 'on'
    action:
      service: variable.set_variable
      data:
        variable: last_motion
        attributes_template: >
          {
                "history_1": "{{ variable.state }}",
                "history_2": "{{ variable.attributes.history_1 }}",
                "history_3": "{{ variable.attributes.history_2 }}",
                "history_4": "{{ variable.attributes.history_3 }}",
                "history_5": "{{ variable.attributes.history_4 }}",
                "history_6": "{{ variable.attributes.history_5 }}",
                "history_7": "{{ variable.attributes.history_6 }}",
                "history_8": "{{ variable.attributes.history_7 }}",
                "history_9": "{{ variable.attributes.history_8 }}",
                "history_10": "{{ variable.attributes.history_9 }}"
          }
      data_template:
        value: >
          {{ trigger.to_state.attributes.friendly_name|replace('motion sensor','') }}:
          {{as_timestamp(states.variable.last_motion.last_changed)| timestamp_custom('%X') }}

57

or in buttons (needs a few tricks…):

55

3 Likes

That’s pretty slick! When I get time I’ll have a look at implementing that.

For now my hacky solution is to add 'last_changed to the entity This at least gives me an idea of which sensors have been activated recently:

type: entities
theme: dark_theme
entities:
  - entity: binary_sensor.door_window_sensor_158d000288d283
    name: Front Door
    secondary_info: last-changed

1 Like

yes, that is also useful, and I did that in my entities card, glance card doesn’t support that unfortunately. Buttons do !

03

Laundry is on but no motion, Dining is on and motion detected, the rest are Off :wink:

1 Like

Do you use an automation for your alarm?

If so you could use the trigger of the automation to set a persistent notification with the friendly_name of the trigger.

yes, indeed a fine solution, for situations a motion trigger needs to set an alarm (away-mode)
might not be too fine a setup, getting pers. notifications all the time when simply moving around in the home… :wink:

  - alias: 'Motion sensor picked up motion'
    id: 'Motion sensor picked up motion'
    initial_state: off
    trigger:
      platform: state
      entity_id:
        - binary_sensor.laundry_motion_sensor
        - binary_sensor.dining_table_motion_sensor

        - etc
      to: 'on'
    condition:
      - condition: template
        value_template: >
          {{ is_state('input_boolean.notify_motion', 'on')}}
      - condition: template
        value_template: >
          {{ is_state('group.family', 'not_home')}}
    action:
      - service: notify.notify
        data_template:
          title: Motion sensor alert
          message: >
            {{ trigger.to_state.attributes.friendly_name }} picked up motion. Check what's up!
      - condition: template
        value_template: >
          {{ is_state('input_boolean.notify_announcement', 'on')}}
      - service: notify.ios_telefoonmhb
        data_template:
          title: >
            {{ trigger.to_state.attributes.friendly_name }} alert:
          message: 'Sensor motion speech:'
          data:
            push:
              sound: 'US-EN-Morgan-Freeman-Motion-Detected.wav'

or add the pers. notify. service:

    - condition: template
      value_template: >
        {{ is_state('input_boolean.persistent_notification_create', 'on')}}
    - service: persistent_notification.create
      data_template:
        title: Motion sensor alert
        message: >
          {{ trigger.to_state.attributes.friendly_name }} picked up motion. Check what's up!

note that I have this automation initial_state: off, and it is turned on on leaving the house, entering motion detection mode.

1 Like

I love these cards, do you have the yaml that generated them ?

@Mariusthvdb and CC variable Wow! This solution is awesome…well worth spending the time to get this working.
Cheers!

1 Like

Sure, but am away now, please remind me in a week or so? I’ll try to remember ?

Thanks, hopefully you can post when you get a chance

here you go:

type: vertical-stack
cards:

  - type: horizontal-stack
    cards:

      - type: custom:button-card
        template: horizontal-filler

      - type: 'custom:button-card'
        template: button_default_title
        styles:
          card:
            - color: '#555B65'
            - background-color: 'var(--paper-card-background-color)'
        state:
          - operator: template
            value: >
              [[[
              return states['binary_sensor.motion_in_hq'].state === 'on';
              ]]]
            name: >
              [[[
              var id = states['variable.last_motion'].state;
              return 'Motion detected: ' + id.substr(0, id.length-11)
              ]]]
            styles:
              card:
                - color: red
                - background-color: '#555B65'
          - operator: template
            value: >
              [[[
              return states['binary_sensor.motion_sensors_active'].state === 'on'
              ]]]
            name: Motion sensors active
            styles:
              card:
                - color: green
          - operator: template
            value: >
              [[[
              return states['binary_sensor.motion_sensors_active'].state === 'off'
              ]]]
            name: Motion detection Off

      - type: custom:button-card
        template: horizontal-filler

  - type: horizontal-stack
    cards:

      - type: custom:button-card
        template: horizontal-filler

      - type: custom:button-card
        template: button_motion
        name: Laundry
        entity: binary_sensor.laundry_motion_sensor
#        tap_action:
#          action: call-service
#          service: switch.toggle
#          service_data:
#            entity_id: >
#              [[[return 'switch.' + entity.entity_id.split('.')[1] + '_switch' ]]]

      - type: custom:button-card
        template: button_motion
        name: Dining
        entity: binary_sensor.dining_table_motion_sensor

      - type: custom:button-card
        template: button_motion
        name: Auditorium
        entity: binary_sensor.auditorium_motion_sensor

      - type: custom:button-card
        template: button_motion
        name: Frontdoor
        entity: binary_sensor.frontdoor_motion_sensor

      - type: custom:button-card
        template: horizontal-filler

  - type: horizontal-stack
    cards:

      - type: custom:button-card
        template: horizontal-filler

      - type: custom:button-card
        template: button_motion
        name: Dorm
        entity: binary_sensor.dorm_motion_sensor
#        tap_action:
#          action: call-service
#          service: switch.toggle
#          service_data:
#            entity_id: switch.dorm_motion_sensor_switch

      - type: custom:button-card
        template: button_motion
        name: Corridor
        entity: binary_sensor.corridor_motion_sensor

      - type: custom:button-card
        template: button_motion
        name: Master bedroom
        entity: binary_sensor.master_bedroom_motion_sensor

      - type: custom:button-card
        template: button_motion
        name: Corridor terrace
        entity: binary_sensor.corridor_terrace_motion_sensor

      - type: custom:button-card
        template: horizontal-filler

  - type: horizontal-stack
    cards:

      - type: custom:button-card
        template: horizontal-filler

      - type: custom:button-card
        template: button_motion
        name: Corridor office
        entity: binary_sensor.corridor_office_motion_sensor

      - type: custom:button-card
        template: button_motion
        name: Control room
        entity: binary_sensor.control_room_motion_sensor

      - type: custom:button-card
        template: button_motion
        name: Attic
        entity: binary_sensor.attic_motion_sensor

      - type: custom:button-card
        template: button_motion
        name: Poort
        entity: binary_sensor.poort_buiten_motion_sensor

      - type: custom:button-card
        template: horizontal-filler

  - type: horizontal-stack
    cards:

      - type: custom:button-card
        template: horizontal-filler

      - type: custom:button-card
        template: button_motion
        tap_action:
          action: more-info
#        name: 'Last Motion'
        name: >
          [[[
          var fakestate = `<span style="color: red;font-size: 11px;word-wrap:break-word;"> ${entity.state.substr(0, entity.state.length-11)} </span>`;
          return 'Last motion' + "<br>" + fakestate
          ]]]
        entity: variable.last_motion
        show_label: false
        show_state: false
        show_last_changed: true
        show_name: true
#        label_template: >
#          return entity.state.substr(0, entity.state.length-11) + 
#          "<br>" + entity.last_changed
        state:
          - operator: default
            styles:
              card:
                - color: grey
#                - opacity: 0.8
              icon:
                - color: red

      - type: custom:button-card
        template: button_motion
        tap_action:
          action: more-info
        name: Bed timed
        entity: binary_sensor.master_bedroom_motion_sensor_timed

      - type: custom:button-card
        template: button_motion
        tap_action:
          action: more-info
        name: Poort timed
        entity: binary_sensor.poort_buiten_motion_sensor_timed

      - type: custom:button-card
        template: button_body
        lock:
          enabled: true
          unlock: hold
          exemptions:
            - username: !secret button_unlock_name
        confirmation:
          text: >
            [[[ return `Are you sure you want to toggle ${entity.attributes.friendly_name}?` ]]]
        name: All Sensors
        entity: group.philips_motion_sensor_switches
        styles:
          state:
            - color: >
                [[[ if (entity.state == 'on') return 'green';return grey;]]]
          label:
            - color: >
                [[[ if (entity.state == 'on') return 'green';return grey;]]]
          name:
            - color: grey
          icon:
            - color: >
                [[[ if (entity.state == 'on') return 'green';return grey;]]]


      - type: custom:button-card
        template: horizontal-filler

my button motion template is:

button_motion:
  template: button_body
  show_last_changed: true
  tap_action:
    action: call-service
    service: switch.toggle
    service_data:
      entity_id: >
        [[[return 'switch.' + entity.entity_id.split('.')[1] + '_switch' ]]]
  state:
    - value: 'on'
      styles:
        card:
          - color: red
        name:
          - color: black
        icon:
          - color: red
    - operator: template
      value: >
        [[[
        return entity.attributes.on == true
        ]]]
      styles:
        card:
          - color: grey
        icon:
          - color: 'green'
        state:
          - color: 'green'
      id: off-icon
    - operator: default
      styles:
        card:
          - color: grey
#          - opacity: 0.8
        icon:
          - color: grey
      id: off-icon

using template button_body:

button_body:
  color: auto
  size: 30%
  aspect_ratio: 1/1
  show_state: true
  show_label: true
  tap_action:
    action: more-info
    haptic: light
  hold_action:
    action: more-info
    haptic: success
  styles:
    icon:
      - color: grey
    lock:
      - color: red
    card:
      - color: 'var(--primary-color)'
      - background-color: 'var(--paper-card-background-color)'
      - padding-left: 5px
    name:
      - text-overflow: unset
      - white-space: unset
      - word-break: break-word
      - text-align: start
      - justify-self: start
      - font-weight: bold
      - font-family: Helvetica 
      - font-size: 13px
    label:
      - text-align: start
      - font-size: 11px
      - font-family: Helvetica
      - justify-self: start
    state:
      - text-align: start
      - font-size: 11px
      - font-family: Helvetica
      - justify-self: start
      - text-transform: capitalize
      - font-weight: bold
    grid:
      - grid-template-areas: '"i" "n" "s" "l"'
      - grid-template-columns: 1fr
      - grid-template-rows: 1fr min-content min-content
    img_cell:
      - justify-content: start
      - align-items: start

hope this helps :wink:

3 Likes