Updating an input_datetime every minute

So… go easy on me as I’m a complete noob, still trying to get a grip on the idiosyncrasies of templates.

I don’t understand enough about Home Assistant to know how to get the automation to keep running for the duration of time that I want the updating to continue, nor to get it to compute and ‘write’ the update once per minute.

Do I use a ‘repeat’ action with a while ‘loop’ for this?

Perhaps start by describing the actual real world problem you want to solve. There may be a better way to accomplish it using state changes rather than loops. This may avoid an XY Problem.

2 Likes

Thanks Tom. As is often the case with extreme noobness, my question was a bit daft and I figured it out eventually. May not be the best solution, but I’ll describe what I was doing and let me know what you think.

As an exercise for understanding how it all works, I wanted to create a helper assisted timer, where the current date/time is saved as a helper, then a helper defined time is added to that date/time, to define an end time for the timer. Then I wanted a third helper to be updated every minute to reflect the time remaining on the timer. Placing all these in Lovelace, I figured this would be a good little exercise to get on top of some of the basics and see exactly what my tinkering was producing.

So I ended up using a repeat action, with the while defined with a template condition checking the endtime minus the now() time remained larger than 1. I got the sequence to first update the remaining time helper, then popped a delay of 60 seconds after that.

Works well enough.

Another way to do it is with some helpers, a template sensor and some state triggered automations. I have this timer that switches on my porch light if I’m expecting a food delivery at night:

Screenshot 2021-08-07 at 12-59-47 Overview - Home Assistant

I set the time with the slider and turn the switch (input_boolean) on.

Input boolean:

pizza_delivery:
  name: Pizza Delivery
  icon: mdi:pizza

Input Number:

pizza_delivery_time:
  name: Delivery Time
  min: 15
  max: 120
  step: 5
  unit_of_measurement: min
  icon: mdi:update

Sensor (updates every minute, or if either the input boolean or input number are changed):

- platform: template
  sensors:
    pizza_delivery_time_remaining:
      friendly_name: 'Time Remaining'
      value_template: >
        {% if is_state('input_boolean.pizza_delivery', 'on') %}
          {{ [ (states('input_number.pizza_delivery_time')|int - (as_timestamp(now()) - as_timestamp(states.input_boolean.pizza_delivery.last_changed)) / 60)|round(0) ,0 ]|max }}
        {% else %}
          0
        {% endif %}
      unit_of_measurement: "min"

Automations:

- id: bd7c3114-4945-4c0b-a003-8c3d84f069ca
  alias: 'Pizza Delivery On'
  trigger:
    platform: state
    entity_id: input_boolean.pizza_delivery
    to: 'on'
  action:
  - service: light.turn_on
    data:
      entity_id: light.lifx_front_porch
      brightness: 255
      kelvin: 2700

- id: ddb3aaa9-544a-45cf-b08b-6a6dcd82848b
  alias: 'Pizza Delivery Off'
  trigger:
    platform: state
    entity_id: input_boolean.pizza_delivery
    to: 'off'
  action:
  - service: light.turn_off
    entity_id: light.lifx_front_porch

- id: e0a18608-491a-48e6-90f6-fa0b1a87a0ac
  alias: 'Pizza Delivery Reset'
  trigger:
    platform: numeric_state
    entity_id: sensor.pizza_delivery_time_remaining
    below: 1
  condition:
  - condition: state
    entity_id: input_boolean.pizza_delivery
    state: 'on'
  action:
  - service: light.turn_off
    entity_id: light.lifx_front_porch
  - service: input_boolean.turn_off
    entity_id: input_boolean.pizza_delivery
  - service: notify.telegram_general
    data:
      title: '*Information*'
      message: "Pizza delivery timer expired. Turning off front porch light."
  - condition: state
    entity_id: input_boolean.dnd
    state: "off"
  - service: tts.google_translate_say
    entity_id: media_player.entranceway_speaker
    data:
      message: "Pizza delivery timer expired. Turning off front porch light."

This was written quite a while ago and could probably do with a bit of rework to combine the first two automations. But it works and I have a lot of energy monitoring configuration to do…

1 Like

Thank you for sharing that implementation: I’m learning a lot from it.
Although the Home Assistant docs are expansive, for me, nothing beats seeing a real world implementation where the context of desired outcome is fully understood before diving into the code.