Automation silently changed on save

I want to change an entity value in a state trigger automation. The automation works as expected when the new value is supplied as a hard-coded number. But I want to use an input_number for the new value instead. So this is the automation I created:

alias: Increase PW reserve
description: Increase backup reserve to upper threshold during IO slots
trigger:
  - platform: state
    entity_id:
      - binary_sensor.octopus_intelligent_slot
    to: "on"
condition: []
action:
  - service: number.set_value
    data:
      value:
        {{ states('input_number.pw_upper_reserve') | float }}
    target:
      entity_id: number.poplar_house_backup_reserve
mode: single

But the automation fails, the trace showing:

Stopped because an error was encountered at 6 November 2023 at 23:30:01 (runtime: 0.01 seconds)
expected float for dictionary value @ data['value']

I had tested my value template in developer tools and it does return the value I want.

Now when I go to look at the automation again, it reads as follows:

alias: Increase PW reserve
description: Increase backup reserve to upper threshold during IO slots
trigger:
  - platform: state
    entity_id:
      - binary_sensor.octopus_intelligent_slot
    to: "on"
condition: []
action:
  - service: number.set_value
    data:
      value:
        "[object Object]": null
    target:
      entity_id: number.poplar_house_backup_reserve
mode: single

The new value in my template has been silently changed from a template to a null object. Saving the automation produced no error message, but I think it should have.

I since found some forum posts where it’s stated that you can’t use a template in a state trigger automation. Obviously I didn’t realise this and still can’t see it stated anywhere in the documentation. I don’t understand why this restriction exists but I don’t want to go into that. I just want to know: is there a correct way - or indeed any way - to achieve my objective of using an input number in a state trigger automation?

Are you sure the value should be on the line below?

This is a key/value pair.

value: {{ states... }}

This is a list called value with {{ states… }} as an element name and no value connected to it (=Null)

value:
  {{ states... }}

I don’t think that’s it. I tried placing the template expression on the same line as the “value:” key and after saving the automation it has been changed back to the exact same null object:

value:
        "[object Object]": null

Did you enclose the template in quotes? SIngle line jinja needs to be quoted. Or add the multiline character after value:. Strictly speaking, you should also provide a default for the float filter in case the input_number is unavailable, although this is probably unlikely.

So you could try:

value: "{{ states('input_number.pw_upper_reserve') | float(0) }}"

Or:

value: >
   {{ states('input_number.pw_upper_reserve') | float(0) }}
1 Like

If it’s on the same line as value: then the template has to be surrounded by quotes. If it’s on the next line you need value: > in the first line and no quotes around the template.

Edit: I was too slow

2 Likes

Thank you! That’s fixed it - or at least the automation is no longer being silently altered on save. It should trigger tonight at 23:30, so fingers crossed!

I’m not sure I’ll ever fully understand the quirks of template syntax.