Input Timer to Turn Off Power Plug - Help with Basic Setup Please

Hi,
I am at a loss and mainly due to having no experiencing with creating these from scratch. I have tried examples shown in the forums but not having any luck and suspect it is easy but perhaps missing the obvious.

The Goal: I have a power plug that I would like to manually control from Lovelace using a Timer.
Concept: To be able to manually type (each time) how many minutes for it to remain on and then for it to turn off.
Device Name = Meross_Plug_05 - outlet
Entity ID = switch.meross_plug_05_outlet

Any chance someone could assist me with correct method in getting this working? Once I get one done, I am sure I can then apply principle to other simular tasks?

Hi there myhass140,

i did something like this for an input_datetime helper. I add 1800 sec (30min) to the time (now).

So that when the TV is turned on you have 30min from that point to watch and then the TV turns off automaticly.

If you replace the 1800 with some text input or so then you could convert min for example to seconds and add this to the now time.

service: input_datetime.set_datetime
data:
  timestamp: "{{ now().timestamp() + 1800 }}"
target:
  entity_id: input_datetime.fernseher_aus

for your case maybe

service: input_datetime.set_datetime
data:
  timestamp: "{{ now().timestamp() + (states('input_text.timer_value_min') | float * 60 ) }}"
target:
  entity_id: input_datetime.timer_helper

or forgett that and use the line with the Timestamp as “template trigger”

description: "Turn off after X min, where X = text input in min"
mode: single
trigger:
  - platform: template
    value_template: "{{ now().timestamp() + (input_text.time_in_min|float * 60) }}"
condition: []
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.meross_plug_05_outlet

Edit: I think you may have to first set some datetime to now when changing the timer. Because otherwise it is always x min till now()…would never trigger.

Edit 2:

Working set Timer Helper:

alias: TEST Timer
description: ""
trigger:
  - platform: state
    entity_id:
      - input_text.timer_min
condition: []
action:
  - service: input_datetime.set_datetime
    data:
      timestamp: "{{ now().timestamp() + (states('input_text.timer_min')|float * 60) }}"
    target:
      entity_id: input_datetime.timer_trigger_helper
mode: single

and then a second Automation

alias: Turn off at DATETIME helper
description: ""
trigger:
  - platform: time
    at: input_datetime.timer_trigger_helper
condition: []
action:
  - service: switch.turn_off
    data: 
       entity: switch.meross_plug_05_outlet
mode: single
1 Like

A shorter approach than using a timer is to create an input number helper like


input_number:
  meross_05_on:
    name: meross_05_on
    min: 0
    max: 120
    step: 1
    mode: box # uncomment this/drop it if you want to have a slider
    unit_of_measurement: min.

and an automation:


trigger:
  - platform: state
    entity_id: switch.meross_plug_05_outlet
    from: "off"
    to: "on"
    for:
      minutes: "{{ states('input_number.meross_05_on') |int(0) }}"
action:
  - service: switch.turn_off
    target:
      entity_id: '{{ trigger.entity_id }}'

2 Likes

@ [pedolsky] Thanks for this and for writing the code and is along the lines to what I am looking for.

I created it however what Card (or the likes) do I use on Home Screen for it to Pop Up or show the “input number” for me to enter on?

@ [tiny1337]: Thanks for that. I will play around with what you noted above as I need to get my head around all the options.

You can use the default Entities Card.

I suspect this is where I am getting lost. So now I have two things i.e. the The Helper and the Automation.

On my Homepage, I added two entity cards, one for the Helper for selecting the time to set and one for the Automation. I suspect that this is not what I am meant to do as it doesn’t work?

Do you mean Entity Card or Entities Card? Both are available.
What exactly is not working?

Agghhh. Silly me, I didn’t restart HA. Once I did that it “WORKED”. Excellent and now have something that is making some sense. By you writing the correct code helped a lot so far…

So would I be right in saying that I would need a min of 3 Entity Cards i.e.
1: Plug On/Off button. To turn the plug on.
2: The Helper for inputting preferred duration in advance
and
3:The Automation for activating it?

Apologies in advance for such what may seem basic questions. First time around is not so obvious.

Basicly you only need the Input Card in the Frontend (Lovelace)

The Automation is working in the background if setup. Also the Switch does not need to be shown.

You Put the switch on a card if you want to manually switch the plug or you want to see what the status is. Its more for you.

But you only “need” the Input to give it how many minutes the timer should be.

1 Like

Thanks and understood. Great points.

In my case and as I want to be able to quickly turn On/Off the Automation without going to Automation’s as it won’t always be used.
Switch On Card: If I do this, then by default it turns everything within card on (incl the automation). As I may not always use the Timer, I can just use the On/Off switch for the Plug that I added to the card. I hope that makes sense?

EXTRA QUESTION (please): Is there a way to see how long is left before it turns off i.e. a countdown timer within the card?

So if you have 1 automation for 1 plug with 1 helper then you would not need to turn the automation off.
Because the trigger timestamp would not change until you use the input again.

But sure you can do it that way.
Just play around with it…

For the Time that is left to see, I personaly use the bar timer card installed from hacs. Works great for me.

GitHub - rianadon/timer-bar-card: A progress bar display for Home Assistant timers

But if I don’t turn off the Automation, the next time I turn the plug on it will turn off. Being playing with it set for 1 min.

if you used @pedolsky 's Approach yes then it will always turn of after the set time.

I have this for my watercooker… 5min then off if it gets turned on.

Sorry, yes, at this point I am using @pedolsky example.
Just need to get my head around the basics of that one. I’ll play around with your one tomorrow as no doubt will learn a little more i.e. many ways to tie a pair of shoelaces.

This chat was to get anything working and and thanks to you both have now been successful. I have wasted many hours trying to figure it out by myself prior to posting here and failed! Now I just need to build on this. I’ll also try work out how to integrate the

Well yeah, i have been there. At the beginning I myself didn’t know anything about home assistant or yaml. But now i am getting better, bit by bit.

It’s an awesome hobby.

If you want to see the time left, use a timer helper and an adjusted automation instead:

Timer:

timer:
  timer.meross_05: # or whatever you like
    name: Timer Meross_05 # or whatever you like
    restore: true

Restart HA :wink: (if this is the first time you create a timer in yaml)

Automation:


trigger:
  - platform: state
    entity_id: switch.meross_plug_05_outlet
    from: "off"
    to: "on"
    id: start

  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.meross_05
    id: end

  - platform: event
    event_type: timer.cancelled
    event_data:
      entity_id: timer.meross_05
    id: cancel

action:
  - if:
      - condition: trigger
        id: start

    then:
      - service: timer.start
        data:
          duration: |-
            {{ states('input_number.meross_05_on') |multiply(60) |int(0) }}
        target:
          entity_id: timer.meross_05

      - service: switch.turn_on
        target:
          entity_id: switch.meross_plug_05_outlet

    else:
      - service: switch.turn_off
        target:
          entity_id: switch.meross_plug_05_outlet

1 Like

Do not hesitate to ask if anything is left unclear.

@ [pedolsky: Thanks for the “Timer Helper” suggestion & code.

Now, don’t laugh but how do I see the time left? I added both of those and then added to Lovelace as Entities. Timer shows as Idle and the Automation shows as “On”

Trigger is your Meross switch. As soon as you turn the switch on, the timer starts.