Using camera.snapshot with a timestamp

I just got the new camera.snapshot service working with a shock sensor and thought this would be a fun little example for the folks here.

This uses a Monoprice Z-Wave Shock Detector and a D-Link DSC-933L IP camera, but it ought to work with pretty much any sensor and generic camera.

In configuration.yaml:

    homeassistant:
      whitelist_external_dirs:
        - /tmp
    camera:
      - platform: mjpeg
        name: DLink DSC-933L
        still_image_url: http://192.168.x.x/image.jpg
        mjpeg_url: http://192.168.x.x/video.cgi
        username: xxx
        password: xxx

In automations.yaml:

    - alias: 'Notify when shock sensor triggers'
      trigger:
        platform: state
        entity_id: sensor.shock1_alarm_level
        from: '0'
        to: '255'
      action:
        - service: notify.notify
          data:
            title: 'Shock Sensor'
            message: 'Shock detected'
        - service: camera.snapshot
          data:
            entity_id: camera.dlink_dsc933l
            filename: '/tmp/shock1.{{ as_timestamp(now()) }}.jpg'

Next step would be to email the photo or add it to an ios: notification. But this works great so far!

Note that your chosen sensor will use some other state change probably - the Monoprice changes from 0 to 255 in ā€œalarm_levelā€ but their door sensor (for example) changes ā€œaccess_controlā€ from 23 to 22. You could also use the binary_sensor but I found that these donā€™t always trip reliably on the Monoprice sensorsā€¦

2 Likes

Thnx for the example. I tried but the timestamp doesnā€™t display the right time for me. I get something like this: shock1.1537353764.842347.jpg

1 Like

Did you get the time stamp thing it should be something like this

{{ now ().year }}_{{ now ().month }}_{{ now ().day }}_{{ now ().hour }}_{{ now ().minute }}
2 Likes

that timestamp code doesnt work. Beside, great work :slight_smile:

This works for me (you can add as many cameras as you want or select a PIR sensor as trigger,ā€¦)

alias: Comando Telegram CƔmaras
description: Manda foto Telegram
trigger:

  • platform: event
  • event_type: telegram_command
  • event_data:
    command: /camaras
    condition:
    action:
  • variables:
    triggerTimestamp: ā€œ{{ now().strftime(ā€˜%Y%m%d_%H%M%Sā€™) }}ā€
  • service: camera.snapshot
    data:
    filename: /config/www/snapshots/esp32cam01_{{ triggerTimestamp }}.jpg
    target:
    device_id: 1d71dfe464bc7ab9dd9f8b9989750a98
  • service: notify.telegram_jags
    data:
    message: CAMARA
    data:
    photo:
    - file: /config/www/snapshots/esp32cam01_{{ triggerTimestamp }}.jpg
    caption: LIVE ESP32cam01

mode: single