Call Service Within Template after Long Press

I’m not sure if what I’m trying to do is possible or if I just have the syntax wrong. I have a floorplan dashboard with icons and badges for my various sensors around the house. I would like to have my front door lock or unlock, based on current state, after a long press on the icon. I’m assuming the way I need to accomplish this is through the use of templating but I can’t seem to get it quite right and I’m not sure where errors might be logged.

- type: state-icon
        entity: lock.front_door_lock
        tap_action: []
        hold_action:
          - action: template
            value_template: |
              {% if is_state('lock.front_door_lock', 'locked') %}
                service: lock.unlock
                data: {}
                target:
                  entity_id: lock.front_door_lock
              {% else %}
                service: lock.lock
                data: {}
                target:
                  entity_id: lock.front_door_lock
              {% endif %}

Can someone help me identify where I’ve gone wrong?

Not tested but it is already better as the only action types for an action are: more-info , toggle , call-service , navigate , url or none

- type: state-icon
  entity: lock.front_door_lock
  hold_action:
    action: call-service
    service: >
      {% if is_state('lock.front_door_lock', 'locked') %}
        lock.unlock
      {% else %}
        lock.lock
      {% end if %}
    data: {}
      target:
        entity_id: lock.front_door_lock

Hmmm… That doesn’t work and gives me an error:

Failed to call service {% if is_state('lock/front_door_lock', 'locked') %} service: lock. Service not found.

I needed to remove the braces after the data field, that also kicked up an error. And the forward slash inside the is_state call isn’t a typo. It looks like it’s not parsing this as a template.

My home assistant is under maintenance, I can’t validate what I’m writing.
You should always test your templates in the developer tools.
Not sure that you can put a tempate there but can’t test now, the other solution is to call a script that will do the template.

1 Like

Move your templated action to a script and then call the script from your card’s hold-action.

1 Like

Home Assistant’s standard set of cards don’t support templates (except for the Markdown card). Do what Didgeridrew suggested.

1 Like

That got things working! Created a script with a simple if statement to toggle based on lock state. Changed the hold_action to call the script and I’m in business. Thank you for the clarifications @Olivier1974 and @Didgeridrew!!

For future reference:

- type: state-icon
  entity: lock.front_door_lock
  tap_action: []
  hold_action:
    action: call-service
    confirmation:
      text: This will TOGGLE the front door lock. Are you sure?
    service: script.toggle_door_lock

By using Didgeridrew’s suggestion but marking your own post with the Solution tag, you have misunderstood how that tag is used in this community forum.

Refer to guideline 21 in the FAQ. The Solution tag’s purpose is to mark the first post that explains how to solve the problem. Without Didgeridrew’s suggestion, you wouldn’t know how to solve the problem so obviously you didn’t solve it and Didgeridrew did. Therefore please consider marking Didgeridrew’s post with the Solution tag.

Mah bad mate