custom-components / ble_monitor

BLE monitor for passive BLE sensors
https://community.home-assistant.io/t/passive-ble-monitor-integration/
MIT License
1.9k stars 247 forks source link

Add support for mjyd02yl-a nightlight #180

Closed skynetua closed 3 years ago

skynetua commented 3 years ago

I would like to request support for mjyd02yl-a nightlight. It provides motion, illuminance data, advertisements are encrypted
logs.txt

Magalex2x14 commented 3 years ago

Hello. Since the data is encrypted, I need an encryption key to look at it. Or we can try it "blindly" - tell me which version of the component you are using, I will make changes to it, and we will see how it works.

skynetua commented 3 years ago

I'm using the latest one 0.8.4 I can give you a key if it's more preferred

Magalex2x14 commented 3 years ago

Let's try first. I think we have all the required entity types. Later I will give you a link to the modified code.

Magalex2x14 commented 3 years ago

I was mistaken in saying that there are all types of entities for this sensor... As far as I understand, there will be time since the last movement and illuminance. The contents of the const.py file should be replaced with the following content: const.py

Leave the report_unknown option enabled. Do not forget to register the encryption key in the configuration. Play around with the sensor and drop the contents of the log here (it will already be decrypted). Pay attention to the entities, created for this sensor (battery, illuminance, light) - see if they change and how.

skynetua commented 3 years ago

Three sensors are created but their state doesn't get any updates. It is unknown all the time. Adv data is not shown in logs anymore as device is not unknown now, I suppose. logs.txt

Magalex2x14 commented 3 years ago

This is strange, since the component writes to the log in addition to unknown sensors, also unknown data from known sensors. Okay, I suggest not to figure out what's the matter now. Just give me the key, and I myself will decipher the data you sent earlier. You can throw the key here and change it, or you can send it to the mail (magalex28@gmail.com).

skynetua commented 3 years ago

I sent the key and new logs to your email.

Magalex2x14 commented 3 years ago

@skynetua Thanks, received.

In addition, thanks to you, I found a serious data decryption bug in ExtendedAdvertisement packets (you probably have an Intel NUC or another platform with a modern bt-adapter).

Please wait a little while I rollover updates for all active branches...

Magalex2x14 commented 3 years ago

@skynetua I created a separate branch to support your sensor - mjyd02yl-a Replace the contents of the custom_components/ble_monitor folder with the files from this branch.

Leave the report_unknown option enabled, as I said earlier. The component will create three entities - illumination, light, motion (light and motion are binary sensors). Check if the illumination values ​​are different from 1 and 100. The strange thing is that in your log I did not see any events of no motion, that is, it seems that the sensor reports the fact of an event, and not the state of the motion sensor. Maybe I'm wrong, periodically check the log for unknown data messages.

Let me know about the results when you collect some statistics.

skynetua commented 3 years ago

It works weird. I can't understand how it updates. Motion is always unknown Light looks like work ok Illuminance is 1 or 100 Buttery value jumping around 2%, probably measurement accuracy issue

I don't see any unknown adv in logs.

It would be probably better to have option to configure update period per particular device to make it more responsive and still have less spam from other sensors.

Magalex2x14 commented 3 years ago

Yes, the sensor reports strangely. I looked at the related issue in the ESPHome repository, and still did not fully understand what it is broadcasting in which situation. From what I've seen, it should send the time elapsed since the last movement event. Maybe the behavior depends on the device settings in MiHome? It's hard to implement support without having a device... I think I'll just implement support similar to ESPHome, and we'll see how it goes.

Binary sensors (from 0.8.4) updated immediately regardless of the period option. The mjyd02yl branch is based on 0.8.5. The period option now only affects measuring sensors that send data very often (many times per minute).

skynetua commented 3 years ago

Actually the is not much to control in app. SmartSelect_20201208-132821_Mi Home

skynetua commented 3 years ago

I'm not sure, but looks like it reports the following events:

  1. Motion detected = true
  2. Ambient light compared to device setting - basically light(100) or dark(1)
  3. State of the light itself, is it turned on or not
  4. Battery

2 and 3 are probably sent only when first one triggered. Also looks like there is really no events for motion = false.

Magalex2x14 commented 3 years ago

If there really is only a motion event alone, then I still have no idea how to properly implement this in HA. This can be used as a trigger in automation, for example, but as a full-fledged binary sensor it is not suitable ... We need some kind of timeout to reset, or reset upon the arrival of the next packet from the sensor (but this can be a rather small time interval, less than 1 second) ...

The light binary sensor is actually fictitious - its state is calculated from the illumination ( "light": 1 if illum == 100 else 0) A motion event is the fact of receiving a message about illumination (in fact, there is a separate type of data object for "illumination", here its own type denoting "motion+illumination").

UPD. found a typo in the motion sensor code - corrected it, and added a motion sensor reset after two seconds. Update the component from the branch as you did before. Let's see how it goes ...

Magalex2x14 commented 3 years ago

Have you checked for firmware updates for this sensor?

skynetua commented 3 years ago

I updated component and sensor firmware. It's getting better. The one issue for now is that updates are strange. They can be not instant or absent at all. I'll setup esphome and try to compare.

andrewjswan commented 3 years ago

Is there a newer version based on 0.9.3?

andrewjswan commented 3 years ago

Screenshot_2020-12-17-14-45-43-232_com xiaomi smarthome Screenshot_2020-12-17-14-46-00-193_io homeassistant companion android

andrewjswan commented 3 years ago

It seems to transmit its state only during the movement, or rather even in the moment of movement, and that's all, it does not transmit any more. But then it is not clear how automations work, is it possible that on the server side they are somehow counted...

andrewjswan commented 3 years ago

Screenshot_2020-12-17-14-56-18-437_com xiaomi smarthome

andrewjswan commented 3 years ago

The status that there was movement I did not reset. :(

Magalex2x14 commented 3 years ago

Unfortunately, there is no code based on the current release yet. I kind of added a timeout there to reset the motion sensor (this is necessary since the sensor reports an event and not a state). Give me more time - I will definitely return to this issue a little later (most likely next year, on holidays).

andrewjswan commented 3 years ago

I'll setup esphome and try to compare.

Is there any news on how the ESPHome nightlight works?

andrewjswan commented 3 years ago

Unfortunately, there is no code based on the current release yet.

I collected all the changes and made them in 0.9.3. But the motion sensor is not reset for some reason. And the status of whether the light is on and its brightness, comes very strange, I did not understand the pattern. Sometimes when motion is triggered, sometimes just like that. Sometimes it does not change at all.

skynetua commented 3 years ago

Is there any news on how the ESPHome nightlight works?

It works a bit better but with some delay. I'd expect more frequent updates. Unfortunately I don't have any xiaomi hubs to check if sensor works with them more accurate.

I don't understand the way it updates either. 😕

andrewjswan commented 3 years ago

Unfortunately I don't have any xiaomi hubs to check if sensor works with them more accurate.

If I knew what to check, I could. But everything that is available with the hub is shown in the screenshots above.

andrewjswan commented 3 years ago

I add some notifications for sensor and get: Screenshot_2020-12-22-08-50-22-127_com google android gm

andrewjswan commented 3 years ago

It looks like when the motion sensor is triggered, the status of all sensors is transmitted, but the lighting status is transmitted with a slight delay, as in the door opening sensor, and is transmitted only when the value is changed. Movement is transmitted with every detection.

andrewjswan commented 3 years ago

Automation seems to work comprehensively, if you select only motion, it seems to come always, but if you select only light, if there is motion when you turn on the light, it does not come. Probably that's why they made the automation light/dark - there is movement. I will check more today.

andrewjswan commented 3 years ago

I kind of added a timeout there to reset the motion sensor (this is necessary since the sensor reports an event and not a state).

I think 2 seconds is not enough, it is better to increase this timeout to 5 or more. Or add it to the settings altogether.

skynetua commented 3 years ago

@andrewjswan do you see any delay between message and actual movement?

I would like to have timeout as customizable option as well.

andrewjswan commented 3 years ago

do you see any delay between message and actual movement?

I haven't really looked at it yet, I just turned on the notifications this morning for a test, but the messages came almost instantly on the move. But here we need to make allowance for the fact that if I have a delay in notification, it may be due to the chain - sensor - gate - cloud MiHome - push notification - phone. In HA I have the motion sensor is always on, I have not seen it in any other state.

andrewjswan commented 3 years ago

@skynetua If there is movement, then the statuses come to MiHome instantly, both the movement itself and the light. It also receives a notification that there has been no movement for a N minutes. If after the first notification to move in front of the sensor, the notification about the movement comes with a period of about 30-60 seconds. But if you just turn on the light without motion, and there is no movement, there is a delay and quite large (3-5 minutes). The same is after switching off the light...

andrewjswan commented 3 years ago

If you are constantly moving within range of the sensor, the notification from MiHome comes at intervals of 15 to 60 seconds.

andrewjswan commented 3 years ago

Any news :)

andrewjswan commented 3 years ago

AlexxIT added different variants of motion sensor behavior to his integration, maybe he has the right ideas and they can be added here too. You can also probably look at his implementation of motion sensors.

https://github.com/AlexxIT/XiaomiGateway3#advanced-config

Ernst79 commented 3 years ago

@Magalex2x14 is/was on holiday, so no news yet. We will look into it, but it will take slightly longer.

Ernst79 commented 3 years ago

Guys, I propose I copy @Magalex2x14 into a new branch, based on the latest version.

Can you tell me what needs to be changed/added more?

skynetua commented 3 years ago

Please add sensor state reset to light entity as well and make it configurable on device level. 2 seconds is too few. Reset countdown each time the new motion event adv received.

Ernst79 commented 3 years ago

What do you mean by "sensor state reset to light entity". Do you want a device attribute reset to light?

skynetua commented 3 years ago

No, I want to reset BinarySensor value to false after configurable amount of time. See 4a717d145c07284e3836813a5b82bf350ff40c45 It's dome for Motion sensor only and hardcoded to 2 sec. Please add the same to class LightBinarySensor(SwitchingSensor) and make this reset timeout configurable per device. If 0 - do not reset.

Thanks)

Ernst79 commented 3 years ago

Thanks, clear. I've uploaded the current state (@Magalex2x14 his changes), merged to the MJYD02YL_v2 branch. This branch is based on 0.9.7-beta. Only changed the time to 5 seconds for now. I will look into the requested changes.

Ernst79 commented 3 years ago

Something like this?

image

I've uploaded the changed code to the MJYD02YL_v2 branch, could you give it a try? I can't test it myself, as I don't have this device. If you don't specify it, it defaults to 0, which is no reset.

If you use YAML, you should be able to set it with

ble_monitor:
  devices:
    - mac: 'A4:C1:38:2F:86:6C'
      name: 'Motion sensor'
      encryption_key: '2171238CF5D22808DA20181502D84C1B'
      reset_timer: 20

It should work for both the light sensor and the motion detector

andrewjswan commented 3 years ago

On 0.9.7-beta my T&H2 sensors not update his values at all :(

Ernst79 commented 3 years ago

Which T&H sensors do you have? Are there any errors in the log? And did you use the 0.9.7-beta, or the modified MJYD02YL branch?

andrewjswan commented 3 years ago

@Ernst79 I have LYWSD03MMC sensors with stock firmware :) It was very late, I did not look at the logs, I noticed that the sensors are not updated 6 hours on the graphs, I installed 0.9.6, rebooted HA, saw that the updates went and went to bed. Now i use 0.9.6 due sensor update problem ... And I did not check the modified version for the same problem.

andrewjswan commented 3 years ago

All my settings in YAML, global restore state in True ...

Ernst79 commented 3 years ago

I have LYWSD03MMC (one with stock firmware) as well, and it works fine with 0.9.7-beta on my end. I'm using UI mode. Restore State works fine. I'll check YAML mode.

andrewjswan commented 3 years ago

Very strange ...

Ernst79 commented 3 years ago

I've used YAML mode, still works fine. Note that stock firmware is extremely slow for LYWSD03MMC sensors (10 minute interval for T and H, 1 hour for Battery). Perhaps you have reception issues?