h4 / LYWSD02-home-assistant

Home-Assistant sensor for LYWSD02 Thermometer
75 stars 12 forks source link

Add services to change units and time #3

Open h4 opened 5 years ago

wimb0 commented 5 years ago

Hi,

I was looking into this also, and found an example of setting time on this device: I found this on the Gadget Bridge application github. (Line87 in https://github.com/Freeyourgadget/Gadgetbridge/blob/461842a945d43f68d2eea0d99d1f70c8a6fd666e/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/mijia_lywsd02/MijiaLywsd02Support.java)

My Python isnt that good, and my Java is even worse, but this is what looks like the UUID and bytes needed to set the time:

    private void setTime(TransactionBuilder builder) {
        BluetoothGattCharacteristic timeCharacteristc = getCharacteristic(UUID.fromString("ebe0ccb7-7a0a-4b0c-8a1a-6ff2997da3a6"));
        long ts = System.currentTimeMillis();
        byte offsetHours = (byte) (SimpleTimeZone.getDefault().getOffset(ts) / (1000 * 60 * 60));
        ts /= 1000;
        builder.write(timeCharacteristc, new byte[]{
                (byte) (ts & 0xff),
                (byte) ((ts >> 8) & 0xff),
                (byte) ((ts >> 16) & 0xff),
                (byte) ((ts >> 24) & 0xff),
                offsetHours});
    }

Maybe this is helpful for you also.

jlongman commented 4 years ago

Hey, so I'm actually using HACS with HomeAssistant to read the eink miija device and when I get it it was 12 hours off so livable, but when the time changed it was now 12 + 1 and I had to fix it. I found this page and then did this:

gatttool -b AA:BB:CC:DD:EE:FF:GG  -I
Exchange MTU for GATT/ATT
[AA:BB:CC:DD:EE:FF:GG][LE]> connect
Attempting to connect to AA:BB:CC:DD:EE:FF:GG
Connection successful
[AA:BB:CC:DD:EE:FF:GG][LE]> char-read-uuid ebe0ccb7-7a0a-4b0c-8a1a-6ff2997da3a6
handle: 0x002f   value: cd ff a1 5f 08
[AA:BB:CC:DD:EE:FF:GG][LE]> char-write-req 0x002f 6f05a25ffb
Characteristic value was written successfully
[AA:BB:CC:DD:EE:FF:GG][LE]>

In order to get the time value 6f05a25ffb I used the above code in an online groovy console, like so:

ts=System.currentTimeMillis();
byte offsetHours = (byte) (SimpleTimeZone.getDefault().getOffset(ts) / (1000 * 60 * 60)); // oops, UTC, server?
ts /= 1000l;
ts= (long)ts;
offsetHours = -5; // FORCE it to eastern
​def printable = [             
  (byte) (ts & 0xff),          
  (byte) ((ts >> 8) & 0xff),         
  (byte) ((ts >> 16) & 0xff),           
  (byte) ((ts >> 24) & 0xff),              
  offsetHours
]​ as byte[];

printable.encodeHex();

And then I cut and pasted the output value at the end of char-write-req 0x002f,

​So anyways it looks like the gatttool can do it so if you can emit those commands you can too.

Olsenius commented 4 years ago

I had trouble connecting the mi home app to some of my clocks. I wrote a simple node app that connects to every lywsd02 in range and sets the time. Only tested it on my 4 clocks and in my time zone. https://github.com/Olsenius/lywsd02

saso5 commented 3 years ago

I've also created a one-time script for clock-sync that you can run from the browser. https://saso5.github.io/LYWSD02-clock-sync/

I've also wanted to add an option to change the unit (F/C) but it doesn't update the clock/sensor. Did anybody try this? The value does get set but the interface doesn't update.

vitkabele commented 2 years ago

Hello, what is the state of this feature? Seems that there is official "Xiaomi BLE" integration now. Could it be integrated into it?