# Doug Smart Home · Câmeras com Frigate
# Configurações > Automações > Criar automação > três pontinhos > Editar em YAML > cole e salve.
alias: 'Câmeras: veículo estacionou em frente de casa'
mode: single
max_exceeded: silent
triggers:
- trigger: mqtt
  topic: frigate/events
  value_template: >-
    {% set a = value_json.after | default({}) %}
    {% set b = value_json.before | default({}) %}
    {{ 'sim' if a.camera | default('') == 'rua'
         and a.label | default('') in ['car', 'motorcycle', 'bus', 'bicycle']
         and 'frente_casa' in (a.current_zones | default([]))
         and a.stationary | default(false) and not (b.stationary | default(false))
       else 'nao' }}
  payload: sim
variables:
  evento: '{{ trigger.payload_json.after }}'
  # path_data = pontos por onde o carro passou. liquido = distância do primeiro ao último ponto;
  # retidao = liquido / caminho total (1 = linha reta). Carro reencontrado fica "pulando" no lugar.
  trajeto: >-
    {% set p = evento.path_data | default([]) %}
    {% set ns = namespace(total=0) %}
    {% for i in range(1, p | count) %}
      {% set ns.total = ns.total + ((p[i][0][0] - p[i-1][0][0]) ** 2
                                    + (p[i][0][1] - p[i-1][0][1]) ** 2) ** 0.5 %}
    {% endfor %}
    {% set liquido = ((p[-1][0][0] - p[0][0][0]) ** 2
                      + (p[-1][0][1] - p[0][0][1]) ** 2) ** 0.5 if p | count >= 3 else 0 %}
    {{ {'liquido': liquido | round(3), 'retidao': (liquido / ns.total) | round(2) if ns.total > 0 else 0} }}
conditions:
- condition: template
  value_template: '{{ trajeto.liquido >= 0.15 and trajeto.retidao >= 0.6 }}'
- condition: template
  value_template: >-
    {{ this.attributes.last_triggered is none
       or now() - this.attributes.last_triggered > timedelta(minutes=10) }}
actions:
- action: notify.mobile_app_seu_celular                      # <-- TROQUE
  data:
    title: '🚗 Veículo parado em frente de casa'
    message: '{{ now().strftime("%H:%M") }}'
    data:
      # Imagem de agora, não a "melhor foto" do evento (que pode ser do carro lá no fim da rua).
      image: /api/camera_proxy/camera.rua                   # <-- TROQUE
