-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add Tuya ME202Z submersible liquid level sensor #10689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MohammedAlhaid
wants to merge
1
commit into
Koenkk:master
Choose a base branch
from
MohammedAlhaid:add-me202z-sensor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import * as tuya from 'zigbee-herdsman-converters/lib/tuya'; | ||
| import * as exposes from 'zigbee-herdsman-converters/lib/exposes'; | ||
| const ea = exposes.access; | ||
|
|
||
| // Data points | ||
| const dp = { | ||
| distance: 2, // liquid height (cm) | ||
| cfg_threshold: 21, // tank depth (mm) | ||
| power_level_v: 5, // power voltage (raw) | ||
| }; | ||
|
|
||
| // Helper to round 1 decimal | ||
| function round1(v: number): number { return Math.round(v * 10) / 10; } | ||
|
|
||
| // Safely parse data point values | ||
| function getDataValue(datatype: any, data: any): number { | ||
| try { | ||
| if (Buffer.isBuffer(data)) return data.readUIntBE(0, data.length); | ||
| if (Array.isArray(data)) return data.reduce((acc, b) => (acc << 8) + b, 0); | ||
| return Number(data); | ||
| } catch { return Number(data); } | ||
| } | ||
|
|
||
| // Cache last known tank depth per device | ||
| const lastKnown: Record<string, { depthMm?: number }> = {}; | ||
|
|
||
| // FromZigbee converter | ||
| const fzLocal = { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
| me202z_report_all: { | ||
| cluster: 'manuSpecificTuya', | ||
| type: ['commandDataReport', 'commandDataResponse'], | ||
| convert: (model: any, msg: any, publish: any, options: any, meta: any) => { | ||
| const out: Record<string, any> = {}; | ||
| const items = msg.data?.dpValues || []; | ||
| const deviceId = meta.device.ieeeAddr; | ||
|
|
||
| let distanceCm: number | undefined; | ||
| let depthMm = lastKnown[deviceId]?.depthMm; | ||
|
|
||
| for (const { dp: id, datatype, data } of items) { | ||
| if (id === dp.distance) { | ||
| distanceCm = getDataValue(datatype, data); | ||
| out.liquid_height = distanceCm; | ||
| } else if (id === dp.cfg_threshold) { | ||
| depthMm = getDataValue(datatype, data); | ||
| lastKnown[deviceId] = { depthMm }; | ||
| out.cfg_threshold_mm = depthMm; | ||
| } else if (id === dp.power_level_v) { | ||
| const val = getDataValue(datatype, data); | ||
| out.power_level_v = round1(val / 10); | ||
| } | ||
| } | ||
|
|
||
| if (distanceCm != null && depthMm != null && depthMm > 0) { | ||
| out.level_percent = round1((distanceCm / (depthMm / 10)) * 100); | ||
| } | ||
|
|
||
| return out; | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| // ToZigbee converter | ||
| const tzLocal = { | ||
| cfg_threshold_mm: { | ||
| key: ['cfg_threshold_mm'], | ||
| convertSet: async (entity: any, key: any, value: any, meta: any) => { | ||
| const val = Number(value); | ||
| await tuya.sendDataPointValue(entity, dp.cfg_threshold, val); | ||
| lastKnown[meta.device.ieeeAddr] = { depthMm: val }; | ||
| return { state: { cfg_threshold_mm: val } }; | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export = [{ | ||
| fingerprint: [{ modelID: 'TS0601', manufacturerName: '_TZE284_mxujdmxo' }], | ||
| model: 'ME202Z-TS0601', | ||
| vendor: 'Tuya', | ||
| description: 'ME202Z submersible liquid level sensor (range 10–400 cm)', | ||
| fromZigbee: [fzLocal.me202z_report_all], | ||
| toZigbee: [tzLocal.cfg_threshold_mm], | ||
| exposes: [ | ||
| exposes.numeric('liquid_height', ea.STATE) | ||
| .withUnit('cm') | ||
| .withDescription('Measured liquid height in centimeters'), | ||
| exposes.numeric('level_percent', ea.STATE) | ||
| .withUnit('%') | ||
| .withDescription('Liquid level as % of configured tank depth'), | ||
| exposes.numeric('cfg_threshold_mm', ea.ALL) | ||
| .withUnit('mm') | ||
| .withValueMin(100) | ||
| .withValueMax(4000) | ||
| .withDescription('Configured tank depth in millimeters'), | ||
| exposes.numeric('power_level_v', ea.STATE) | ||
| .withUnit('V') | ||
| .withDescription('Power supply voltage'), | ||
| ], | ||
| onEvent: async (type: string, data: any, device: any) => { | ||
| if (['deviceAnnounce', 'deviceInterview'].includes(type)) { | ||
| const ep = device.getEndpoint(1); | ||
| try { await tuya.sendDataPointQuery(ep, dp.distance); } catch {} | ||
| try { await tuya.sendDataPointQuery(ep, dp.cfg_threshold); } catch {} | ||
| } | ||
| }, | ||
| }]; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you move this to
tuya.ts?