Timed Switch: Trigger a timed action on an entity from the front end

Hi,

I have a boiling water tap that is connected to a smart plug. I don’t want the boiler to be on all the time as when we are out I want to save the energy. Rather than turn the smart plug off and then back on myself or by a pre-defined schedule, I want to be able to turn if off for a defined period of time, after which it will turn back on.

I have searched but haven’t found a similar topic, so I have played about a bit, as below. My questions are:

  1. How can it be improved?
  2. How can it be made properly generic so it can be called/applied by/to any switch entity?

Thanks so much for your advice

Dashboard button

Off
image

On - shows consumption
image

On Timer - shows time left on timer
image

Button config

type: custom:mushroom-template-card
primary: Tap
secondary: |
  {% if states('switch.hot_tap') == 'on' %}
    {{ states('sensor.hot_tap_power') }} {{ state_attr('sensor.hot_tap_power','unit_of_measurement') }}
  {% elif is_state('timer.switch', 'active') %}
    {{ '00:00:00' if 'timer.switch' == None else (as_datetime(state_attr('timer.switch','finishes_at')) - now()).total_seconds() | timestamp_custom('%H:%M:%S', false) }}
  {% endif %}
icon: mdi:faucet-variant
entity: switch.hot_tap
icon_color: >-
  {{ (states(entity) == 'on') |
  iif("var(--my-kitchen-active-color-rgb)","var(--my-inactive-color-rgb)")}}
tap_action:
  action: toggle
  confirmation:
    text: Are you sure?
hold_action:
  action: navigate
  navigation_path: '#duration'
double_tap_action:
  action: more-info

Associated entities

Script

alias: Generic Timer
sequence:
  - service: homeassistant.toggle
    metadata: {}
    data: {}
    target:
      entity_id: "{{ entity }}"
  - service: timer.start
    metadata: {}
    data:
      duration: "{{ states('input_number.generic_timer_duration') }}"
    target:
      entity_id: timer.switch
  - wait_for_trigger:
      - platform: state
        entity_id:
          - timer.switch
        from: active
        to: idle
  - service: homeassistant.toggle
    metadata: {}
    data: {}
    target:
      entity_id: "{{ entity }}"
description: ""
icon: mdi:camera-timer
fields:
  entity:
    selector:
      entity: {}
    name: entity
    required: true

Duration

Timer

Bubble Card Pop Up

type: vertical-stack
cards:
  - type: custom:bubble-card
    card_type: pop-up
    hash: '#duration'
    name: Select Timer Duration
    icon: mdi:camera-timer
    button_type: slider
    close_by_clicking_outside: false
    close_on_click: false
    entity: input_number.generic_timer_duration
    show_state: true
    show_last_changed: false
    show_attribute: false
    sub_button:
      - entity: timer.switch
        icon: mdi:play
        show_state: false
        show_name: false
        tap_action:
          action: call-service
          service: script.generic_timer
          data:
            entity: switch.hot_tap
          target:
            entity_id: switch.hot_tap

Using a timer in a Wait is a little counterproductive. By setting it up that way you lose the restart/reload survival benefit of the timer.

If you require the ability to display the remaining time you need a timer entity for each switch, at least for now, so generalization is going to be difficult.

Recent additions to the Assist integration make it possible to do “On for X minutes” type automations using the conversation.process service, but the ad hoc timers that method uses are not available as entities. So, it’s generalizable but it wouldn’t have all the functions of your current set up.

Thank you. Regarding the timer, would you have that separate and use the timer completing as a trigger then?

If you are happy with the way it’s working currently, keep it the way it is. If you use a good naming convention, you can use templating to get the name of the desired timer for each dashboard button. Just make sure you set the script’s mode to parallel.

You could set up an automation using an Event trigger for the timer.finished event, but it wouldn’t have direct access to the entity that was passed from the original button action. You would need either to follow a strict naming convention or to maintain a dictionary of timer: switch relationships so that the automation can toggle the appropriate device.

Personally, I would consider alternative methodologies. My goal is always to automate, not to create something that is, essentially, a fancy light switch on a tablet/phone. If you want the heater to turn off when no one is home, then do that… If you want it to turn back on when someone leaves work, or X minutes before the time you usually start cooking dinner, then do that.

Thank you, that makes sense. I do have other automations that react to presence, schedules, etc but for this particular use case where the time of day, duration and need to have the switch turn on after a (configurable) certain amount of time meant that I couldn’t quite find anything.