Tuya motion sensor is not supported in Home Assistant

@Joe_Hardy what i did was also ‘debouncing’ the ping binary sensor. the timer and latch approach is also a good alternative if the simpler template sensor approach i had offered did not work for you for some reason.

i never had problems with my state machine - the real reason i stopped using the motion sensors for automation (using any ping approach) is because sometimes the ping sensor just wouldn’t activate in a timely fashion to ping the sensors. thats a problem in HA itself and how it decides to schedule updates to the ping sensor.

for all folks here following the solution i had proposed: with 0.113’s automation modes feature, we can make this a lot more simpler. i have discussed the newer approach in this thread

update: with the latest HA versions (this solution was created way back in 0.103 or something), the ping binary sensor updates its ‘last_changed’ even when the sensor is still ‘off’ - e.g. when HA restarts or at random arbitrary times. It would have immensely helped if HA had a ‘last_on’ state for a binary sensor but the workaround i’ve figured out for getting the correct ‘last_on’ state (as compared to ‘last_changed’ - which now changes even for an ‘off’ state - even when it hasn’t necessarily transitioned from on to off but just off to off, for some reason) - is to use an SQL sensor:

sensor:
  - platform: sql
    db_url: !secret recorder_db_url
    queries:
      - name: Motion Sensor Last On
        query: "SELECT last_changed FROM states WHERE entity_id='binary_sensor.ping_tuya_motion_sensor' AND state='on' ORDER BY last_changed DESC LIMIT 1;"
        column: 'last_changed'

now use it in your template sensor:

- platform: template
     sensors:
       motion_sensor_state:
         friendly_name: "Motion Sensor State"
         value_template: >-
           {{ as_timestamp(states.sensor.date_time.last_changed) - as_timestamp(states('sensor.motion_sensor_last_on')) < states('input_number.motion_sensor_idle_seconds')|int }}

This worked for me, had to keep it simple and created an automation based on the binary sensor on it’s own.

For a simple turn light on when entering a room (Smart Life PIR with LIFX Light), I did the following:

configuration.yaml

binary_sensor:
  - platform: ping
    host: 192.168.0.8
    count: 1
    scan_interval: 1
    name: bathroom_pir

image

Automation File

- alias: "Bathroom Light PIR"
  trigger:
  - entity_id: binary_sensor.bathroom_pir
    platform: state
    to: 'on'
  condition:
    condition: time
    after: '16:00'
    before: '09:00'
  action:
  - device_id: 5539117627df4c67b856a08343c2e937
    domain: light
    entity_id: light.bathroom_light
    type: turn_on

Doing exactly what I wanted it to. Thank you. I had first tried setting it up in IFTTT, but that’s so unreliable, it kept triggering it every 1 minutes even when no events we logged in the Smart Life app… weird.

2 Likes

Hey All,
I need some help:

I insert Thiscode in my configuration.yaml

binary_sensor:
  - platform: ping
    host: 192.168.178.21
    count: 1
    scan_interval: 1
    name: ping_tuya_motion_sensor_master_bedroom
  - platform: template
    sensors:
      tuya_motion_sensor_master_bedroom:
        friendly_name: "Master Bedroom Motion Sensor Alarm"
        value_template: >-
          {{ as_timestamp(states.sensor.date_time.last_changed) - as_timestamp(states.binary_sensor.ping_tuya_motion_sensor_master_bedroom.last_changed) < states('input_number.motion_sensor_idle_secs')|int }}

I can see that ping_tuya_motion_sensor_master_bedroom is responding but the Master Bedroom Motion Sensor Alarm isn’t. I have no device with a sensor, only an entity.

Please help this newbee.

tgw2000.

This worked for me except that it takes about 6 to 7 seconds before the lamp it turned on.
Can this be accelerated for example. If motion is detected the lamp switches on in about 2 seconds.

Thankyou all. this works for me:
I got a WIFI binary PIR sensor in host: 192.168.xxx.xxx at home.
but
How could I see a PIR in a country house? normally is not reacheable from home.
How sould I config the host as I see them in smatlife app ?

Thakyou !!

Hi. I followed this thread to make my PIR turn on a switch to activate lights in my celler. My problem now is how to set it up to turn of after 5 min? I am totaly new to this world, but want to learn. My config is like this

- alias: "Kjellarlys - "
  trigger:
  - entity_id: binary_sensor.kjellar_pir
    platform: state
    to: 'on'
  condition:
    condition: time
    after: '00:00'
    before: '23:59'
  action:
  - service: switch.turn_on
    data:
      entity_id: switch.bfa5cb2c8f5aa6eb74dqmq
  mode: single

  • delay: 00:05:00

man you are a BEAST!!

Hi, I’m trying to get this setup so that when motion is detected, Govee LED slight strip turns on. I have a ENER-J motion sensor that is setup with smart life and I have made an automation within the app to send a message when triggered. As mentioned above, this has showed up in my scenes. I have also set up the ping with the IP address of the motion sensor (idk if this is right) as shown by @angadsingh . But it is showing the opposite - connected all the time! so not sure what I am doing wrong. Can anyone help?

binary_sensor:
  - platform: ping
    host: 78.230.4.133
    count: 1
    scan_interval: 1
    name: pir_sensor
  - platform: template
    sensors:
      pir_sensor:
        friendly_name: "Master Bedroom Motion Sensor Alarm"
        value_template: >-
          {{ as_timestamp(states.sensor.date_time.last_changed) - as_timestamp(states.binary_sensor.pir_sensor.last_changed) < states('input_number.motion_sensor_idle_secs')|int }}

Screenshot 2020-12-19 at 7.06.36 PM

Hi, I have found another approach which is less speedy but works for me because of simplicity.
I’m using the presence detection. In other words, has that sensor connected to my router.
Is this sufficient for you?

  • This will detect when “something happens after a while”. Meaning if you open the door, it will flag. If the door stays open and then closes, it will flag. The “delay” between each flag is around 3minutes
  • This is because the sensor connect to the router, then stays idle for about 3 minutes and disconnects. That’s what I am tracking.

Prep and gather info

  • In the Smart Life app, I went to device information (click on device, then “pen” on top right, then device info) - and found the Mac adress. Note that
  • I went to my router to check devices connected. Then opened my door, and watched. I found which one popped up and validated the Mac adress. I noted the IP adress
  • At this point, I “reserved” this ip in my router for this device. It will always stay the same.

Code in configuraiton.yaml

  • The IP I’m tracking in this example is 192.168.86.79 (the second one).
  • I did this this way to have a slower and more tolerant scan for phone presence (hosts: 192.168.86.55-60) and a very tight one for the specific door sensor
  • I’m using the NMAP integration (nothing needed to install) which scans the network for connected IP adresses
device_tracker:
  - platform: nmap_tracker
    hosts: 192.168.86.55-60
    home_interval: 10
  - platform: nmap_tracker
    hosts: 192.168.86.79
    interval_seconds: 1
    consider_home: 1

Code in knowndevices.yaml

  • This is where you will name your entity and link it with the Mac adress you found earlier on
tuya_door_gardenroom:
  icon:
  mac: 48:3f:da:10:f0:80
  name: Door Gardenroom
  picture:
  track: true

There you go

Notes and tests I did

  • The entity created here was device_tracker.tuya_door_gardenroom
  • The entity will have 2 status possible: home and not_home
  • 5 seconds delay on my system between the door opening and status “home”
  • 3m15sec later, the status became “not_home” - regardless if the door stayed open or not.
  • Remember, this is a “something happened” type of sensor - it doesn’t track status
  • It works fine for my need which is to send a small warning on my home assistant when this remote door of my house is opened

Another piece of code…

  • Set manually (override) home or not home (you might need that if you combine automation with lights, or another sensor or whatever. It just seemed to be not much known:
  • You can call a service to set the status manually - note that you don’t call the entity name as usual but the device_id as chosen above:
service: device_tracker.see
data:
  dev_id: tuya_door_gardenroom
  location_name: not_home
1 Like

Thank you so much! You’re a legend!

Hi guys, I was thinking I’d share my Tuya motion sensor workaround. It’s very simple but works great for me. I have a Tomato router (https://www.home-assistant.io/integrations/tomato/). After connecting this integration I get presence status of my LAN devices based on their MAC addresses. I simply found the entity with the MAC address of my motion sensor and created a presence based light automation (home and not_home) for the motion sensor. I tried the above ping based solutions but my console log got flooded with many messages. This one seems a bit cleaner and super easy to use. If you have a supported router you might want to try. :slight_smile:

1 Like

Hey guys,

I also have another work around. Works with Tuya Sensors, Node Red and Alexa Routines, so does rely on the cloud.

First, you will need to link your Alexa to the Tuya / Smart Life skill in the Alexa app.
Then discover devices, so that your Tuya sensor is in the Alexa app.

In Node Red, I downloaded the alexa-local node. https://flows.nodered.org/node/node-red-contrib-alexa-local

I made two of these nodes, and named one SensorOpen and one SensorClosed.
Then I asked Alexa to discover devices, so she found SensorOpen and SensorClosed.

Then I made two routines in the Alexa app;
One to say when my Tuya sensor opens, then turn on SensorOpen.
One to say when my Tuya sensor closes, then turn on SensorClosed.

I played around in Node Red to hook up what lights I wanted to turn on and off, depending on if the sensor was opened or closed, linking the lights to the appropriately named node in node red.

It means that you don’t need a secondary plug in Tuya, because you can set up as many Alexa local nodes with whatever names you want, to match as many Tuya sensors as you have.

Hope this helps someone out!

1 Like

I think you retrieved this information in the Smart Home App. The app just shows your public IP.
Look for the device connected to your WLAN router with the same MAC address as shown in the App for your internal IP.

On the node-red-contrib-alexa-local github repo it says that is not longer working.
Have not tried it yet, but maybe this can be an alternative.

I have installed and am using node-red-contrib-alexa-local without any problems, so it is worth a shot.

I also have node-red-contrib-alexa-home-skill - which requires some more setup, as you have to make new devices at https://alexa-node-red.bm.hardill.me.uk/ which is another account to make at https://alexa-node-red.bm.hardill.me.uk/docs, but I find my alexas find the devices from the contrib-alexa-home-skill a lot quicker than the alexa-local skill.

Did anyone have any luck with the non wifi tuya sensors? Bluetooth or Zigbee?

Thanks Matt!!

I’ve adapted your approach to do the same without the need for the node-red component.

I created an input number for the sensor (0,1)
I created a binary sensor that evaluates the input number
I created two scenes in HA. One to turn the input number to 0 and the other to 1.
I created two routines in Alexa. One to call the 0 scene when the Tuya sensor is open (door sensor example) and one routine to call the 1 scene when the Tuya sensor is closed (door sensor example)

Works a treat.

Now I can use the >90% of cheap tuya sensors on the Australian market. Awesome. Thank you!

2 Likes