Keep light on through automation

Hello,
I’m writing this post because i would like to know if the solution that I’ve done in order to solve my problem is right.

Problem:

I have 1 motion sensor and 1 light, I would like to turn on the light if there is a movement and keep the light on if there were motions in the last 10 seconds.

Sensor:

The motion sensor use a mqtt server to push the updates (‘motion’, ‘no motion’) the no motion is pushed by the sensor only after 1 minute of no motion, the motion is pushed every time there is a motion.
I’ve configured two motion sensor that report the data from one phisical motion sensor, one is used to trigger the automation and the other one is used to check if there were motions in the last 10 seconds.

Implementation:

The automation is implemented like this:

  • alias: ‘Rule 5 - Luci Cameretta ON’
    trigger:
    platform: state
    entity_id: binary_sensor.cameretta
    state: ‘on’
    condition:
    condition: and
    conditions:
    - condition: time
    after: “16:30:00”
    before: “21:00:00”
    - condition: state
    entity_id: ‘switch.autoluci’
    state: ‘off’
    action:
    • service: light.turn_on
      data_template:
      entity_id: light.soffitto
      brightness: >
      {% if now().strftime(“%-H”) | int > 14 and now().strftime(“%-H”) | int < 19 %}254{% else %}4{% endif %}
    • service: switch.turn_on
      data:
      entity_id: switch.autoluci
    • delay:
      seconds: 1
    • service: mqtt.publish
      data:
      payload: “no_motion”
      topic: “xiaomi/motion/158d00011733a1/status”
    • service: light.turn_off
      data:
      entity_id: switch.autoluci

The swith template autoluci is implemented like this:

  • platform: template
    switches:
    autoluci:
    friendly_name: ‘Automazione Luci’
    value_template: ‘{{ is_state(“light.soffitto”, “on”) }}’
    turn_on:
    service: autolight_service.trigger
    turn_off:
    service: light.turn_on
    entity_id: light.soffitto

And the autolight_service is a custom component implemented below:

The domain of your component. Should be equal to the name of your component.

import time
import logging
import os
import homeassistant.components as core
DOMAIN = ‘autolight_service’

TARGET_SENSOR = ‘binary_sensor.camerettaauto’
TARGET_SENSOR_MAIN = ‘binary_sensor.cameretta’

TARGET_GROUP = ‘light.soffitto’

Shortcut for the logger

_LOGGER = logging.getLogger(name)

def setup(hass, config):
“”“Setup is called when Home Assistant is loading our component.”“”

def handle_trigger_autolight(call):
    if not TARGET_SENSOR:
        _LOGGER.info('No sensor found')
        return
    can_tun_off = False
    
    while not can_tun_off:
        _LOGGER.info(hass.states.get(TARGET_SENSOR).state)
        if hass.states.get(TARGET_SENSOR).state == 'on':
            
            
            hass.states.set(TARGET_SENSOR, 'off')
        else:
            _LOGGER.info('No motion..')
        _LOGGER.info('Wait for 10 secs')
        time.sleep(10)
        if not (hass.states.get(TARGET_SENSOR).state == 'on'):
            _LOGGER.info('No motion turn all off and reset the motion main sensor state')
            core.turn_off(hass, TARGET_GROUP)
            hass.states.set(TARGET_SENSOR_MAIN, 'off')
            can_tun_off = True
hass.services.register(DOMAIN, 'trigger', handle_trigger_autolight)
_LOGGER.info('Auto Light registered')
return True

This automation/configuration works like a charm but I would like to know if is the right/simplest way in order to solve this problem.
Thank you

There are multiple ways to solve this, there is not one single way that is the best :slight_smile:. If you’re satisfied with the way you’ve solved it, keep with it. You can look on these other examples to see other ways of solving the same issue.

If you want to simplify and down size in the amount of lines to get this working you can look at AppDaemon and this script for the same end result AppDaemon motion detectionlights version 1.1

Or if you prefer wholly inside of HASS you can look at Turn on lights on and keep them on when motion detection

/R

1 Like

Thank you, I’ll have a look to the links seem really interesting!

1 Like

Check out my implementation as well. May provide more configuration options:
Github Repo

Looks interesting, thanks!