Can someone help me write a code for a contact sensor

Hello, I’m wondering if someone can help me write a simple code for a contact sensor, I’m happy to reimburse for the time, considering I can’t really offer much else :frowning:.

Im looking for a door contact sensor to turn a light or lights on when a door is opened, but turn them off when a door is closed a second time with in a specific amount of time that could be entered on the smartapp (considering the first time a door is closed would be when someone goes into a room and closes the door initially). I would also like to add a scheduling ability as different lights would need to be triggered during day or night.

Any help is appreciated. Thank You.

You Might not need custom code for this, just a shortcut group.

Check this FAQ and see if it helps:

1 Like

Just one door? Can you describe the actual scenario? Like someone enters the bathroom and then exits within a certain amount of time.

I’ve tried literally every function thats built in and even some smart apps. The main thing I need is the function of a contact sensor causing an off action when closed the 2nd time. Basically this would be for a bathroom, Ive come to understand that motion sensors don’t work because they dont detect motion while someone is showering, humidity sensors work but it takes too long for humidity to drop down for actions to occur, so that left me with the current idea which it seems would work flawlessly.

Exactly right.

Person enters bathroom, light(s) turn on and door closes but lights remain on. Person leaves bathroom and closes door (technically 2nd time door closes within a short time) lights turn off.

In the event person opened door, then closed door to use restroom but left the restroom and never closed the door, then lights would turn off automatically at the set amount of time.

Your best bet is still motion sensors. The door opens and then closes scenario breaks when someone goes to the bathroom just to drop off some towels and does not shut the door. The next person after them will open and close the door and the lights will be out.

You just need to set a large time window for turning the light out after no motion is detected. Detecting humidity would also work for when someone is in the shower. The bathroom is one of the places I don’t automate. Your partner will not appreciate getting out of the shower in the dark. My partner for example wants absolutely no light on when she goes to the bathroom at night. I automated our guest bath, but not the master bath.

I was thinking to have both a motion sensor and door contact as well for those times that the door was left open, and the time out where the lights would turn off would be the scenario reset.

Any chance you can recommend someone that can code this, I really think it may work without my partner going crazy, I put a lot of thought into this. I think it will work.

I can code it, but I am still a little confused on exactly the sequence of events you want with both the contact sensor and the motion sensor.

I can code it. I will post an example of how to do this over the weekend.

I’ve actually had a lot of luck with my bathroom — I use a ceiling mounted motion sensor that picks up the shower, toilet area, entrance, sink. During the day, it turns on the all the lighting, and at night, it slowly dims up a cute ikea moon lamp with a very low wattage bulb over the toilet.

I’ve set the dip switches on the motion sensor to be highly sensitive and it only turns off after five minutes of inactivity. Seems to work. In fact, it’s the most reliable part of my ST setup.

1 Like

Thank you. Eric and Jodyalbritton. Is there a way for me to private message so I can give my phone number and explain over the phone.

Click on the picture of me and you will see “Private Message”

@April Let’s make this a code challenge. Can there be a badge for the Bathroom SmartApp that does not tick people off.


It doesn’t come up with a private message icon for now, I think its because I am new. Can you PM me.

@jodyalbritton - I accept your challenge :wink:


I need this for my bathroom.

@baldeagle072 @Jim

If it was not clear, I just spoke with @borya on the phone, and what he wants is for when someone enters the bathroom and closes the door the lights are. After some amount of time they exit the bathroom and the the lights turn off. He wants this to work using a door contact sensor and a motion detector. So the motion detector will take over anytime the door is open and the contact sensors will turn the lights on and off based on open close events.

If it is before that amount of time, the lights will stay on?

That is the voodoo

  1. Door Opens, Light comes on and Person Enters
  2. Door Closes lights stay on.
  3. Person Exits the bathroom and door opens.
  4. Door closes and the Lights go out.

He also said he wants the motion detector to work when the door is open and that motion could be used to determine bathroom occupancy.

There are other things to take into consideration. Like the door being left open, or someone just entering and exiting. I think I have a method for accomplishing this.