Turn switch off after X minutes based on input_number value

I have a switch that I want to turn off after a given number of minutes. The number of minutes is set by an input slider. However it’s not switching off after the selected time.

Any ideas as to what I’m doing wrong?

automations.yaml

- alias: Niu Manual Charge
  initial_state: true
  trigger:
    platform: state
    entity_id: switch.41034600cc50e3d1cbd7
    to: 'on'
    for:
      minutes: "{{ states('input_number.niu_battery_manual') }}"
  condition:
    - condition: state
      entity_id: input_select.niu_charge_type
      state: 'Manual'
  action:
    service: switch.turn_off
    data:
      entity_id: switch.41034600cc50e3d1cbd7

configuration.yaml:

input_number:
  niu_battery_manual:
    name: Battery Manual Time
    min: 1
    max: 7
    step: 1
    icon: 'mdi:battery-unknown'

input_select:
  niu_charge_type:
    name: Niu Charging
    icon: 'mdi:ev-station'
    options:
      - Manual
      - Bike

I believe an input_number returns a float value. Therefore your template will assign to minutes: float values like 2.0, 3.0, etc instead of integer values like 2, 3, etc.

Try this:

      minutes: "{{ states('input_number.niu_battery_manual') | int }}"

Confirmed; it returns float values.

Screenshot from 2020-03-10 16-41-45

1 Like

Ahh! I got the basics of the code from elsewhere and that had the input_number cast to a float which didn’t work either. I’ll give that a try now. Thanks.

Yup, that works. Thanks.

1 Like

Actually home assistant is written in python so states are text values. Hence you need to convert to integer or float to do numeric comparisons.

1 Like

Good to know. Thanks.