Input_number not working as to: for: trigger (as minutes)

I have the following automation that works switches a switch off if it’s been on for a predefined period of time (in this case 10 minutes):

- alias: Niu Manual Charge
  #initial_state: true
  trigger:
    - platform: state
      entity_id: switch.41034600cc50e3d1cbxx
      to: 'on'
      for:
        minutes: 10
  condition:
    - condition: state
      entity_id: input_select.niu_charge_type
      state: 'Manual'
  action:
    - service: switch.turn_off
      entity_id: switch.41034600cc50e3d1cbxx

However, I created an input_number that I want to be able to manually select the time period after which the automation is triggered. But this just isn’t working for me:

input_number:
  niu_battery_manual:
    name: Battery Manual Time
    min: 15
    max: 480
    step: 15
    icon: 'mdi:battery-unknown'
- alias: Niu Manual Charge
  #initial_state: true
  trigger:
    - platform: state
      entity_id: switch.41034600cc50e3d1cbxx
      to: 'on'
      for:
        minutes: "{{ states('input_number.niu_battery_manual') | int }}"
  condition:
    - condition: state
      entity_id: input_select.niu_charge_type
      state: 'Manual'
  action:
    - service: switch.turn_off
      entity_id: switch.41034600cc50e3d1cbxx

Any ideas as to why this isnt working?

Hey Phill, does the template itself work?
Go to Developer Tools -> Templates and paste:

{{ states('input_number.niu_battery_manual') | int }}

Should spit out an integer. I’ve tried setting a trigger with a template similar to yours, works for me. Try setting it to “seconds” for testing maybe? Maybe your switch turns off or is unavailable while it’s waiting.

Thanks, that returns the correct value. I’ve no idea then why it’s not working as part of my automation.

I’d guess it doesn’t stay on but is unavailable or something similar. Check the states history if there’s something sketchy. Also try using seconds: instead of minutes: to make sure the automation itself is okay (I think it is).
As a quick fix you could use the first automation (without the “for:”) and just add two actions (before the turn_off service call):

  - delay:
      minutes: {{ states('input_number.niu_battery_manual') | int }}
  - condition: state
      entity_id: switch.41034600cc50e3d1cbxx
      state: 'on'

That’s an interesting alternative. I’ll give it a try. Thanks.

Is ths supposed to go under the action section before the switch is turned off?

Yes, the delay and the condition are both actions you need to put before the turn_off service.

So delay is a top level command eg:

trigger:
 -something
delay:
 -something
condition:
 -something
action:
 -do_something

rather than

action:
 -delay: 10 mins
 -do_something

No, they are both actions.

Try this as your automation, bear in mind there is probably something wrong here, since the for: template should work. Have you tried using seconds: instead yet?

- alias: Niu Manual Charge
  #initial_state: true
  trigger:
    - platform: state
      entity_id: switch.41034600cc50e3d1cbxx
      to: 'on'  
  condition:
    - condition: state
      entity_id: input_select.niu_charge_type
      state: 'Manual'
  action:
    - delay:
        minutes: {{ states('input_number.niu_battery_manual') | int }}
    - condition: state
      entity_id: switch.41034600cc50e3d1cbxx
      state: 'on'
    - service: switch.turn_off
      entity_id: switch.41034600cc50e3d1cbxx

Thanks for the clarification and the example. I’ve actually got something similar to your example, hence why I was getting confused with where you appeared to be saying to place the delay.

It seems that the problem might actually be the switch becoming unavailable sporadically for whatever reason. I need to look into that.

Thanks for the help.

Yes, that’s what I think as well. Might just be a coincidence the template doesn’t work. If you’re using an ESP based Chip (Sonoff, Tuya,…) you could consider flashing it to remove the cloud dependency