Automation assistance with person detection and light bulbs

I have HA running on a PI running Hassbian. I have HA connecting to an MQTT broker which is a separate private VM which is collecting MQTT feeds from different devices. I have a Philips Hue bridge and light bulbs configured within HA and I can control those without any issue. My goal is to be able to turn the lights on when a person is detected from my Meraki camera and then turn the lights off after a certain amount of time. I have the first part working with this automation…

alias: Motion Detection - Light on
  trigger:
  - platform: mqtt
    topic: /merakimv/<Camera-SN>/raw_detections
  condition:
  - condition: template
    value_template: '{{ ''person'' in trigger.payload }}'
  action:
  - alias: ''
    data: {}
    service: light.turn_on
    entity_id: light.living_room

However, I am not sure how to get the second part working. Basically I need to write another automation to turn the lights off. For example, if the word “person” is not seen for 5 minutes turn the lights off. Just not sure if this would be a condition template or not and how to write the logic. Any help would be appreciated. Thanks

I don’t use MQTT, but I’m thinking, maybe the easiest way to do this is to define a 5 minute timer. Then add to the existing automation’s action a step that starts the timer. If the automation is triggered again within the 5 minute interval, the timer will be restarted. But, if the automation isn’t triggered again for 5 minutes, the timer will finish. Then you can have a second automation that is triggered by the timer finishing that then turns the light off.

Thanks much for the reply. I found a similar recommendation and was able to find some sample code. Below is the end result and this is working perfectly. For demo purposes, I have my timer set to 1 minute. For a home or business type building automation system, a longer timer would probably work better.

First, I added the following timer to my configuration.yaml

timer:
  timer_light:
    duration: '00:01:00'

Second, I updated my automations.yaml to start a timer with the first automation and check for the timer to be finished in the second automation.

alias: Person Detection - Lights On
  trigger:
  - platform: mqtt
    topic: /merakimv/<camera sn>/raw_detections
  condition:
  - condition: template
    value_template: '{{ ''person'' in trigger.payload }}'
  action:
  - service: light.turn_on
    entity_id: light.living_room
  - service: timer.start
    entity_id: timer.timer_light

  alias: Person Detection - Lights Off
  trigger:
    - platform: event
      event_type: timer.finished
      event_data: 
        entity_id: timer.timer_light
  action:
  - service: light.turn_off
    entity_id: light.living_room
1 Like