Updating an input_datetime every minute

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