Decrease Number by X via code?

In home assistant I have this chips card but instead of setting the value to “55” I want to decrease it by 10:

chips:
  - type: template
    entity: number.cmf_phone_1_base_screen_brightness
    tap_action:
      action: perform-action
      perform_action: number.set_value
      target:
        entity_id: number.cmf_phone_1_base_screen_brightness
      data:
        value: 55

I tried different versions:

chips:
  - type: template
    entity: number.cmf_phone_1_base_screen_brightness
    content: "-10"
    tap_action:
      action: perform-action
      perform_action: number.set_value
      target:
        entity_id: number.cmf_phone_1_base_screen_brightness
      data:
        value: >-
          {% set current = states('number.cmf_phone_1_base_screen_brightness') | float(default=0) %}
          {% set new_val = (current - 10) %}
          {{ [new_val, 0] | max | min(255) }}

But no matter what I do always get the error when pressing the button:

Failed to perform the action number/set_value. expected float for dictionary value @ data['value']

Most of the frontend does not support templates.
Just for testing, try this: value: "{{55}}"

If that doesn’t work then it will not work. But you can create a script that you run from the frontend that does the templates.

But this seems to be a custom mushroom thing, may be it does support templates… Some mushroom expert is needed.

Or just test if it works

If value: 55 works then value: {{ 55 }} should also work if templates is active

This indeed did not work!

I solved it now by creating two scripts (increase / decrease) and a helper number that I increase / decrease and as second step of each script I assign the screen brightness this value.

More complicated but works!

You could also use a template sensor to create a new sensor with the value you want. It’s one extra sensor instead of two scripts and a helper.

1 Like

Even cards that support templates elsewhere rarely support then in card actions. Using a script is the best available method… though I don’t see why you need two scripts. You should be able to do it with one script that accepts variables then adds that variable’s value to the current value of number.cmf_phone_1_base_screen_brightness. Just pass a negative value when you want tapping the chip to decrease the brightness.

1 Like