Light auto off timer

Hey everyone,

I’m new to the forum and to HA.
I am trying to configure a setup so when I turn my switch for the bathroom fan, it turns of in 15 min.

I’m running Hass.io and the switch is a Tp-link hs200.

Here is what I have so far…

(configuration.yaml)
timer:
bathroom_fan:
duration: ‘00:15:00’

(automations.yaml)

  • entity_id: timer.bathroom_fan
    service: timer.start
    alias: Timerswitch
    id: Timerstart
    trigger:
  • entity_id: switch.tplink_switch
    from: ‘off’
    platform: state
    to: ‘on’
  • action:
    • service: switch.turn_off
      data:
      message:
      alias: Timerstop
      id: Timerstop
      trigger:
      platform: event
      event_type: timer.finished
      event_data:
      entity_id: timer.bathroom_fan

I have the action of the light turning on and that activates the timer, but the light does’t shut off at the end of the respective time.

Not sure why the formatting is all screwed up in the post??? Your assistance is appreciated!
Cheers

2 Likes

If you’re going to stop exactly 15 minutes after going on why not just use a delay in the action of your automation.

For example:

- delay: '00:15:00'
- service: homeassistant.tun_off
  entity_id: switch.whatever_you_call_your_switch

…or you could have the automation trigger be that the switch has been on for 15 minutes.

trigger:
- platform: state
  entity_id: switch.tp_link_switch
  to: 'on'
  for:
    minutes: 15

Then forget about the timer and have the action just turn off the switch.

1 Like

Hi Jonathan,

Thanks for your response!

I placed your automation in automation.yaml and got a boat load of errors.
Do I need any text in configuration.yaml?

Thanks!

Hi! Many thanks for your response.

I put this automation into automation.yaml and got a boat load of errors.
Is there text to insert into configuration.yaml?

 trigger:
 - platform: state
   entity_id: switch.tp_link_switch
   to: 'on'
   for:
     minutes: 15
 action:
 - service: homeassistant.turn_off

Many thanks!

Please repost with formatting.
It’ll help us iron out the wrinkles :slight_smile:

lol didn’t know how to format it, but figured it out!

I think your indentations are off.

Here is an automation I use to cause a switch to turn off again after 1 second creating a toggle effect.

id: Turn_Off_GD_Sw
  alias: Toggle North Garage Door Switch
  trigger:
    platform: state
    entity_id: switch.garage_door_north_operator_switch
    to: 'on'
    for:
      seconds: 1
  action:
    service: homeassistant.turn_off
    entity_id: switch.garage_door_north_operator_switch

to modify for your use:

id: timed_off_bathroom_fan
  alias: Bathroom fan timer
  trigger:
    platform: state
    entity_id: switch.tplink_switch
    to: 'on'
    for:
      minutes: 15 
  action:
    service: homeassistant.turn_off
    entity_id: switch.tplink_switch

Try it to see if it works the way you want.

HI! Thanks for your reply, much appreciated.

Here is the error that came up from what you provided…

Testing configuration at /config
2018-01-28 22:49:35 ERROR (SyncWorker_0) [homeassistant.util.yaml] mapping values are not allowed here
in “/config/automations.yaml”, line 2, column 8
2018-01-28 22:49:35 ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/automations.yaml”, line 2, column 8

Any help you can provide would be great!

Thanks!

If you have several automations in your automations.yaml file you’ll need to put a - in front of the “id:”. make sure you put a space after the dash. like this

- id: timed_off_bathroom_fan
...

This is my only automation, I applied the dash just for the sake of trying it and now i have this error…

Testing configuration at /config
2018-01-29 02:20:32 ERROR (SyncWorker_0) [homeassistant.util.yaml] mapping values are not allowed here
in “/config/automations.yaml”, line 2, column 10
2018-01-29 02:20:32 ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/automations.yaml”, line 2, column 10

Here is my configuration…

- id: timed_off_bathroom_fan
    alias: Bathroom fan timer
    trigger:
      platform: state
      entity_id: switch.tplink_switch
      to: 'on'
      for:
        seconds: 5 
    action:
      service: homeassistant.turn_off
      entity_id: switch.tplink_switch

Thanks again!!!

Ok.

One more thing to check is if you have any extra stray spaces at the end of the first line.

Did you copy and paste it or did you type it in manually?

It’s saying you have something wrong in column 10. Before you added the dash and space it said column 8.

Maybe if you type it manually ensuring you syntax it may fix it.

Or maybe line 2 is actually the second line and there is problem between alias: and Bathroom.

The last thing is there only 2 spaces of indent on the second line?

In most cases your code should be starting on an odd numbered column. If you see an error saying column 8 or 10 I would be looking at it all being off by one spot in either direction. Everything should be on 1,3,5,7,9 and so on.

Try typing it in exactly this way below but remove the leading x’s. They are there to show you the proper amount of indentation for each line.

- id: timed_off_bathroom_fan
xxalias: Bathroom fan timer
xxtrigger:
xxxxplatform: state
xxxxentity_id: switch.tplink_switch
xxxxto: 'on'
xxxxfor:
xxxxxxseconds: 5 
xxaction:
xxxxservice: homeassistant.turn_off
xxxxentity_id: switch.tplink_switch
1 Like

IT’S ALIVE!!!

Thanks for walking me through this guys @finity, @jonathanp and @jivesinger. I really appreciate it.

Hope you all have a great week!
Cheers

2 Likes

What finally fixed it?

Edit: nevermind… I saw the solve indicator…

I’m having a very similar issue. The current setup, using device or state, only triggers after a light/switch gets turned on while all conditions are true. I want it to trigger without having to go from an off to an on state. For example, I have it set to turn the porch light off after 30 minutes, but only after sunset. If I turn it on before sunset nothing happens. If I turn it on after sunset it works as intended.
The “from” and “to” states are actions, a “to” state only triggers when it goes from another state to “to”. Is there an “is” state that I’m missing? This would solve the problem (there would probably be a polling interval attached)

You could simplify your trigger to just be 30 minutes after sunset. Turn off the light.
You can keep your current automation as well, so that if its turned on after sunset, it will turn off after 30 minutes as well.

unsure what are you trying to do… maybe best to share your code here so people can see and help out