Can you add a template to a button card?

I am trying to switch the charging speed toggle of my car. Unfortuanetly it needs a service call to switch so can I do that with a toggling button?

Something like:

show_name: true
show_icon: true
type: button
tap_action:
  action: call-service
  service: skodaconnect.set_charger_max_current
  target: {}
  template:
    {% if is_state('sensor.superb_charger_max_ampere', 'Reduced')%}
      data:
        device_id: 8f9d4be11e497ae55a91c88dcacdd82c
        current: 254
    {% else %}
      data:
        device_id: 8f9d4be11e497ae55a91c88dcacdd82c
        current: 252
    {% endif %}
entity: sensor.superb_charger_max_ampere

EDIT Misread the OP…

Create a script to handle the logic, then have the tap action call the script.

Yeah that is a solution that i’ve applied to other buttons…but it would be so nice to have this solution self contained within the button definition

No, the frontend does not accept templates. You can make them template buttons.

template:
- button:
  - unique_id: set_charger_max_current
    name: Charger Max Ampere
    press:
    - action: skodaconnect.set_charger_max_current
      data:
        device_id: 8f9d4be11e497ae55a91c88dcacdd82c
        current: >
          {{ 254 if is_state('sensor.superb_charger_max_ampere', 'Reduced') else 252 }}

then in your dashboard

show_name: true
show_icon: true
type: button
entity: button.template_charger_max_ampere
2 Likes

I’d recommend to use button-card by RomRaider. With this, you can easily set what you want in the button itself. Seems easier to work with, but that’s a (your) personal choice. :slight_smile:

EDIT: you could use different tap actions on button-card as well, something like “one tap” is on/off, “double tap” is toggle between high and low and so on.

Yeah I was trying that but couldn’t get it to work. Do you know what is wrong with my syntax?

type: custom:button-card
show_name: true
show_icon: true
tap_action:
  action: call-service
  service: skodaconnect.set_charger_max_current
  target: {}
  data: |
    {% if is_state('sensor.superb_charger_max_ampere', 'Reduced')%}
      device_id: 8f9d4be11e497ae55a91c88dcacdd82c
      current: 254
    {% else %}
      device_id: 8f9d4be11e497ae55a91c88dcacdd82c
      current: 252
    {% endif %}
entity: sensor.superb_charger_max_ampere

The syntax for custom button card isn’t jinja, it’s JS (javascript). Secondly, no matter what you’re doing (Jinja or JS), you cannot template entire fields. You can only template individual fields. Thirdly, data is not a valid option for a tap_action service_call, it needs to be named service_data.

I.e.

type: custom:button-card
show_name: true
show_icon: true
tap_action:
  action: call-service
  service: skodaconnect.set_charger_max_current
  service_data:
    device_id: 8f9d4be11e497ae55a91c88dcacdd82c
    current: >
      [[[
        return (entity.state === 'Reduced') ? 254 : 252;
      ]]]
entity: sensor.superb_charger_max_ampere

Lastly, this may not work. It depends on how far custom button card lets you template into an object. Last I checked, templates only work in specific areas. I’m not sure if it allows it here, it’s been a while since I set these up.

Brilliant, thanks!

I know its an old post, but if you guys could help me with this one I would really appreciate

Can I use template for the name: itself?
I got a boolean that turns on when I arrive at work, but I would like to show as well the total time that I spent at work. For the time spent I work I created a sensor that combines all work locations in to one

- platform: history_stats
  name: Time at Work   # >>>>>>>>>>>>>>> Total at work per Today <<<<<<<<<<<<<<<<<
  entity_id: person.robert
  state: >
    {% set a = states('sensor.time_at_a')| float %}
    {% set d = states('sensor.time_at_d')| float %}
    {% set r = states('sensor.time_at_r')| float %}
    {% set f1 = states('sensor.time_at_f1')| float %}
    {% set f2 = states('sensor.time_at_f2')| float %}
    {% set f3 = states('sensor.time_at_f3')| float %}
    {{(a + d + r + f1+f2+f3)}}
  start: "{{ now().replace(hour=0, minute=0, second=0) }}"
  end: "{{ now() }}" 

what would be the proper syntax to show the above? I am keep getting the [object Object] and I tried few different scenarios wit curly brackets and ’ or "

show_name: true
show_icon: true
type: custom:button-card
styles:
  card:
    - font: 12px/20px Roboto
    - background: radial-gradient(rgb(58,0,228,50%), rgb(0,0,0))
    - box-shadow: 0px 0px 20px rgb(18,81,219,100%)
tap_action:
  action: toggle
entity: input_boolean.nfc_flagstar
name: WORK #{{states(sensor.time_at_work)}} ← works in dev tools/template
icon: mdi:vacuum
show_state: false
color: rgb(255,0,0)
color_type: icon
aspect_ratio: 1/1

thanks in advance

Custom button card does not use Jinja templating, it uses javascript templates which cannot be tested in Dev Tools. According to the card’s docs, the name is templatable.

The first part of your comment is Off Topic for this thread. In the future start your own thread

History Stats Issue

You cannot use templates in the state of a History Stats sensor. As shown in the docs, it only accepts a string or list of strings.

image

thank you for your input, I was not trying to hijack an old post and especially go off topic. My one and only goal was to show how I arrived at the stage that I am stuck on