There have been various workarounds to fix the 'stuck' motion sensor, however none genuinely create a functioning motion sensor. Well not in my case anyway. Whilst Lenovo / Tuya WiFi motion detectors - Fix always detected issue would cause deactivation after a period of time, it would not necessarily trigger again if there was ongoing movement and would result in apparently spurious triggering.
The root cause, for my sensor at least was not that there was no deactivation event, but that the same event signature was being used for activation, deactivation and for battery status updates.
Using the above workaround with a short reset period of 10 seconds it was possible to create a virtual motion sensor in templates.yaml that activates when motion is detected and clears when there is no motion:
### Virtual PIR Sensor ###
- trigger:
- platform: state
entity_id:
- binary_sensor.motion_sensor_1_motion
from:
- 'off'
to:
- 'on'
binary_sensor:
- name: Virtual Motion Sensor 1
device_class: motion
icon: mdi:motion-sensor
state: >
{{ (states('binary_sensor.virtual_motion_sensor_1')|bool(false)==false and state_attr('binary_sensor.virtual_motion_sensor_1','battery')|float == states('sensor.motion_sensor_1_battery')|float) or (states('binary_sensor.virtual_motion_sensor_1')|bool(false)==true and state_attr('binary_sensor.virtual_motion_sensor_1','battery')|float != states('sensor.motion_sensor_1_battery')|float) }}
attributes:
battery: >
{{ states('sensor.motion_sensor_1_battery') }}
The final bit of the puzzle was to stop the state of the virtual sensor inverting when there was a battery status update. Hence the strange logic using the stored battery state.
Just as with the physical motion sensor the virtual sensor should be set to 'off' on home assistant restart. Add the below action to the 'When Home Assistant is started' automation:
data:
entity_id: binary_sensor.virtual_motion_sensor_1
state: "off"
action: python_script.set_state
So far, replacing the battery has not caused the virtual sensor state to go out of sync.