dkerr64 / homebridge-yolink

Apache License 2.0
13 stars 2 forks source link

WaterMeterController Support (YS5008-UC and YS5006-UC) #98

Closed ssnthedj closed 3 months ago

ssnthedj commented 4 months ago

Please add support for Water Meter controller YS5008-UC

https://shop.yosmart.com/pages/ys5008-support

This is a brand new product from Yolink so I understand why it is not supported yet but it would be awesome if it could be added. I think this product will be quite popular at it's price point. Way cheaper than Moen Flo or Phyn Plus.

dkerr64 commented 4 months ago

Do you have one to test with? I would need to see the log messages as the plugin discovered the device and what messages are received.

The plugin already supports the original YoLink water valve (I have a YS4909-UC) and if this new one uses the same basic commands then I would expect open/close to work. But the additional features that measure the flow will not, and I don't think HomeKit would support them anyway.

ssnthedj commented 4 months ago

Yes I do have one, I actually got this error message when I added it to the Yolink app and then looked in my homebridge instance and saw the errors about it not being supported. I've attached the relevant section of the log files with the error.

homebridge.log.txt

dkerr64 commented 4 months ago

Thanks for the log. It is reporting itself as a different device so current plugin will not support. I should be able to add support but I may need you to do a couple or rounds of testing with me. I'll let you know when I have something for you to try.

Thanks.

ssnthedj commented 4 months ago

Gotcha, yes this is a completely new product from Yolink combining their water meter and smart valve into one device. I think it will be a popular product from them given it's competitive price point. It's half the cost of others like Moen flow. I'm happy to do any testing on my end just let me know once you need. I'm getting the device installed by a plumber next week. I appreciate you taking the time to develop this plugin!

dkerr64 commented 4 months ago

I just published a beta version to NPM. Go to the plugin in Homebridge and select "manage version" from the dot-dot-dot menu. It is v1.5.8-beta.1

I believe it should correctly report status of the valve. I am not sure whether it will be able to set the valve (open or close it) as I am guessing that the data to send is the same as what I send for the older style valve. But that may be a bad assumption... as the format of the status data received from this valve is different (per your log file). Unfortunately YoLink have not published the API for this device at their developer API document page yet. If it doesn't work, then I will have to reach out to them for documentation.

New feature in this device is that it appears to report whether water is flowing or not. HomeKit supports this, so I added support for that. Again, I don't have one to test (and you won't be able to test that before you have it installed!.

Please turn on verbose logging and report back with what it shows. I'm particularly interested in seeing what is logged when you control the device from your phone YoLink app... as that will cause messages to be sent to the plugin reporting status.

Thanks.

ssnthedj commented 4 months ago

I installed v1.5.8-beta.1 and the water meter now shows up in HomeKit and does not give a "device not supported" error in homebridge. However, when I try to control the valve it throws an API error. I am attaching the logs for your review. Also, in HomeKit it keeps showing "starting" on the device but that does not change.

homebridge-yolink.log.txt IMG_9348 IMG_9350 IMG_9349

dkerr64 commented 4 months ago

I'll need to look and see if I can find some documentation on what this device expects. Also, did you enable verbose logging in the plugin settings? If not please do that and try restarting again.

The 00201 errors are, unfortunately, fairly common (see my comments on that in the technical notes section of README. So not clear whether they are relevant.

Thanks.

ssnthedj commented 4 months ago

I had enabled verbose logging for homebridge itself but did not realize I needed to do separately for plugin. See snippet below of verbose output when trying to close/open the valve.

Screenshot 2024-07-18 at 12 20 04 AM
dkerr64 commented 4 months ago

The log looks odd because it is missing data I would expect to see after the SENDING: and RECEIVING: statements (will be JSON strings). What version of Homebridge UI are you using? It may be clipping that data when you only view logs for the YoLink plugin. They are showing fine for me with the UI v4.56.4.

I need to see the full YoLink log to check that the device is properly registered... assuming the log is properly reporting the JSON data. Can you download and attach that please. You should redact any appearance of your userAccessId and secretKey (they may appear twice, once when the config file is logged, and again when they are used. Other identifiers are okay to leave in as they have temporary life... but if you want to be super cautious you can redact the _accesstoken... it has a life of a couple of hours.

Also, next time you test, please do not try and control the valve from HomeKit / Apple Home. Instead use the OEM YoLink app to control the valve... that should trigger messages on the log which I need to see. I am looking for something like this.

[7/18/2024, 8:36:15 AM] [YoLink] [verbose] MQTT message: yl-home/d3d51dfc169c4f7aa02b1bf5901a92db/d88b4c01000369a3/report
{
  "event": "Manipulator.setState",
  "time": 1721306175062,
  "msgid": "1721306175062",
  "data": {
    "state": "open",
    "loraInfo": {
      "netId": "010205",
      "signal": -71,
      "gatewayId": "d88b4c1603008c02",
      "gateways": 1
    }
  },
  "deviceId": "d88b4c01000369a3"
}

But yours will say WaterMeterController instead of Manipulator.

dkerr64 commented 4 months ago

I am also looking for something like this during plugin initialization...

[7/18/2024, 3:59:38 PM] [YoLink] [verbose] [YS4909-UC (d88b4c01000369a3) Water Main Valve] checkDeviceState (refresh after 14500 seconds)
[7/18/2024, 3:59:38 PM] [YoLink] [YS4909-UC (d88b4c01000369a3) Water Main Valve] YoLinkAPI.getDeviceState [lite]
[7/18/2024, 3:59:38 PM] [YoLink] [verbose] YoLinkAPI.getAccessToken
[7/18/2024, 3:59:38 PM] [YoLink] [verbose] SENDING:
{
  "time": 1721332778417,
  "method": "Manipulator.getState",
  "targetDevice": "d88b4c01000369a3",
  "token": "C346EFC1AEEADD1D9C69CE30815DBD30"
}
[7/18/2024, 3:59:38 PM] [YoLink] [verbose] MQTT connect: subscribe to messages for 'yl-home/d3d51dfc169c4f7aa02b1bf5901a92db/+/report'
[7/18/2024, 3:59:38 PM] [YoLink] MQTT subscribed: yl-home/d3d51dfc169c4f7aa02b1bf5901a92db/+/report
[7/18/2024, 3:59:40 PM] [YoLink] [verbose] RECEIVED:
{
  "code": "000000",
  "time": 1721332780252,
  "msgid": 1721332780252,
  "method": "Manipulator.getState",
  "desc": "Success",
  "data": {
    "state": "open",
    "battery": 4,
    "powerSupply": "battery",
    "delay": {
      "ch": 1,
      "off": 0
    },
    "openRemind": 0,
    "version": "0911",
    "time": "2024-06-18T07:59:39.000Z",
    "tz": -4,
    "loraInfo": {
      "netId": "010205",
      "signal": -70,
      "gatewayId": "d88b4c1603008c02",
      "gateways": 1
    }
  }
}
[7/18/2024, 3:59:40 PM] [YoLink] [verbose] [YS4909-UC (d88b4c01000369a3) Water Main Valve] Battery level: 100%
[7/18/2024, 3:59:40 PM] [YoLink] [YS4909-UC (d88b4c01000369a3) Water Main Valve] At 7/18/2024, 3:59:40 PM: Device state updated: Valve (both): open, Battery: 4 [lite]
[7/18/2024, 3:59:40 PM] [YoLink] Device initialized: YS4909-UC (d88b4c01000369a3) Water Main Valve
[7/18/2024, 3:59:40 PM] [YoLink] [verbose] [YS4909-UC (d88b4c01000369a3) Water Main Valve] Set data refresh timer to run every 14498 seconds

Thanks.

dkerr64 commented 4 months ago

I just pushed a new beta. Please try it and capture logs.

Thanks.

ssnthedj commented 4 months ago

Attached is an updated log. With your latest beta I can actually control the valve now so that's progress! I think there may still be some issues with reporting data to homekit though. For instance I see the words "waiting" and "starting" a lot in the homekit UI for the meter. I have not captured logs when controlling the valve from the yolink app but I can certainly do that if you still need me to. homebridge-yolink.log.txt

ssnthedj commented 4 months ago

when I initially change the state of the valve in homekit it updates the status, for instance when I open the valve it says "running" as the status in homekit. However if a leave it for a while and come back it just says "starting" or "waiting". If I close the valve it says "off" in homekit. I have not left it closed to see if I come back if it still says that but I might try and see what happens. It is worth nothing this is not just a smart valve but also a water meter so hopefully get can get that to report as well. Of course that can't really be tested until it is installed into my water line (hopefully next week).

dkerr64 commented 4 months ago

Good, we are making progress. The log file is very helpful, I can now see all the data fields and I do see at least one change I'll need to make.

I have the original manipulator valve, and I also see "starting" and "waiting" when I control the valve. I am not sure what causes those but it may have something to do with the InUse characteristic. This is supposed to indicate that fluid is flowing through the valve. For the original YoLink valve it did not report whether water was flowing, so I just assume that if the valve is open then it is InUse. However, your valve does include a waterFlowing value, and I now use that to set the InUse characteristic property... which for now is always false. But once it is installed, that waterFlowing value should change to true (or false) and hopefully that will make the "starting" and "waiting" states change.

dkerr64 commented 4 months ago

As for anything other than open/close, our options are limited. The HomeKit accessory documentation does not include water meters (or power meters). Some 3rd party apps have added support for device types not in the official spec. Examples are smart plugs which can measure power consumed by the connected device, but it is hard to implement (and I have not done it for the YoLink smart plugs). Water meters are likely in the same situation.

It might be possible to add a leak detector accessory, that is in the HomeKit spec, and I do support the YoLink leak detectors. This water meter is reporting a number of possible alarm conditions, one of which is leak. So that may be possible.

I'm also interested in the temperature field. Right now it is presumably reporting ambient temperature... but I'm curious if once you have it installed it will report the water temperature.

dkerr64 commented 4 months ago

I also see quite a lot of YoLink API error code: 020104 Device is busy, try again later. errors in the log. These will likely go away once you settle down with it installed. See comments in my readme.

dkerr64 commented 4 months ago

However, your valve does include a waterFlowing value, and I now use that to set the InUse characteristic property... which for now is always false. But once it is installed, that waterFlowing value should change to true (or false) and hopefully that will make the "starting" and "waiting" states change.

However, this may not be desired behavior... if you open a valve that controls your whole house, then water may not flow immediately... another faucet would need to be opened. We will have to test this once you have it installed and may have to revert back to how I have it for the older valve.

tothbe commented 4 months ago

@dkerr64 The temperature field will always show the ambient temperature as it measures that for the anti-freezing function and alerting built in to the device. I am also aiming to buy one soon so I made some investigation on its possibilities.

dkerr64 commented 4 months ago

I just pushed beta 3 which adds support for the temperature sensor. Note that temperature sensors (that are embedded within another type of device) are not enabled automatically... you have to request it in the configuration either globally for all devices (that provide temperature) or per-device. See README.

As I do not have one of these devices I cannot test this, so I'd appreciate some feedback and a verbose log.

dkerr64 commented 4 months ago

And beta 4 has leak sensor added. Again I cannot test this so I'd appreciate feedback and verbose log.

ssnthedj commented 4 months ago

Would you like me to install beta 3, collect logs and then install beta 4 and do the same or can I just go direct to beta 4? I do have some of the leak sensors too I was planning to use in tandem with the valve.

ssnthedj commented 4 months ago

However, your valve does include a waterFlowing value, and I now use that to set the InUse characteristic property... which for now is always false. But once it is installed, that waterFlowing value should change to true (or false) and hopefully that will make the "starting" and "waiting" states change.

However, this may not be desired behavior... if you open a valve that controls your whole house, then water may not flow immediately... another faucet would need to be opened. We will have to test this once you have it installed and may have to revert back to how I have it for the older valve.

I think that's fine actually because then I can easily see if a device (sink, shower etc) is using water in my house.

dkerr64 commented 4 months ago

Would you like me to install beta 3, collect logs and then install beta 4 and do the same or can I just go direct to beta 4? I do have some of the leak sensors too I was planning to use in tandem with the valve.

Just go straight to beta 4.

Thanks

ssnthedj commented 4 months ago

See updated logs after upgraded to beta 4. I removed anything prior to the beta 4 upgrade. In homekit it still says a status of "waiting" when the valve is open. Maybe that will change when water is flowing though it? Also I do have one of Yolink's stand alone water leak detectors but I don't see that in homekit, I guess you were talking about the leak detection feature of the water meter/valve itself.

homebridge-yolink.log.txt

ssnthedj commented 4 months ago

When the valve is closed it does report a status of "off" in homekit but when it's open it just says "waiting".

Valve Closed Room overview screen Valve Open

ssnthedj commented 4 months ago

Also, here is how the meter/valve shows up in the Yolink app.

Overview Details 1 Details 2

ssnthedj commented 4 months ago

Oh I just noticed there is a firmware update already. This is a brand new device from Yolink so that does not surprise me. Let me know if you want me to go ahead and update the firmware. That might break some stuff but better to find out now than later.

dkerr64 commented 4 months ago

Oh I just noticed there is a firmware update already. This is a brand new device from Yolink so that does not surprise me. Let me know if you want me to go ahead and update the firmware. That might break some stuff but better to find out now than later.

Yes go ahead and update. It can take some time because the LoRa is very slow.

dkerr64 commented 4 months ago

When the valve is closed it does report a status of "off" in homekit but when it's open it just says "waiting".

Yes I think this is expected until you have it installed... the waterFlowing status is always false... which is what would be expected when the valve is closed. When you turn it on I think it is waiting for the waterFlowing state to change to true... which is not happening until you have it installed.

dkerr64 commented 4 months ago

See updated logs after upgraded to beta 4. I removed anything prior to the beta 4 upgrade. In homekit it still says a status of "waiting" when the valve is open. Maybe that will change when water is flowing though it? Also I do have one of Yolink's stand alone water leak detectors but I don't see that in homekit, I guess you were talking about the leak detection feature of the water meter/valve itself.

Your existing leak detectors should appear in Apple Home as "Alarm sensors" which in a room on MacOS version show in the top right. Click on that and a window opens listing them, like this...

image

And yes, I have added a leak sensor accessory to the water meter itself. I do not know how that will appear in Apple Home, if not like above then it may be inside the settings for the valve itself... for example, this dehumidifier has three accessories... the dehumidifier itself, a temperature sensor and a humidistat...

image

I suspect that the water temperature sensor and leak detector may appear there.

dkerr64 commented 4 months ago

Your most recent log looks good. I'm assuming you did not enable the temperature sensor as I don't see logs related to that.

I think we're at the point of waiting until this is installed to see how it works in real life.

dkerr64 commented 4 months ago

How does this device determine if there is a leak? I'm guessing it is something like measuring a really slow flow rate... or an excessively fast flow rate? Somehow it would be good to test that a leak alert is properly sent to HomeKit.

dkerr64 commented 4 months ago

Looking at the YoLink app screenshots, I see that the temperature is shown. I decided that for this device we should always create a temperature accessory service HomeKit rather than require a config settings. This is enabled in beta 5.

ssnthedj commented 4 months ago

Found the my other water leak sensor in the alarms section as well as some other devices from a different brand I thought were not showing up. Turns out I was just not looking in the right place. so thanks for that. As for the leak sensor built into the water meter itself I'm not sure exactly how that detects leaks. I agree I think it makes sense to enable the temperature sensor by default and I am indeed seeing this after updating to the latest beta. I kinda wish it was measuring the water temp but I guess ambient air temp makes more sense to alert if it's getting near freezing mark. I don't know exactly which day yet but I am hoping to get a plumber to install the valve this week. Once that is done I'll send you some updated logs. I really appreciate all the work you're doing on this.

ssnthedj commented 4 months ago

Here's how the device shows up now in Homekit.

IMG_9377 IMG_9378

dkerr64 commented 4 months ago

Looking good. I will wait until after you have it installed and we confirm our theory on the water flowing / in-use indicator before I release it into the mainline.

Thanks.

ssnthedj commented 4 months ago

Just had the valve installed today. Opened it and turned faucets on and off and captured the logs. Unfortunately homekit does not seem to update if water is flowing so I think we are still missing something. Also, not sure if this matters but it appears the temperature is in fact water and not ambient temp. At least it says "water temp" in the Yolink app.

homebridge-yolink.log_07-24-2024.txt

dkerr64 commented 4 months ago

Is the Apple Home app still showing "waiting" when you open the valve?

I have to decide what to do with the HomeKit "InUse" characteristic. For the old valve I set it to in-use whenever the value was open, but for this new valve I use the value of "waterFlowing" in the data we get back from it. But if that data is not coming in promptly then we may not be able to rely on it.

Your log does show "waterFlowing" as true sometimes, false other times. But I don't know what Apple Home does with the InUse setting.

I just pushed a beta 6... no material differences, I tweaked the code that handles MQTT messages, and now check if the valve reports a fault or not.

ssnthedj commented 4 months ago

So I updated to beta-6 and now the status does eventualy update in homekit but it is far from instant. It look about 30 seconds to update in the Yolink app and I was pressing the manual refresh button so I'm not sure how long it would of taken if I didn't do that. I've attached the latest logs as well as a few screenshots. This device is a bit different since it's not just a valve. It's a water meter with a valve. So I do think it makes sense for "water flowing = in use" however maybe that status could be broken out separately? Maybe as a "sensor" and then have the valve status report "open" or "closed"? I will say I don't really how it always says "starting" in homekit when the valve is open but water is not running.

homebridge-yolink.log_07-24-2024_9PM.txt

IMG_9399 IMG_9405 IMG_9404

ssnthedj commented 4 months ago

Here's what it looks like in Yolink app. Also it's worth noting even when status is showing as "running" in homekit when you click into the details view of the valve it still says "waiting".

Screenshot 2024-07-24 at 9 33 39 PM
ssnthedj commented 4 months ago

I just did another test this time not opening the Yolink app but just opening homekit and looking at the status of the valve. I turned on a sink and at the 1:30 mark it was still not showing as "running". Then I opened up the Yolink app which showed a status of "water is currently running" then I switched back to the homekit app and the status had updated there. So it seems the Yolink app triggered somekind of status refresh. There is even a manual update refresh button in the valve.

ssnthedj commented 4 months ago

The water meter/valve runs on batteries by default but does have the option of connecting a power adapter. I wonder if it would update it's status more often if it was AC powered. It seems like even in the Yolink app the water flowing status is not real-time.

dkerr64 commented 4 months ago

So, I can explain what is going on, but I'm not sure if there are any changes we can make. Let me explain how things work...

We can ask the device for current state, and we receive unsolicited messages from the device which report its state.

When you open up Apple Home it generally requests an update. In your log it does this only once (timestamp 1:12:49am) where a WaterMeterController.getState request is sent by us to the device... and you can see in the log the request sent and the received status.

The reason there is only one instance of this in the log is because I have deliberately throttled the number of times I ask a device for status... HomeKit can be very talkative and YoLink imposed rate limits on their servers, and the LoRa protocol is slow. So I severely cut back how often I actually ask the device for status, instead relying on the unsolicited updates and providing cached status when HomeKit asks. In the log HomeKit is actually asking for status 90 times... usually multiple in the space of under a second!

The unsolicited messages come in as MQTT and they take two forms in your log... a WaterMeterController.Report event and a WaterMeterController.getState event.

Report events are sent regularly by a device to provide updated state, generally they come in regular intervals. There are 8 report events over a couple of hours, all of them report water not flowing.

The getState events appear to be triggered when you use the YoLink app... it is telling all other interested parties (us) that someone (the YoLink app) has requested device status, and provides the status. There are 4 of these in the log in the space of ~90 seconds. The first two report water flowing as false, the second two as true.

What I am not seeing in the log is any real time reporting of changes. Other devices send a StatusChange event, which would come through MQTT as WaterMeterController.StatusChange but there are none in the log. So the device is not reporting in realtime when water starts/stops flowing.

The workaround to this is to add a config option to the plugin settings to change the "refresh after" value. Do this just for this device, so in the config user interface add the device using its device identifier (yours is d88b4c01000a13fb) then set the refresh time to something like 60 seconds, that will result in a lot more requests for status actually going through to the device.

I think I will also to reach out to YoLink and ask. I have seen them update device firmware in the past to add MQTT messages and it feels like that is what is needed here, polling every 60 seconds is really not ideal.

In other words... the plugin is working, it is reporting what it receives from the device.

Thanks.

EDIT: Minimum time between refreshes is 60 seconds, cannot be less.

dkerr64 commented 4 months ago

The water meter/valve runs on batteries by default but does have the option of connecting a power adapter. I wonder if it would update it's status more often if it was AC powered. It seems like even in the Yolink app the water flowing status is not real-time.

I doubt it will make a difference. But if you have a power supply then I would like to see how this is reported in the logs... both with batteries inserted as well, and without (if that is a supported configuration). I do report battery health to HomeKit, so if that changes when it has external power supply then I may need to update the code for it.

dkerr64 commented 4 months ago

@ssnthedj I emailed YoLink yesterday, no reply yet. In the meantime I have changed the plugin (beta 7) to report InUse based on whether valve is open or closed... the same as I do for the older valves that do not report it. I also added a config setting to allow users to switch this to use the waterFlowing Status from the valve if desired.

If YoLink update the firmware to accurately report the status change then we can try changing the default.

I've documented this in the Device Notes of the readme.

If this looks good in your testing then I will remove beta tag and publish it in a release update.

dkerr64 commented 4 months ago

@matrixd2 I see you work on the YoLink home assistant plugin. I wonder if you could review the discussion in this thread and comment? Thanks.

ssnthedj commented 4 months ago

@dkerr64 thanks for all the updates and testing you've done. I updated to the latest beta and it is reporting water is "on" when valve is open. I think that's better than the using the "water flowing" data is that is so delayed. I appreciate you reaching out to yolink about that. Even in the Yollink app the status of "water flowing" is quite delayed. I've seen it take several minutes to update. Really not ideal at all. Maybe they can make some changes. I do wonder if it was a battery saving measure for it to update less often. I am going to try and order a power adapter for mine because honestly I don't like relying on batteries. Strangely Yolink does not sell one just lists the voltage so I need to find something compatible.

I did have one thought would it be possible to break out the "water flowing" into a separate sensor in homekit? Right now we have water temp, leak sensor so maybe water flowing could be added there but the main status is still if the value is open/closed?

matrixd2 commented 4 months ago

@dkerr64 Thank you for the work you are doing now.

Water is considered to be flowing only when the water flow exceeds a certain threshold. So there may be a delay in the water flowing state.

When the water flowing status changes, the device will not send data to the server, so your mqtt client will not receive the StatusChange message, You need to refresh by calling the getState API

dkerr64 commented 4 months ago

I did have one thought would it be possible to break out the "water flowing" into a separate sensor in homekit? Right now we have water temp, leak sensor so maybe water flowing could be added there but the main status is still if the value is open/closed?

Leak and Temperature are easy to break out... as HomeKit has specific services for those. But the water flowing (or InUse) state is built into the HomeKit Valve service and there is no separate service available for this. So we would have to fake it... call it a Switch or a Contact Sensor. I'm not sure that this buys us anything as unless/until YoLink provide more timely reporting its status. Unless the value can be relied upon, I think it is misleading to report it at all.

dkerr64 commented 4 months ago

@dkerr64 Thank you for the work you are doing now.

Water is considered to be flowing only when the water flow exceeds a certain threshold. So there may be a delay in the water flowing state.

When the water flowing status changes, the device will not send data to the server, so your mqtt client will not receive the StatusChange message, You need to refresh by calling the getState API

@matrixd2 Thank you for confirming what we have observed. This is unfortunate, hopefully YoLink will update the firmware to provide a StatusChange MQTT message. Regularly sending getState requests is really not the right way to do this. With rate limits on YoLink cloud servers and the generally low capacity and slow speed of the LoRa network polling should be a last resort. That said, I can do it and have documented it as a work-around for now.

I did send an email to R&D Team (address posted at bottom of the API page) brining this to their attention, but no reply yet.

Also, can you confirm how a leak or freeze is notified? Will I receive a MQTT for those, if so what will the event be? I have seen Alert events for other devices, is that what the WaterMeterController will send too... that is what I have assumed and coded to for now.

Thanks.