automation: turn on lights when I enter the room
The LED striplights in a kitchen use 12v transformers and they are powered through smart plugs / sockets. The sockets are by TP-link – and they remember their on-off state if ever there’s a loss of power. The lights are set to turn off a few times each day and night, just in case a lot of lights are left on. The TP-link socket performs this schedule even if there’s a loss of wifi / internet. TP-link sockets do not need a hub. Traditionally, for ease of use, the lights should all be wired to one or two switches but my preference was for no switches. I wanted it so that if you entered the room the lights would just turn on.
Turning on lights is easy, turning them off later requires an automation routine
The first successful approach was to use a smart DLink motion sensor and connect it to IFTTT, a home automation platform that helps link up different smart devices. Every time the sensor was triggered a message was passed to the TP-link plug sockets to turn things on. In other words, IF the DLink motion sensor triggers THEN turn on the TPlink lights.
How it initially worked was ridiculously complicated: when the DLink motion sensor was triggered a message was passed to IFTTT which sent a message to a service called Stringify which started a timer for 20 minutes after which a message was sent to TPLink to turn off the lights. But then Stringify was acquired by Comcast and the service was closed to teach all home automation enthusiasts not to depend on on a cloud service ever again.
Make an automation routine with presence detection
So I looked for a better way to turn off the lights and it was my first attempt to write an automation. This is the idea:
- Wait for the DLink motion sensor to trigger IFTTT
- Wait for IFTT to send a message (webhook) to trigger Home Assistant
- When Home Assistant receives the IFTTT trigger, check the time: if it’s night start a night routine; if it’s daytime start a day routine
- The day routine: start a 20 minute timer and turn on the ceiling and floor lights
- The night routine: start a 20 minute timer and just turn on the floor lights
- Wait for the timers to complete. When 20 minutes is up turn off all the lights.
alias: off the ceilinglights trigger: event_data: entity_id: timer.ceiling_lights event_type: timer.finished platform: event condition:  action: data: entity_id: switch.ceiling_lights service: switch.turn_off alias: off the cupboardlights trigger: event_data: entity_id: timer.cupboard_lights event_type: timer.finished platform: event condition:  action: data: entity_id: switch.cupboard_lights service: switch.turn_off alias: motion day lightson trigger: event_data: action: call_service event_type: ifttt_webhook_received platform: event condition: after: 06:00 before: '23:00' condition: time action: data: entity_id: switch.ceiling_lights service: switch.turn_on data: entity_id: switch.cupboard_lights service: switch.turn_on data: entity_id: timer.ceiling_lights service: timer.start data: entity_id: timer.cupboard_lights service: timer.start alias: motion night lightson trigger: event_data: action: call_service event_type: ifttt_webhook_received platform: event condition: after: '23:00' before: 06:05 condition: time action: data: entity_id: switch.cupboard_lights service: switch.turn_on data: entity_id: timer.cupboard_lights service: timer.start
Evaluation of the above automation
It took some learning to use IFTTT to send a message to Home Assistant but that was the only way to use a motion sensor plug which DLink has discontinued and Dlink need not think that I will buy another product of theirs. On the plus side I now use a custom component for Dlink. And I am now using a RCWL radar/microwave sensor for presence detection.
Secondly the automation could be simpler as I discovered a ‘Delay’ command which should work instead of the ’20-minute timer’.
Thirdly there is no need for separate ‘switch off’ floor or ceiling light routines – I can turn everything off at once.