Tuya motion sensor is not supported in Home Assistant

Hi @Dennis_Vogt. Can you share some more details on what automation can be made? I only have a single tuya sensor, and I cannot see option for any automation besides a notification. The notification shows up as a scene in HA, but I cannot see in the STATES of the Developer Tools how HA acknowledges that a notification was sent. Task as an option does not work either on its own, I think a second tuya device is needed?

tl:dr; This only works when you have an OpenWrt router.

Knowing that the motion sensors connect when they detect motion we can look in the OpenWRT log (logread) for dhcp ack messages, they look something like:

Wed Jun 17 15:13:13 2020 daemon.info dnsmasq-dhcp[1541]: DHCPACK(br-lan) 192.168.1.246 70:91:14:cb:11:db ESP_AA55DD

I’m running the following script on my rpi sshing to my router at 192.168.1.1 with a key

#!/bin/bash

while true; do
  logger "Follow the log output filtered on DHCPACK and print timestamps"
  ssh -i /home/pi/.ssh/id_rsa -o ServerAliveInterval=60 [email protected] logread -t -e DHCPACK -f | \
  while read CMD; do
    line=$(echo $CMD | cut -d' ' -f6,12)
    stamp=$(echo $line | cut -d' ' -f1)
    dev=$(echo $line | cut -d' ' -f2)
    #lowercase
    dev=${dev,,}
    if [[ $dev == *"ESP"* ]]; then
      mosquitto_pub -h localhost -t "home/motion/$dev" -m "$stamp"
      logger "home/motion/$dev $stamp"
    fi
  done
done

When a log statement is found with DHCPACK and a tuya device (ESP_XXXX) send a mqtt message on home/motion/esp_xxxx.

In HA I have the general automation:

- id: '159544539864'
  alias: Motion dispatch
  description: ''
  trigger:
  - platform: mqtt
    topic: home/motion/+
  condition: []
  action:
  - data_template:
      entity_id: input_boolean.motion_{{ trigger.topic.split('/')[-1] }}
    service: input_boolean.toggle

Then under helpers I just create some boolean inputs that represent the pir sensors I want(it triggers for all tuya devices, so only create pir ones here, but it keeps the rest of the code nice and short)
input_boolean.motion_esp_1812 and I use those to trigger my automations:

- id: '15945789596'
  alias: Turn light on for a minute
  description: ''
  trigger:
  - entity_id: input_boolean.motion_esp_1812
    platform: state
  condition:
  - after: sunset
    condition: sun
  action:
  - entity_id: light.hallway_light
    service: light.turn_on
  - delay: '60'
  - entity_id: light.hallway_light
    service: light.turn_off

Also using the LSC Smart Connect action devices here.

The presence luci device tracker didn’t work for me reliably (polling interval too big I guess).
This seems to work reliably and instant(no cloud overhead) so far…

Hi,

What’'s your plan about the simpler ping sensor which can just do a simple continuous ping and report back state that way (instead of waking up on intervals and doing pings)?

Because this seems to work for me, but it’s really enoying HA misses the sensor misses sometimes.
It would be nice if the new sensor is coming soon :slight_smile:

Excellent work. I’m attempting to use these (inexpensive) PIR sensors to detect animals in my yard at night. I live in a neighborhood that deer seem to frequently visit (at wee hours of the morning). I’m in hopes to turn on that zone’s sprinkler to scare them away.

Ciao Come fate a vedere l’ip in quanto io tramite app smartlife vedo alla voce ip dispositivo il mio ip esterno

I’m trying to use this for some toilet occupancy sensors (thanks Covid) in our bar. the sensors work great and the ping sensor is accurate, however I can only get one of the sensor templates to work reliably ,the other one seems to just flick between on/off constantly. I wonder if I have to define the two sensors differently?

sensor:
  - platform: time_date
    display_options:
      - 'date_time'

input_number:
  motion_sensor_idle_secs:
    name: Motion Sensor Idle Seconds
    initial: 130
    min: 10
    max: 3600
    step: 1

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

  - platform: ping
    host: 192.168.2.61
    count: 1
    scan_interval: 1
    name: ping_tuya_motion_sensor_ladies_toilet
  - platform: template
    sensors:
      tuya_motion_sensor_ladies_toilet:
        friendly_name: "Ladies Toilet Motion Sensor Alarm"
        value_template: >-
          {{ as_timestamp(states.sensor.date_time.last_changed) - as_timestamp(states.binary_sensor.ping_tuya_motion_sensor_ladies_toilet.last_changed) < states('input_number.motion_sensor_idle_secs')|int }}

so I never managed to get two of these sensors working, but I did manage to come up with another way of interacting with the basic ping sensor - and it seems to work! it’s not as pretty as @angadsingh 's original solution, but it’s been reliable for a few days now, and has let me add counters etc.

I read a great post about “debouncing” a binary sensor, which meant the 5-8 ping responses I get from the Tuya motion sensor are evened out. here’s the original article:

https://community.home-assistant.io/t/debouncing-binary-sensory/111833/5

from this I created the following sensors:

timer:
  gents_ping_motion_latch:
    duration: '00:01:00'

input_boolean:
  gents_ping_motion_latch:
    initial: off

then I needed a whole bunch of automation - but it worked! the main automation times an RGB bulb moving between Red Amber Green depending on when someone last visited. I needed a couple of other automations as well to reset the timer for the debouncer, but it works!

- id: '1595074710698'
  alias: Gents timer automation
  description: ''
  trigger:
  - entity_id: binary_sensor.ping_tuya_motion_sensor_gents_toilet
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: input_boolean.gents_ping_motion_latch
    state: 'off'
  action:
  - data: {}
    entity_id: input_boolean.gents_ping_motion_latch
    service: input_boolean.turn_on
  - data: {}
    entity_id: input_number.gents_isbusy_counter
    service: input_number.increment
  - data: {}
    entity_id: timer.gents_ping_motion_latch
    service: timer.start
  - scene: scene.gents_red
  - delay: 00:01:00
  - scene: scene.gents_amber
  - delay: 00:01:00
  - scene: scene.gents_green
- id: '1595077977474'
  alias: Gents Motion latch timer finished
  description: ''
  trigger:
  - event_data:
      trigger:
      - event_data:
          entity_id: timer.gents_ping_motion_latch
        event_type: timer.finished
        platform: event
    event_type: timer.finished
    platform: event
  condition: []
  action:
  - data: {}
    entity_id: input_boolean.gents_ping_motion_latch
    service: input_boolean.turn_off

And that’s it - I couldn’t have got it working without the ping binary sensor, but for me, once I had 2 sensors defined, the second one always stayed on. with this automation I managed to set the two things up.

Just thought I’d post this in case anyone else got stuck, I’m still new to HomeAssistant, but I thought that maybe someone could benefit from my week of troubleshooting.

2 Likes

@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.

3 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!