Automation value template assistance

- alias: Motion Detected Notification
  
  trigger:
  - entity_id: binary_sensor.carport_motion
    platform: state
    to: 'on'  

    
  condition: template
    value_template: '{{ as_timestamp(now()) - as_timestamp(states.automation.motion_detected_notification.attributes.last_triggered) | int > 1800 }}'

  action:    
  - service: notify.pushbullet
    data:  
      message: 'Motion Detected in front of Carport {{now().strftime("%H:%M:%S on %m/%d/%y")}}'

I get Error
Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/Automation/Motion Sensors.yaml”, line 37, column 19

I am somwhat new to this and not sure what I am doing wrong, from everything I can find, it seems to be right.

try this for your condition section:

condition:
  condition: template
  value_template: '{{ as_timestamp(now()) - as_timestamp(states.automation.motion_detected_notification.attributes.last_triggered) | int > 1800 }}'
1 Like

As always, you guys are fantastic.
I think I had tried it that way at one point, but I guess I didn’t since that worked.

Thanks again.

Another question based on my question. Do I have it correct, for what I am wanting it to do?

I want the automation to run when motion is detected then notify me, but if motion is detected within a certain time, like this example 30 minutes it will not notify me. Only notify me again after the time frame elapsed.
I assume the 1800 is in seconds.

Tested it in the template editor and I believe it to be working, however, if I trigger it manually twice within a 1 minute I get the notification twice.

I honestly don’t know about that. I don’t know if the manual trigger ignores the conditions or not. But if I would guess I would be surprised if it ignored them. But I’ve been surprised before. :wink:

Manually triggering an automation effectively bypasses its triggers and conditions and just unconditionally runs its actions. That’s why you’re seeing that.

The condition you’re using is basically ok. In fact, I do something very similar in one of my automations (which also involves motion detection.) However, before the first time an automation is triggered its last_triggered attribute will be None, which will cause an error in the template as you’ve written it. This is what I’d suggest (based on what works for me):

  condition:
    condition: template
    value_template: >
      {% set last_triggered = as_timestamp(
        state_attr('automation.motion_detected_notification', 'last_triggered')) %}
      {{ last_triggered is none or (as_timestamp(now()) - last_triggered) > 30*60 }}

Thanks, giving it a try now