Automation condition based on attribute of triggering entity id

Hi,

I have an automation which alerts my phone when motion is detected on one of my cameras. I want to use the same automation for all of the cameras and have used the trigger.entity_id in a template on the notification.

Now i want to setup something to disable specific cameras if they are causing too many alerts (rain and spiderwebs) or disable a group, eg disable backyard yard when doing gardening but still get notified if someone comes to the front door.

What would be the best way to achieve this? I have added a attribute to each camera motion sensor for enabled true/false, but not sure how to use it in the condition with the triggering camera. Iā€™ve also tried with an input boolean, but cant figure out a way to say if frontdoor triggered the motion then check if frontdoor is enabled before sending the notification.

how about showing us your code make it easyer on us to help you

I would keep the triggers and then just add the condition

also copy paste is your friend copy the working one and change it to meet the new logic

here is one

I build a Cuckoo clock

- id: 33dcd8e2-e87c-4d18-82bc-c7f9b53a1624
  alias: Cuckoo Clock
  initial_state: true
  mode: restart
  trigger:
    - platform: time_pattern
      minutes: 0
    - platform: time_pattern
      minutes: 15
    - platform: time_pattern
      minutes: 30
    - platform: time_pattern
      minutes: 45

dont want it making a here it at nite time

so I added a condition

  condition:
    condition: and
    conditions:
    - condition: time
      after: '08:29:00'
      before: '18:00:00'

then I got thinking as im a shift worker only want to go off when Im home

so added a new condition

    - condition: state
      entity_id: person.steve
      state: home

now it only only works after 08:30:00 and if im home and stop after 18:00:00

so I would have a input_boolean out the back gardening on/off

add that to the condition

this is the automation as it stands,

- id: '161456657'
  alias: Motion Detection Camera
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.garage_motion
    - binary_sensor.laundryside_motion
    - binary_sensor.laundryside_motion
    - binary_sensor.southeast_motion
    - binary_sensor.southwest_motion
    - binary_sensor.gatecam1_motion
    - binary_sensor.gatecam2_motion
    - binary_sensor.alfresco_motion
    - binary_sensor.frontdoor_motion
    to: 'on'
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: timer.notification_rate
      state: idle
  action:
  - service: notify.mobile_app_chris
    data:
      message: Camera {{ trigger.entity_id.split('.')[-1].split('_')[0] }}
      title: Motion Alarm
      data:
        color: red
        channel: MotionDetect
        ttl: 0
        importance: high
        priority: high
        vibrationPattern: '100, 1000, 100, 1000, 100, 250, 1000, 250 '
        clickAction: /lovelace/motion-sensors
        actions:
        - action: alarm
          title: Alarm!
        - action: snooze10
          title: Snooze 10 Minutes
        - action: snooze30
          title: Snooze 30 Minutes
        image: /api/camera_proxy/camera.{{ trigger.entity_id.split('.')[-1] }}_snap
  - service: timer.start
    data:
      duration: '0'
    target:
      entity_id: timer.notification_rate
  mode: single

and as an example for the binary_sensor.alfresco_motion that triggers the automation i added the ā€œalertingā€ attribute using customizations as below

friendly_name: Alfresco Motion
device_class: motion
alerting: 'on'

basically i would like to have the automation check if the ā€œalertingā€ attribute is ā€˜onā€™ for the entity that triggered the automation.

I would create an input_boolean per camera fi input_boolean.camera1 (on : you want a message for this camera, off : you donā€™t)ā€¦

The automation:

trigger:
  - camera1 has detected a motion
  - camera2 has detected a motion
  - camera3 has detected a motion
condition:
  - conditions: or
    - condition:          
      - trigger.enty_id == camera1 and input_boolean.camera1 == on
      - trigger.enty_id == camera2 and input_boolean.camera2 == on
      - trigger.enty_id == camera3 and input_boolean.camera3 == on
action:
2 Likes

Unrelated to this topic, but I believe you can reduce the four Time Pattern Triggers, for your Cuckoo Clock, to just one Time Pattern Trigger:

  trigger:
    - platform: time_pattern
      minutes: '/15'

It will trigger every 15 minutes at 0, 15, 30, and 45 minutes after each hour.

you could change it to

Camera {{ trigger.to_state.attributes.friendly_name }}

and

and add @browetd logic to your automation

then we start thinking making it smarter
by getting thos input_boolean.camera to turn on/off base on some other logic

thanks bro

didnā€™t know about that one

see can teach a old dog a new trick

logic change

OK, so a bit of messing around but found i can set each trigger individually with a trigger id, then use that trigger id in a condition statement
built using the automation builderā€¦ so could probably be optimized a bit

  - condition: or
    conditions:
    - condition: and
      conditions:
      - condition: trigger
        id: garage
      - condition: state
        entity_id: input_boolean.garage_motion_alerting
        state: 'on'
    - condition: and
      conditions:
      - condition: trigger
        id: laundryside
      - condition: state
        entity_id: input_boolean.laundryside_motion_alerting
        state: 'on'
  action:

it does seem to be getting pretty complex now, i wanted to keep it simple so i can easily add more cameras without having to add extra conditions every thimeā€¦

nice one bro

think the light is been turn on

why not just have one automation for each motion rather than one automation and looking at each trigger that going to make LOOOOOOONNNNNNNGGGGGG and hard to fix if you break it LOL

based on the complexity ill break them up per camera and do some cut and paste for now, if there is an easy way to do it in the future, please let me know!

As long as you make sure your naming conventions for the trigger ids and boolean names are consistent with the example you gave, you should be able to template the condition. The ā€œandā€™sā€ and ā€œorā€™sā€ will be unnecessary.

condition:
- condition: state
entity_id: input_boolean.{{trigger.id}}_motion_alerting
state: ā€˜onā€™
action:

EDIT: Apparently, state conditions donā€™t accept templatesā€¦ See @123ā€™s template condition below

If youā€™re interested, this:

      message: "Camera {{ trigger.entity_id.split('.')[-1].split('_')[0] }}"

can be reduced to this:

      message: "Camera {{ trigger.to_state.object_id[:-7] }}"

or you can do what myle suggested and use the entityā€™s friendly_name attribute.

Why do all that when the trigger variable for a State Trigger already provides information about the triggering entity?

condition: 
  - "{{ is_state('input_boolean.' ~ trigger.to_state.object_id[:-7] ~'_motion_alerting', 'on') }}"

I donā€™t believe the State Condition supports templates.

Thanks very much for your input, it is very very helpful for many of my awesomations!

I am banging my head against a wall with the template condition thoughā€¦ even putting in a simple template

"{{ is_state('input_boolean.test_motion_alerting', 'on') }}"

tests perfectly in the developer tools-template, result is true when the input is on, and false when offā€¦ as expected

however the trace on the automation is always false. any ideaā€™s what im doing wrong?

this is the simplest test i can do to prove whatā€™s happening, just needs the one input boolā€¦ the condition template always returns false

id: '1632514489736'
alias: test_motion
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.test_motion
    to: 'on'
condition:
  - condition: template
    value_template: "{{ is_state('input_boolean.test_motion', 'on') }}"
action:
  - service: notify.notify
    data:
      message: condition was true
mode: single

I created and tested a nearly identical automation on my system and it worked as expected.

alias: test_motion
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.test
    to: 'on'
condition:
  - condition: template
    value_template: "{{ is_state('input_boolean.test', 'on') }}"
action:
  - service: notify.persistent_notification
    data:
      message: condition was true
mode: single

i just switched to edit in YAML and it worked too, then i tried changing something in the visual builder and it threw and errorā€¦

Yep there is a strange bug when editing the template in the visual editor vs the YAML methodā€¦

if I edit the template in the visual editor it doesnā€™t work anymore (always tests false), but if I change it and save it in the yaml editor it works againā€¦

edit: it seems to be some formatting changes when saving in the visual mode, it splits up the lines and adds a >- and drops the template onto the next line.

I created it using the Automation Editor; worked for me. :man_shrugging:

strange! i found editing it then saving then edit then save seemed to cause the formatting of the template to change when viewing in the YAML editor, where the ~ symbol was it split to the next line, and sometimes it puts extra single quotes. strange thing is it didnt happen every single time.

anyway, thanks very much for your input, it has significantly reduced the codeā€¦ once i have finished testing i will post the resultsā€¦

I now have this working very well, my code is not perfect but hopefully it helps someone!.

I have several cameras with a meaningful name ie Frontdoor or Gate. and wanted a way to send notifications to my phone. i also wanted to be able to change some settings for each camera as to weather it will take my phone off silent before sending the alert, and be able to silence each camera individually or have a timer so i dont forget to turn it back on.


The cameras I use for the motion detection come from a MQTT message from an app called AITool that uses deepstack to analyse the images from blue iris. the scope of this is beyond this post, but any camera should work.

Each camera has a few associated entities (using my Frontdoor camera as an example)
camera.Frontdoor - the camera
binary_sensor.Frontdoor_motion - the motion detection signal (from blueiris)

I created some helpers for these automations too, each camera has
timer.Frontdoor_motion_snooze - the timer for snoozing notifications
input_boolean.Frontdoor_motion_enabled - send notifications for this camera
input_boolean.Frontdoor_motion_unmute - take phone off silent before sending notification
input_number.Frontdoor_motion_snooze_time - a slider which starts the snooze timer for that camera

and a couple of global timers
input_number.motion_global_snooze_time
timer.motion_global_snooze
timer.motion_notification_rate

The main automation, with PHONEID and device ID removedā€¦ update with your own.

alias: Motion Detection All Camera
description: 'Automation to send notifications for movement detected on security cameras'
trigger:
  - platform: state
    entity_id: 
      - input_boolean.test_motion
      - binary_sensor.camera1_motion
      - binary_sensor.camera2_motion
      - binary_sensor.camera3_motion
      - binary_sensor.camera4_motion
      - binary_sensor.camera5_motion
      - binary_sensor.camera6_motion
      - binary_sensor.camera7_motion
      - binary_sensor.camera8_motion
    to: 'on'
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: timer.motion_notification_rate
        state: idle
      - condition: state
        entity_id: timer.motion_global_snooze
        state: idle
      - condition: template
        value_template: {{ is_state('input_boolean.' ~trigger.to_state.object_id~'_enabled', 'on') }}
      - condition: template
        value_template: {{ is_state('timer.' ~trigger.to_state.object_id~'_snooze', 'idle') }}
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: state
                entity_id: sensor.PHONEID_ringer_mode
                state: silent
              - condition: template
                value_template: {{ is_state('input_boolean.' ~trigger.to_state.object_id~'_unmute', 'on') }}
        sequence:
          - device_id: b6eca04cfb7a63e5e1daxxxxxxxxxxx
            domain: mobile_app
            type: notify
            title: normal
            message: command_ringer_mode
    default: []
  - service: notify.mobile_app_phoneXX
    data:
      message: '{{trigger.to_state.name[:-7]}} Camera'
      title: Motion Alarm
      data:
        color: red
        channel: MotionDetect
        ttl: 0
        importance: high
        priority: high
        vibrationPattern: '100, 1000, 100, 1000, 100, 250, 1000, 250 '
        clickAction: /lovelace/motion-sensors
        actions:
          - action: alarm
            title: Alarm!
          - action: URI
            title: Snooze all cameras
            uri: entityId:input_number.motion_global_snooze_time
          - action: URI
            title: Snooze this camera
            uri: {{ 'entityId:input_number.' ~trigger.to_state.object_id~'_snooze_time' }}
        image: /api/camera_proxy/camera.{{trigger.to_state.object_id}}
  - service: timer.start
    data:
      duration: '00:01:00'
    target:
      entity_id: timer.motion_notification_rate
mode: single

One thing i searched for and couldnā€™t find was a nice way to adjust/start a timer from and input sliderā€¦ so I made this automation to do just that

alias: Motion Slider moved
  description: 'automation to start a timer when a slider is moved. '
  trigger:
  - platform: state
    entity_id:
    - input_number.test_motion_snooze_time
    - input_number.camera1_motion_snooze_time
    - input_number.camera2_motion_snooze_time
    - input_number.camera3_motion_snooze_time
    - input_number.camera4_motion_snooze_time
    - input_number.camera5_motion_snooze_time
    - input_number.camera6_motion_snooze_time
    - input_number.camera7_motion_snooze_time
    - input_number.camera8_motion_snooze_time
    - input_number.motion_global_snooze_time
  condition: []
  action:
  - service: timer.start
    data:
      duration: 00:{{ states(trigger.entity_id)| int }}:00
    target:
      entity_id: {{ 'timer.' ~trigger.to_state.object_id[:-12]~'_snooze' }}
  mode: single

If you look at the actionable notification in the main automation, it opens up the snooze slider for that camera, or the global one. the alarm triggers an alarm script

i also have some scripts to mute/unmute all cameras and have these set as quick access tiles on my phone

mute_camera_12h:
  sequence:
  - service: timer.start
    data:
      duration: '12:00:00'
    target:
      entity_id:
      - timer.motion_global_snooze
  mode: single
  alias: Mute Camera 12h
  icon: mdi:cctv-off
unmute_cameras:
  alias: UnMute Cameras
  sequence:
  - service: timer.finish
    target:
      entity_id:
      - timer.camera1_motion_snooze
      - timer.camera2_motion_snooze
      - timer.camera3_motion_snooze
      - timer.camera4_motion_snooze
      - timer.camera5_motion_snooze
      - timer.camera6_motion_snooze
      - timer.camera7_motion_snooze
      - timer.camera8_motion_snooze
	  - timer.motion_notification_rate
      - timer.motion_global_snooze
      - timer.test_motion_snooze
  mode: single
  icon: mdi:cctv

Thereā€™s a leftover single-quote at the end of this template:

      entity_id: {{ 'timer.' ~trigger.to_state.object_id[:-12]~'_snooze' }}'
                                                                           ^
                                                                           |

FWIW, you can also do it like this:

      entity_id: 'timer.{{ trigger.to_state.object_id[:-12] }}_snooze'

There are other places in the code where this minor reduction might also be applicable