Need some help from a template expert!

Ok…Well this probably doesn’t need to be solved by an expert, but I’m still struggling with wrapping my head around this template stuff. I’m pretty sure what I want to do is really easy, I’m just struggling with getting it into an automation.

I have a input number helper entity that has a start value of “45”
I want to build an automation that everyday at a specific time (say 10pm) it subtracts that value by 1 until it the entity ultimately reaches 0. Can someone give me some direction of how to do the math inside an automation?

Any help is GREATLY appreciated!

If you have defined the steps of the helper with ‘1’, you could use input_number.decrement :


trigger:
  - platform: template
    value_template: "{{ states('sensor.time') == '10:00' }}"

 action:
  - service: input_number.decrement
    target:
      entity_id: input_number.xxxx

Alternatively:


- service: input_number.set_value
  data:
    value: |
       {% set in = states('input_number.x') |int(0) %}
       {{ in - 1 }}
   target:
    entity_id: input_number.x

2 Likes

You can just use a time trigger. OP might not even have a time sensor configured.

trigger:
  - platform: time
    at: "22:00"

Fair point. But I seem to remember that platform: time evaluates every second, sensor.time only once per minute.

I’m wondering why the time platform would need to evaluate at all? I confess I haven’t dug deep enough to figure out how the time platform actually works but I would have assumed the trigger would be initialized to fire at the time specified and Bob’s you’re uncle.

I’ve always tended to avoid templates if an alternative trigger/condition etc would do because I was under the impression the “native” platforms were more efficient.

Perhaps someone with a better understanding than myself will jump in and clarify. I’m curious now.

I believe that in an automation trigger in won’t make any difference because the trigger is being continuously evaluated in either case.

In a sensor using now() will be limited to evaluating once per minute.

And sensor.time only updates itself every minute.

But I don’t think using now() or sensor.time in a trigger limits the evaluation of the trigger to only once a minute.

Found the topic: With a single Automation, how do you turn lights on between two set times and then the lights off? - #22 by Mutt