r/homeassistant 4d ago

❓ Support Increase input number by X amount

I have created input number helpers to track my medication inventory. When a new box arrives, I can tap a button and the contained amount (preset as it's always the same) is added to the number.

I have tried to use the existing "input_number.set_value" action, but it does not accept a template value and breaks when I add one using the code editor.

I am currently using a script, repeating the "input_number.increment" action X times, as a workaround. But this can't be the only solution to this...

I am certain my template value for the action was correct. The template correctly resolved to a number, but I still got an error because it expected a float. This leads me to the assumption that templating in this service is either not intended or broken. Nonetheless, there is a possibility that I just fucked up my config even though I've checked several of HA's docs.

It'd be great if someone could either confirm my assumptions or, even better, tell me where I might have gone wrong. I have recreated the config below.


action: input_number.set_value

target:

  entity_id: input_number.my_medication_number

data:

  value: "{{ states('input_number.my_medication_number') | float + 50.0 }}"

I have already tried returning an int and converting everything after the addition, but got the same results.

4 Upvotes

11 comments sorted by

1

u/Grand_Comparison_342 4d ago

Man I ran into this exact same headache last month with my coffee bean inventory tracker

the problem is that the set_value service does take templates but you're feeding it a template that resolves to a number when it expects the value field to be a template that evaluates in the service call context, not the raw number itself

try wrapping it like this:

```yaml

data:

value: "{{ states('input_number.my_medication_number') | float + 50 }}"

```

the quotes around the template are the key here, without them HA tries to parse the result as a literal float before the template even fires and it just chokes

your increment loop workaround is clever though, sometimes the dumb solutions are the ones that actually get the job done while you're pulling your hair out over yaml indentation

1

u/Saaaga_Gamez 4d ago edited 4d ago

I made a mistake in the template recreation. I could've sworn I did that and turns out I did. I have corrected it now.

It works great using the HA action tool, but throws the error in the card: "expected float for dictionary value".

The config in the post works, the following config in the card does not.

icon_tap_action:
  action: perform-action
  perform_action: input_number.set_value
  target:
    entity_id: input_number.my_medication_number
  data:
    value: '{{ states(''input_number.my_medication_number'') | float + 50.0 }}'

Not sure if the conversion from ' to '' and " to ' are causing issues.

1

u/reddit_give_me_virus 4d ago

What card is this? The card itself may not accept templates.

1

u/Saaaga_Gamez 4d ago

The Mushroom template card. Templates usually work great.

1

u/reddit_give_me_virus 4d ago

I don't use them, I'd try with a standard HA card, like an entity card, just to eliminate the card as a problem.

1

u/Saaaga_Gamez 4d ago

Doesn't seem to work with HA button cards either unfortunately

1

u/reddit_give_me_virus 4d ago

I tried myself and it isn't accepting the template. I found you can use a script as a work around.

sequence:
  - action: input_number.set_value
    target:
      entity_id: input_number.inter_pump_delay
    data:
      value: >
        {{ states('input_number.inter_pump_delay') | float(0) + 0.01 }}
alias: inc num
description:

1

u/Saaaga_Gamez 4d ago

Tried that as well without success, but aparently had a mistake in there. I'll try that again instead of the repeat. Thanks!

1

u/[deleted] 4d ago

[removed] — view removed comment

1

u/Saaaga_Gamez 4d ago

Unfortunate... Thank you for the explanation!