antfurn
(Antfurn)
May 7, 2026, 9:24pm
1
Hi, Can anyone tell me or point me to the docs so I can get an automation to do a couple of multiplications. The below isn't working with the error msg:
Error: ValueError: Template error: multiply got invalid input '0.0' when rendering template '{{ states("sensor.xl_tool_3_grams") | multiply(0.01) | multiply( "changePercent" ) | float }}' but no default was specified
variables:
fromPercent: '{{ trigger.from_state.state | float }}'
toPercent: '{{ trigger.to_state.state | float }}'
changePercent: '{{ trigger.to_state.state | float - trigger.from_state.state | float }}'
usedGrams: >-
{{ states("sensor.total_grams") | multiply(0.01) | multiply(
"changePercent" ) | float }}
How can I get it to do:
usedGrams = ( sensor.total_grams / 100) * (toPercent-fromPercent)
From the traces, I can see 'changePercent' is getting set to the value I expect.
Previously I had this working with the below. I just can't get it to also multiple the final value I need by the number of percent changed [ * (toPercent-fromPercent) ]
variables:
fromPercent: '{{ trigger.from_state.state | float }}'
toPercent: '{{ trigger.to_state.state | float }}'
usedGrams: >-
{{ states("sensor.total_grams") | multiply(0.01) | float }}
Thanks for any help, Ant
You didn't change the state string to a float in usedGrams and variables don't get quotes:
variables:
fromPercent: '{{ trigger.from_state.state | float(0) }}'
toPercent: '{{ trigger.to_state.state | float(0) }}'
changePercent: '{{ toPercent - fromPercent }}'
usedGrams: >-
{{ states("sensor.total_grams") | float(0) | multiply(0.01) | multiply(changePercent) }}
Without the changes shown above the template was basically asking something like...
What is one percent of the words "one hundred sixteen" times the word "changePercent"?
2 Likes
antfurn
(Antfurn)
May 7, 2026, 10:59pm
3
Thank you. I've been trying to find what fully describes all the syntax nuances, they seem very context dependant.
Nick4
(Nick)
May 8, 2026, 9:17am
4
Hi, welcome to the forum!
Does it work now?
Troon
(Troon)
May 8, 2026, 9:20am
5
It's all pretty logical once you're familiar with it. Start here:
Troon has provided a link above to the documentation.
I'm not sure what you mean by "context dependent" in the above example... if you can clarify, we'll try to provide an answer.
antfurn
(Antfurn)
May 8, 2026, 10:54pm
7
Things like when I need to use ' vs "
In the below, the editor has replaced all my (and the supplied above fixes) ' with " or indeed \" - all very confusing!
antfurn
(Antfurn)
May 8, 2026, 10:57pm
8
Thanks for the help, I seem to have it working now. The complete work flow to get all the parts of the puzzle to be able to get the below script to work has been quite the journey - but it is working
alias: Print progress - update used filament
description: ""
triggers:
- entity_id:
- sensor.prusalink_progress
for:
hours: 0
minutes: 0
seconds: 5
trigger: state
conditions:
- condition: state
entity_id: sensor.prusalink
state:
- printing
- condition: template
value_template: "{{ trigger.from_state.state|float(0) < trigger.to_state.state|float(0) }}"
actions:
- variables:
fromPercent: "{{ trigger.from_state.state | float }}"
toPercent: "{{ trigger.to_state.state | float }}"
changePercent: "{{ toPercent - fromPercent }}"
usedGrams_1: >-
{{ states("sensor.xl_tool_1_grams") | float(0) | multiply(0.01) |
multiply(changePercent) }}
usedGrams_2: >-
{{ states("sensor.xl_tool_2_grams") | float(0) | multiply(0.01) |
multiply(changePercent) }}
usedGrams_3: >-
{{ states("sensor.xl_tool_3_grams") | float(0) | multiply(0.01) |
multiply(changePercent) }}
usedGrams_4: >-
{{ states("sensor.xl_tool_4_grams") | float(0) | multiply(0.01) |
multiply(changePercent) }}
usedGrams_5: >-
{{ states("sensor.xl_tool_5_grams") | float(0) | multiply(0.01) |
multiply(changePercent) }}
- action: spoolman.use_spool_filament
metadata: {}
data:
id: "{{ states(\"number.spoolman_filament_id_tool_1\") | int(0) }}"
use_weight: "{{usedGrams_1}}"
- action: spoolman.use_spool_filament
metadata: {}
data:
id: "{{ states(\"number.spoolman_filament_id_tool_2\") | int(0) }}"
use_weight: "{{usedGrams_2}}"
- action: spoolman.use_spool_filament
metadata: {}
data:
id: "{{ states(\"number.spoolman_filament_id_tool_3\") | int(0) }}"
use_weight: "{{usedGrams_3}}"
- action: spoolman.use_spool_filament
metadata: {}
data:
id: "{{ states(\"number.spoolman_filament_id_tool_4\") | int(0) }}"
use_weight: "{{usedGrams_4}}"
- action: spoolman.use_spool_filament
metadata: {}
data:
id: "{{ states(\"number.spoolman_filament_id_tool_5\") | int(0) }}"
use_weight: "{{usedGrams_5}}"
mode: single
It almost never matters... Templates in YAML - The quoting rule
That's an artifact of how UI generated automations and scripts are stored it doesn't actually mean anything.