Lenovo / Tuya WiFi motion detectors - Fix always detected issue

Hi all,
Like a few others he in Australia, I recently purchased some cheap Lenovo / Tuya WiFi motion detectors to use with HA, ALARMO and a couple automations to turn on the lights, where speed isn’t that important. They were on sale for $4 each and by the looks a few fellow Aussies bought them too! They are cheap, they are WiFi but they work good enough for my needs!

Anyway, the issue is though, these detectors need to be added to HA with the official Tuya / Smartlife addons (unless you flash them) and they report a “detected” state the entire time…

So, I wrote a couple automations to fix this and again, thought I’d share with you wonderful folks and post it all here, step by step how I got to work… :smiley:

The first, “motion_detector_reset_state_after_motion” does as it says on the box, 10 seconds after detecting motion, this will reset the sensor state to clear. The second automation simply runs the first Automation when HA first starts up. I have multiple sensors, so this on HA startup resets all my detectors to clear.

To install you must have the motion detectors installed in Tuya, have HACS and enable & install the python script “set_state.py”.

  1. Install your motion detector into the Tuya addon via the Tuya / Smart life app. I had to click add manually, then select wifi motion sensor. For ease of this, call them “Motion Sensor 1”, “Motion Sensor 2” etc etc.

  2. Edit your HA configuration.yaml and add the following line:
    python_script:

  3. Create a directory called “python_scripts” under your main HA install directory. ie: /homeassistant/python_scripts. Create a new file called “set_state.py” and copy & paste the contents of:
    code-snippets/motion_detector_set_state.py at c8bfaec1adf1166fb0a14da3b360afcebad0eeb4 · AdamGit69/code-snippets · GitHub
    Save file and reboot HA.

  4. Create a new automation called “Motion Sensor 1 - Reset State After Motion” and copy & paste the contents of:
    code-snippets/motion_detector_reset_state_after_motion-AUTOMATION at main · AdamGit69/code-snippets · GitHub
    Edit it to suit if you didn’t name your detectors “Motion Sensor 1” etc earlier, otherwise you’ll need to change the two entity_ids to point to your motion detector. You will need to create one of these automations for each one of your detectors.

  5. Create another new automation called “Motion Detector Reset All States On Startup” and copy & paste the contents of:
    code-snippets/motion_detector_reset_all_states_on_startup-AUTOMATION at main · AdamGit69/code-snippets · GitHub
    Edit it to suit, changing the entity_ids to point to your automation(s) you created in step 4.

And that’s it, your motion detectors should now function as you’d expect…

All files and the instructions are on my GitHub here:

More info on the set_state.py python script can be found here:

Happy HA’ing :slight_smile:

EDIT: SEE MY COMMENT BELOW FOR MY AUTOMATION TO TURN ON AND OFF LIGHTS.

🙂

1 Like

That’s a nice project. Thanks for sharing with the community.
I would suggest taking on the project of turning this into a couple of blueprints. You are already 85% of the way there, and it is easier for the newer members of the community to click the download button and add the entity names, rather that dive into the world of Github and the text editor.
If you decide to take this on and need any help, feel free to ping me.

1 Like

Ahh thank you for your kind words, good idea too, I’ve been meaning to learn how to make blueprints, proper cards and stuff but I just haven’t time to learn yet! I just whip these things up for myself mainly, get them working enough for me, figure I’d share and then move on to the next project. lol I will get onto learning blueprints though! Thanks for the prompt to get off my butt and learn. :slight_smile:

1 Like

Bonus:
Here is the automation I use to turn on my lights when motion is detected by one of these devices. This is NOT that fast but it works good enough for places like my doorway, carport or shed. It can take a couple seconds to turn on once motion is detected, so I wouldn’t use it on a staircase for example! lol

Anyway, when motion is detected and is sent to HA, this automation checks first that the light is not already turned on (ie with the switch or whatever), then it turns on the light and waits until no motion is detected for 30 seconds and then turns them off again. If the light was already turned on this will not turn it off. It will only turn the light off again if this automation turned it on. The 30 second time along with the 10 seconds in the above automations means if no motion is detected in roughly 40 seconds, the lights will turn off… If you want to make the light go off quicker edit this time but I suggest a minimum of 30 seconds or you could end up with a disco… :slightly_smiling_face:

There is a link below to the YAML, I suggest creating a new automation, copy & paste the contents, and then switch to visual editor to find all your device and entity ids.

You will need to edit (in visual as I said is easier):

The when section: edit to the Entity ID of your motion detector and the “to” should be set to detected

The if section: edit the device id to the light you want to check is off. The condition should be XYZ light is off

The do section: Change the first part, the “light turn on” dropdown thingy, to the target device of your light
The wait for 1 trigger part, set your motion sensor device id, the trigger should be XZY STOPPED detecting motion. SET THE DURATION TO 30 SECONDS.
Finally change the “light turn off” part, to the target device of your light.

Automation YAML can be found here:

P.S. I will learn to make these blueprints soon! :slight_smile:

Believe it or not, it will save you work in the end trying to help folks find Github, pasting in the wrong place, and such…

1 Like

I’m having a read now while I have some time. :slight_smile: