Increment input_number value with a sensor attribute via automation

Hi,

I’m trying to get a total from a sensor’s attribute.

Every time this sensor receives a new state/attribute it would add the attribute’s number to the a input number and then give me a total.

Would it be possible?

I tried this but doesn’t work, pretty sure my code ain’t right.

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - sensor.baby_last_feed
condition: []
action:
  - service: input_number.set_value
    metadata: {}
    data:
      value: {state_attr('sensor.baby_last_feed', 'amount') + states('input_number.clem_total_feed')}
    target:
      entity_id: input_number.clem_total_feed

The template syntax is incorrect and it attempts to add a string value (must be converted to a number).

      value: "{{ state_attr('sensor.baby_last_feed', 'amount') | int(0) + states('input_number.clem_total_feed') | int(0) }}"

The value of the amount attribute may already be a number so the first int(0) may be unnecessary. However the state value of all entities, including an Input Number, is a string and must be converted to a number before performing any arithmetic operations (so the second int(0) is required).

A template appearing on the same line as the YAML option should be wrapped in quotes.

Reference

Important Templating Rules

1 Like

My hero, it works, thank you very much!

1 Like