Turn off a switch after 3 hours after it was turned on

I am still new to Home Assistant but haven’t taken advantage of any automation’s yet. I have my phone charger plugged into a smart switch and can control it within Home assistant and by asking Alexa to turn on my smart plug (Alexa turn on phone charger). I would like to create an automation that will automatically turn off the smart plug after 3 hours when ever I turn it on. I feel this will be long enough to charge my phone, without leaving it continuously charge. I prefer to use Alexa to turn on my smart plug, and work weird hours so I will be charging the phone at different times on different days.

- trigger:
  - platform: state
    entity_id: switch.xyz
    to: 'on'
      hours: 3
  - service: switch.turn_off
    entity_id: switch.xyz

Wow that was fast, thanks will give it a shot and let you know.
Thanks again

This is a test to see if it worked. I thought I would put it in my /config/automations.yaml. When I went to run a Configuration validation, I got an error. I am sorry I was not able to get it to work. Would you mind pointing me in the right direction again.
Thanks again.

[]- trigger:

  • platform: state
    entity_id: switch.12803017840d8e8cxxxx
    to: ‘on’
    seconds: 20
  • service: switch.turn_off
    entity_id: switch.12803017840d8e8cxxx

When you post code you need to format it correctly, otherwise it is very difficult to read. See especially #11:

I think your problem is that you didn’t enter it correctly. I.e., you didn’t write correct YAML code. To fix it just delete the square brackets at the beginning.

1 Like

I am sorry, and thank you for your help. I will look more before reaching out. The last thing I want is a hand out, or to ask a question that has been answered before. Thank you again.

No need to apologize. Yes, that topic does talk a lot about how to ask for help, and there’s a lot of good things to consider there, but mainly I was using it to point out how to format code (i.e., point #11.) YAML is very touchy about indentation, and if code is not formatted correctly many problems cannot be easily seen.

Don’t be too hesitant to ask for help. Yes, many things are answered over and over again on this forum, and often that’s due to people asking before looking, but at the same time, it’s not always easy to find something relevant.

No worries! :smiley:

1 Like