Buttons to increase and decrease temperature

Im trying to create a button to increase the temperature by 0.5 deg on each press.

This is the code so far:

- type: "custom:button-card"
  tap_action:
    action: call-service
    service: climate.set_temperature
    service_data:
      entity_id: climate.home
      temperature: >
        {{ (state_attr('climate.home', 'temperature') +0.5) | float }}
    icon: "mdi:plus"
    show_name: false

Its displaying the message in the UI:

Failed to call service climate/set_temperature. expected float for dictionary value @ data['temperature']

If I replace the

{{ (state_attr('climate.home', 'temperature') +0.5) | float }}

with say 21, the button works changing the temperature to 21. Which makes me think the problem is within the {{ }} line. I recently added the '| float' but it still didn’t resolve the issue.

Ive tested the {{ }} line in dev-tools > template and all works.

You need to swap float with +0.5. First convert the string to float (numerical) then add to it. Would be my guess.

Edit: also, I am not sure if custom button-card supports Jinja templates? I think it would require a Javascript template according to the docs?

1 Like

Could make it a script and execute the script on button press.

I think as well that you have to use javascript template… Replace {{ }} by [[]]

I am not an expert in javascript template but I used this to return the attribute of an entity:

label: >
             [[[ var battery = (states['vacuum.roomba_courcelles'].attributes.battery_level);
                  return "Batterie: "+battery ; ]]]

So in your case I would do something like this (to be tested):

  temperature: >
             [[[ var temp = ((states['climate.home'].attributes.temperature) + 0.5);
                 return temp; ]]]
1 Like

@browetd again, you cant flip flop between JS and Jinja. Sorry, just noticed custom:button card. Disregard.

@denver

The error is coming from the fact that lovelace does not support templates. No matter what you place in there, it has to be a number and only a number. The easiest way to accomplish your goal is to make a script and call the script.

script:
  temperature_up:
    sequence:
    - service: climate.set_temperature
      data_template:
        entity_id: "{{ climate_id }}"
        temperature: "{{ state_attr('climate.home', 'temperature') + value | float }}"
  temperature_down:
    sequence:
    - service: climate.set_temperature
      data_template:
        entity_id: "{{ climate_id }}"
        temperature: "{{ state_attr('climate.home', 'temperature') - value | float }}"

Then in lovelace

- type: "custom:button-card"
  tap_action:
    action: call-service
    service: script.temperature_up
    service_data:
      climate_id: climate.home
      value: 0.5
    icon: "mdi:plus"
    show_name: false
5 Likes

Thanks Emphyrio, I had tried swopping float around without success. Hadn’t seen the fact regarding javascript templates.

‘Not an expert’??? well you solved my problem thank you browetd, worked perfectly.

Thank you petro, but the javascript above worked. Im still glad of your input because it will help me in script writing. I’ve been focusing too much on Jinja and need to understand other methods.

Well the custom button card is unique in the fact it allows JS templating in every field. So this solution will only work in that card, where as a script will work everywhere

Ahrr, that explains it. Thank you.

Is there a way to ensure that a maximum or minimum value is not exceeded?

@splinter You can try to use the following Javascript functions: “Math.min” and “Math.max” to keep the variable “temp” between the max and the min values… I have not tested so not sure it will work in HA…

Thank you.
I solved it this way:

 [[[ var temp = ((states['climate.riscaldamento_termosifoni'].attributes.temperature) + 0.5); if (temp > 24) [temp = 24]; return temp; ]]]
2 Likes