Delay Templating Syntax + Timer

Hi everyone, just getting started with my first automation which uses a switch for an e-bike charger.

image

Essentially I place a current voltage reading and if I choose to do an 80% charge I want a countdown timer to begin after doing the calculations.

I’m confused on how to do the calculations within the delay template and then pass that on to a timer. Here’s what I have to so far:

- id: '1613105679210'
  alias: Bike Timer
  trigger:
  - platform: state
    entity_id: switch.bike_charger
    to: 'on'
  condition:
  - condition: state
    entity_id: input_boolean.bike_timer
    state: 'on'
  action:
  - delay:  "00:{{ states('input_number.voltage') | int }}:00"
  - service: switch.turn_off
    entity_id: switch.bike_charger

The calculations I need to do for the delay are:
((51.5 - input_number.voltage) / 0.8) <round this answer *25

Then I would need to convert minutes to HH:MM:SS for the timer.

Appreciate your patience and assistance, thanks!

Try this:

- id: '1613105679210'
  alias: Bike Timer
  trigger:
  - platform: state
    entity_id: switch.bike_charger
    to: 'on'
  condition:
  - condition: state
    entity_id: input_boolean.bike_timer
    state: 'on'
  action:
  - delay:
      minutes: "{{ (((51.5 - states('input_number.voltage') | int ) / 0.8) | round) * 25 }}"
  - service: switch.turn_off
    entity_id: switch.bike_charger

Thanks! Is there a way to pass the state of that calculation on to a timer? Or maybe I should say, can you assign an entity to this delay so its state can be captured?

You can use the same template to calculate a timer’s duration.

Service timer.start

Thank you for pointing me towards that. I have updated my automation file but am now having two issues:

  • The time is calculated correctly however when the timer starts it is a few minutes less than what is calculated. Ex: 01:40:00 runtime is started as 1:38:41

  • The input_boolean condition has no effect. The state shows up correctly under the developer options when toggled but it’s not working in the automation.

Here’s the updated code:

- id: '1613421612713'
  alias: Turn On Bike Timer
  trigger:
  - platform: device
    type: turned_on
    device_id: d441d294531a50aad538d81c66e3d46f
    entity_id: switch.bike_charger
    domain: switch
  condition:
  - condition: state
    entity_id: input_boolean.bike_timer
    state: 'on'
  action:
  - service: timer.start
    entity_id: timer.bike
    data:
      duration: 00:{{ (((51.5 - states('input_number.voltage') | int ) / 0.8) | round)* 25 }}
  mode: single

Thanks in advance.

How are you testing the automation?

  • If you are triggering the automation manually, it will skip the trigger and condition.
  • If you are triggering the automation by turning on switch.bike_charger, the State Condition will only allow execution of the action if the input_boolean is on.

I am turning on switch.bike_charger through the Lovelace UI entity card. I then toggle the input_boolean.bike_timer (in the same manner) but the Countdown timer remains idle.

If you’re doing it in that order, the automation will not trigger.

The moment you turn on the switch, the automation checks the state of the input_boolean. If it’s off, the action is not executed. Turning on the input_boolean after the switch is already on will not trigger the automation.

Turn on the input_boolean then turn on the switch; this sequence ensures the automation will execute its action.

FWIW, the extra templating you are doing for delay is unnecessary. Review the documentation for delay and you’ll see why my example employs a simpler format.

For timer, the default unit of time for duration is seconds. Therefore multiply your result by 60 and done (no need to tack on zeros as placeholders).

Ah ok that makes sense. Thanks I’ll give that a shot.

- id: '1613421612713'
  alias: Turn On Bike Timer
  trigger:
  - platform: state
    entity_id: switch.bike_charger
    to: 'on'
  condition:
  - condition: state
    entity_id: input_boolean.bike_timer
    state: 'on'
  action:
  - service: timer.start
    data:
      entity_id: timer.bike
      duration: "{{ (((51.5 - states('input_number.voltage') | int ) / 0.8) | round) * 1500 }}"
  mode: single

I updated that automation with the duration however it is still giving me 1:38:41 instead of 1:40:00 when beginning.

What is the input_number’s value?

48.4, should this be float instead of int?

Edit: That didn’t change anything.

If the input_number is 48.4 then that template will report 6000 seconds which is 100 minutes or 1 hour and 40 minutes.

I tested creating a timer with a duration of 6000 seconds and it starts counting down from 1:40. I don’t what you’ve done to get a different result.

Strange it just started working for me… Well anyway I REALLY appreciate your assistance and patience with this one.

Glad to hear it sorted itself out.

Please consider marking my first post (which answers your original question) with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has an accepted solution. Effectively, it indicates your question was answered and helps other users find answers to similar questions.

:+1: Done, thank you.

1 Like