Lovelace button card service action filename templating

I’m making a lovelace view with my CCTV camera and adding a button for making a snapshot and making a recording.

I seem to have a problem using call-service.

My code:

type: vertical-stack
cards:
  - type: picture-entity
    entity: camera.vmsynds3617xs01_achter_terras
    camera_image: camera.vmsynds3617xs01_achter_terras
    camera_view: live
  - type: horizontal-stack
    cards:
      - type: button
        tap_action:
          action: call-service
          service: camera.snapshot
          data:
            entity_id: camera.vmsynds3617xs01_achter_terras
            filename: /tmp/test.jpg
        entity: camera.vmsynds3617xs01_achter_terras
        icon: 'hass:image'
        name: Take snapshot
        icon_height: 50px
        hold_action:
          action: none
      - type: button
        tap_action:
          action: call-service
          service: camera.record
          data:
            entity_id: camera.vmsynds3617xs01_achter_terras
            filename: /tmp/test.mp4
            lookback: 5
            duration: 60
        entity: camera.vmsynds3617xs01_achter_terras
        name: Make recording
        icon_height: 50px
      - type: button
        tap_action:
          action: more-info
        entity: binary_sensor.lumi_lumi_sensor_motion_aq2_ias_zone
        icon_height: 50px
        show_name: true
        name: Motion detected
        icon: 'hass:walk'
        hold_action:
          action: none

The error i get:

Failed to call service camera/snapshot. required key not provided @ data[‘filename’]

I have filename configured in data below the service. Am I not writing it correctly?

Figured it out from an example in the custom:button-card.

I have to use service_data apparently. This is different from using service action in automations.

      - type: button
        tap_action:
          action: call-service
          service: camera.snapshot
          service_data:
            entity_id: camera.vmsynds3617xs01_achter_terras
            filename: /tmp/test.jpg
1 Like

However I want to use camera name and date-time in the filename.

I’m using custom:button-card now.

      - type: 'custom:button-card'
        tap_action:
          action: call-service
          service: camera.snapshot
          service_data:
            entity_id: camera.vmsynds3617xs01_achter_terras
            filename: >
              [[[ 
                  var name = states[camera].object_id; 
                  var time = now().strftime("%Y%m%d-%H%M%S"); 
                  return '/media/snapshots/' + name + '_' + time +'.jpg'; 
              ]]]
        entity: camera.vmsynds3617xs01_achter_terras
        icon: 'hass:image'
        name: Take snapshot

But that doesn’t seem to work. What am I doing wrong here?

I have it working with jinja:

    filename: >
      {% set current_datetime = now().strftime("%Y%m%d-%H%M%S") %}
      /media/snapshots/{{ current_datetime }}_achter_terras.jpg

Sorry for this monologue.
It might help others in the future.

1 Like