Call-service button from lovelace?

I want to have a button that calls the a rest_command service. All the examples I see require an entity which this doesn’t have and the other options are to download custom button-cards, which isn’t ideal.
Any out of the box options?

thanks

Use the custom button card. Doesn’t need an entity.

Create a script that calls the rest command service. The script is an entity.

  • type: entity-button
    entity: script.rest_command
    tap_action: call-service
    service: script.turn_on

Or just create a simple entity to tie to the button.

input_boolean:
rest_entity_button:
name: Dumb entity that does nothing

Now override the tap_action with call-service and you’re done.

But this requires bringing in the custom cards from github ( https://github.com/custom-cards/button-card) ?

Yep. If you want a flashy interface, you’ll cross this bridge at some point. Might as well jump in the deep end now.

Set up HACS first. It makes installation of custom <anything> brainless.

3 Likes

thanks for the info

Managed to get this working

             - entity: scene.day
                hold_action:
                  action: more-info
                icon_height: 30px
                show_icon: true
                show_name: true
                tap_action:
                  action: call-service
                  service: scene.turn_on
                  service_data:
                    entity_id: scene.day
                type: button
   
11 Likes

Searched for exactly this solution! Thanks! :slight_smile:

Sorry to bother you, but can anybody tell me where to add this yaml snippet? In the card config or the conig.yaml or somewhere else? Is it using the Custom Button Card?

Thanks

Just add a regular old button card from the UI, you’ll see the option to change the tap action to a service. Most everything in this thread is outdated.

Thank you that worked for me!

Here is what I came up with using the Button card to toggle 2 service calls.
(input_boolean.mail_present is a boolean helper)
The 3 square brackets syntax is explained here.

type: custom:button-card
entity: input_boolean.mail_present
name: >
  [[[
    var status = states["input_boolean.mail_present"].state;
    return 'Turn it ' + (status === 'on' ? 'off' : 'on') 
  ]]]
color: white
show_state: true
tap_action:
  action: call-service
  service: >
    [[[
      if (entity.state == 'off')
        return 'input_boolean.turn_on';
      else
        return 'input_boolean.turn_off';
    ]]]
  service_data:
    entity_id: input_boolean.mail_present

Hi. Does this example actually work? I’ve tried exactly the same thing to call an “on” service or an “off” service depending on a state and I get an error with the [[[ notation.

Here is the final enhanced and working version :

type: custom:button-card
entity: input_boolean.mail_present
layout: icon_label
icon: mdi:mailbox
label: |
  [[[
    var status = states["input_boolean.mail_present"].state;
    var arrivalDate = entity.state == 'on' ? states["input_datetime.mail_arrived"].state.split(' ')[1] : '';
    return arrivalDate === '' ? 'No mail' : arrivalDate;
  ]]]
show_state: false
show_label: true
show_name: false
styles:
  card:
    - '--mdc-ripple-color': yellow
    - '--mdc-ripple-press-opacity': 0.5
  icon:
    - color: |
        [[[
          if (entity.state === 'on') return 'yellow';
          else return 'white';
        ]]]
tap_action:
  action: call-service
  service: |
    [[[
      if (entity.state == 'off')
        return 'input_boolean.turn_on';
      else
        return 'input_boolean.turn_off';
    ]]]
  service_data:
    entity_id: input_boolean.mail_present

Can you share your code to see what’s wrong with it ?

I keep getting this error also. I believe its a syntax error. I have to do some more digging.

hi Jerome

I am wondering if you can help me with a similar issue. I am trying to get my garage door opener working. I am unable to set it up as a cover and it only shows as a sensor so I have set up scenes in the Tuya app to open and close the door. The scenes work if i create buttons just for them but I would like to have a button which shows the garage door icon (by state) and runs the open or close scene depending on the state. I have adjusted your code below but get the following error:
Failed to call service scene/open_garage_door. Service not found.

can you see what I am doing wrong? FYI I am very new to HA

type: custom:button-card
entity: binary_sensor.garage_door
name: |
  [[[
    var status = states["binary_sensor.garage_door"].state;
    return 'Turn it ' + (status === 'on' ? 'off' : 'on') 
  ]]]
color: white
show_state: true
tap_action:
  action: call-service
  service: |
    [[[
      if (entity.state == 'off')
        return 'scene.open_garage_door';
      else
        return 'scene.close_garage_door';
    ]]]
  service_data:
    entity_id: binary_sensor.garage_door
icon: hass:garage
state:
  - value: 'on'
    icon: hass:garage-open
    color: rgb(255,0,0)
  - value: 'off'
    color: rgb(68,115,159)

Thanks

I’m sorry, I don’t have scenes myself and it’s hard to debug remotely :frowning:
Maybe the call-service action does not support scenes (I have input_boolean in my case)

To call a scene, you need to call the scene.turn_on service, open_garage_door is not a service but a scene,

Your call should be built like that:

service: scene.turn_on
data: {}
target:
  entity_id: scene.open_garage_door

or

service: scene.turn_on
data: {}
target:
  entity_id: scene.close_garage_door

got this working, thanks guys