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


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


On - shows consumption

On Timer - shows time left on timer

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') |
  action: toggle
    text: Are you sure?
  action: navigate
  navigation_path: '#duration'
  action: more-info

Associated entities


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



Bubble Card Pop Up

type: vertical-stack
  - 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
      - entity: timer.switch
        icon: mdi:play
        show_state: false
        show_name: false
          action: call-service
          service: script.generic_timer
            entity: switch.hot_tap
            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.