logo elektroda
logo elektroda
X
logo elektroda

Tuya JSON specification - how to get smart device IO template automatically?

p.kaczmarek2 3012 11
ADVERTISEMENT
📢 Listen (AI):
  • Sample Tuya configuration JSON with various parameters.
    Automatic Tuya JSON extraction allows you to automatically configure the GPIO template of your smart device. Unlike in Tasmota, where all GPIO probing has to be done manually, OpenBeken flasher BK7231GUIFlashTool is able to extract the Tuya GPIO roles, so it can automatically tell which pin is used for relay, which pins are used for PWMs, which baud is used for TuyaMCU, etc.
    The usage of this feature has been presented already in our video:



    Today I am going to show you few more details about the Tuya JSON inner working.
    Let's start by considering a sample JSON:
    Code: JSON
    Log in, to see the code

    This is a description of a simple 6014HA device, featuring a single relay, WiFi LED and a button.
    The most important keys here start with "rl1_", "netled_", and "bt1_".
    The "rlX_" prefix stands for relay. There can be multiple relays, so X stands for the relay number.
    There are multiple keys for relays:
    Code: C / C++
    Log in, to see the code

    The "lv" key stands for active level. Value 1 means that relay is active-high, so relay is closed when the given GPIO has high level.
    This is a standard for all Tuya products so we are not even parsing that.
    The more important key is rl1_pin, which is a GPIO index of Relay pin. 14 there means that relay is on P14 (not on the 14th pin of QFN case).
    The same logic applies to buttons. There can be also multiple buttons, for example, bt1_pin is a GPIO of a first button, and bt2_pin is a GPIO of second button. Futhermore, there can be also LEDs following relay states, so led1_pin, led2_pin, etc is possible.
    Of course, most of IO roles are not numbered, for example, status_led_pin. Here is more detailed list of Tuya JSON keys:
    - status_led_pin - this is a WiFi pairing LED
    - bz_pin_pin - buzzer pin
    - samp_sw_pin - battery ADC sampling enable pin
    - samp_pin - battery ADC sampling pin used to measure battery voltage
    - i2c_scl_pin and i2c_sda_pin - I2C bus pins
    - alt_pin_pin - ALERT pin (interrupt from temperature/humidity sensor)
    - one_wire_pin - OneWire bus for DS18B20, etc
    - backlit_io_pin - backlight pin
    - max_V and min_V - battery voltages for battery measurement
    - rl_on1_pin and rl_off1_pin - this is used for bridge (bistable) relay
    - k1pin_pin (etc) - alias for button pin
    - door1_magt_pin - door sensor pin for non-TuyaMCU device
    - onoff1, onoff2, etc - this is used to connect old-style bistable switch on relay modules
    - basic_pin_pin - this is a PIR sensor for motion detectors
    - netled_pin - WiFi LED (again), also may be used as netled1_pin
    - ele_pin - BL0937 energy metering pin ELE
    - vi_pin - BL0937 energy metering pin VI (Voltage/Current)
    - sel_pin_pin- BL0937 energy metering sel pin
    - sel_pin_lv - BL0937 sel pin level (usually 1, active-high)
    - r_pin, g_pin, b_pin, c_pin, w_pin - R G B C W PWMs for lamps and LEDs
    - micpin, also sometimes mic - microphone input for music-based effects
    - ctrl_pin - it seems it may be a LED strip controller button
    - mosi - SPI MOSI, always 16 on Beken, used for example to drive WS2812B
    - buzzer_io - again, buzzer pin
    - buzzer_PWM - buzzer PWM frequency
    - irpin or infrr - IR input (receiver)
    - infre - IR output (sending)
    - reset_pin - reset/pair button
    - wfst_pin - WiFi state LED, again, pairing LED
    - pwmhz - PWM frequency (for LED?)
    - pirsense_pin - PIR sensitivity adjustment (probably via PWM)
    - pirlduty - PIR adjust duty (min)
    - pirfreq - PIR adjust frequency
    - pirmduty - PIR adjust duty (max)
    - pirin_pin - PIR input (read this value to detect PIR state, 1 or 0)
    - total_bt_pin - again, reset/pair button
    There are also some twowire (I2C-like) LED controllers, like SM2253, BP5758. They are using keys:
    - iicscl - I2C (not really I2C, because adressation is missing) Clock
    - iicsda - I2C (as above) Data
    I haven't investigated it fully, but it seems that LED drivers have separate RGB and CW (or separately, C and W) current limits.
    - ehccur, wampere, iicccur - SM2135 current levels (color mode?)
    - dccur - BP5758D current level (color mode?)
    - dwcur - BP5758D current level (cool/warm mode?)
    - cjwcur - BP1658CJ current level (cool/warm mode?)
    - cjccur - BP1658CJ current level (color mode?)
    - _2235ccur - SM2235 current level (color mode?)
    - _2335ccur - SM2335 current level (color mode?)
    - kp58wcur - KP18058 current level (cool/warm mode?)
    - kp58ccur - KP18058 current level (color mode?)
    - iicr, iicg, iicb, iicc, iicw - color indexes for I2C-like RGBCW drivers (they are not standarized)
    Futhermore, there is BL0942 and TuyaMCU related keyword:
    - baud - TuyaMCU/BL0942 communication baud rate
    And there is a generic information keyword:
    - module - name of the WiFi module, like CBU, CB3S, etc

    The list above is not comprehensive and more information will be added there over time.

    To extract Tuya Config JSON from a live device or from 2MB flash backup, use our flasher:
    https://github.com/openshwprojects/BK7231GUIFlashTool
    To decode Tuya JSON, you can use our online utility:
    https://openbekeniot.github.io/webapp/templateImporter.html
    For some sample Tuya flash dumps, check out flash dumps library:
    https://github.com/openshwprojects/FlashDumps

    As you can see, Tuya JSON decoding can make "cloudless" conversion of your Tuya device much faster and easy than it was in case of ESP8266/Tasmota devices. Most of the related GPIO roles are included in JSON, so you don't have to guess anymore. Futhermore, OpenBeken can automatically convert Tuya JSON to a template, so all you have to do is just to copy and paste the extracted text.

    Cool? Ranking DIY
    Helpful post? Buy me a coffee.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 14177 posts with rating 12057, helped 645 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #2 21155828
    divadiow
    Level 38  
    very interesting.

    I fed ChatGPT all the cloudcutter json files and it came back with this deduped list.

    Code: Text
    Log in, to see the code
  • ADVERTISEMENT
  • #3 21156229
    p.kaczmarek2
    Moderator Smart Home
    They don't have all pins, for example, "micpin" seems to be missing.
    Helpful post? Buy me a coffee.
  • #4 21156243
    divadiow
    Level 38  
    true. those are included in the jsons but my GPT request wasn't good enough. anyway, it's a snapshot of the variety used, as you say in the first post.
  • ADVERTISEMENT
  • #5 21156244
    p.kaczmarek2
    Moderator Smart Home
    Btw those key names doesn't seem standarized, as I have seen both "micpin" and "mic"
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #6 21156250
    divadiow
    Level 38  
    it'd be a bit of task to have Easy Flasher cater for them all I guess
  • Helpful post
    #7 21447785
    divadiow
    Level 38  
    netled_reuse also indicates if an LED is shared - I guess for wifi then for on/off purposes once wifi established. This could be used to assign a new dual-purpose pin assignment that changes the LED behaviour after wifi connected - removing the need for any autoexec script.

    https://www.elektroda.com/rtvforum/find.php?q=netled_reuse
  • #8 21447813
    p.kaczmarek2
    Moderator Smart Home
    So you'd like to have a separate LED pin role? I don't know, LED_State? StateLED?
    Helpful post? Buy me a coffee.
  • #9 21447841
    divadiow
    Level 38  
    An idea perhaps. I think I saw this floated before somewhere. Even if netled_reuse is wrong (I don't think it is), a single assignment to change the behaviour of LED depending on wifi connection would be a cool thing.
  • #10 21447878
    insmod
    Level 31  
    My idea is to make keep WifiLED, but allow assigning channel to it. So, as soon as wifi is connected, it would act like a led for assigned channel.
    But, if channel is 0, then keep original behavior.
  • #11 21447955
    p.kaczmarek2
    Moderator Smart Home
    Good idea but not perfect - channel 0 is often used for relays, so it's a normal channel, not a special value.
    We can't do -1 as well, because channel indexes are stored as unsigned byte....
    Maybe we could introduce a special value or something, like 255.
    Helpful post? Buy me a coffee.
  • #12 21834399
    divadiow
    Level 38  
    I got LLM to make a bulk bin file finder, Tuya KV locator, decrypter and json extractor for the purpose of extracting all pins in user_param_keys Easy Flasher is not yet handling. This was run against FlashDumps and a load of other local collections scraped from various places.

    Initial findings:

    JSON files in ZIP: 1609
    Unique (byte-identical de-dupe): 1361
    Duplicates removed: 248
    (Counts shown as: unique KVs / total files, after byte-identical de-dupe in the combined pack.)

    Pin assignment keys to add
    1) key_pin (+ key_lv) — 15 / 18
    - Map as: Button input (like bt*_pin / k*pin_pin) + polarity via *_lv

    2) sound_pin6 / 6
    - Map as: Buzzer/Sounder output (same bucket as buzzer_io / bz_pin_pin)

    3) total_led_pin (+ total_led_lv) — 4 / 4
    - Map as: Indicator LED pin (status/overall LED)

    4) display_led1_pin (+ display_led1_lv) — 2 / 2
    - Map as: Indicator LED pin (treat like led1_pin)

    5) net_led_pin (+ net_led_lv) — 2 / 3
    - Map as: WiFi/Status LED alias (treat like netled_pin / netled1_pin) or report as TODO

    6) lightplus_sw_pin (+ lightplus_sw_lv) — 2 / 2
    7) lightminus_sw_pin (+ lightminus_sw_lv) — 2 / 2
    - maybe map as: Button inputs (brightness +/- style); polarity via *_lv or report as TODO

    8) tamper_pin_pin (+ tamper_pin_lv) — 1 / 1
    - maybe map as: Digital input (tamper switch); polarity via *_lv or report as TODO

    GPIO-bearing keys to surface (pins, but role not safe to auto-assign)
    9) zero_io_pin2 / 2
    - Likely timing/zero-cross style input; recommend “report only” for now

    10) hl_ctl_io_pin2 / 2
    11) a_ctl_io_pin2 / 2
    12) p_sw_io_pin2 / 2
    - “control IO” pins (ECR6600-style); recommend “report only” for now

    13) Singletons (report only): expowctrl_pin, p_type_pin, l_pin, m_pin1 / 1 each
    14) RGB naming variants (report only unless you want to treat as PWM pins):
    red_pin, green_pin, blue_pin1 / 1 each (+ corresponding *_lv seen)

    “Pins” that don’t include “_pin” but should be handled as aliases
    15) ir12 / 17
    - Map as: IR Receiver pin (alias to irpin/infrr-style meaning)

    16) MOSI2 / 2
    17) MISO3 / 3
    - Map as: SPI MOSI/MISO aliases (tuyaconfig.cs only handles uppercase MOSI/MISO)

    UART-ish GPIO assignments
    18) ele_rx / ele_tx14 / 16
    19) series_rx / series_tx4 / 6
    - Recommend printing as: “UART (ele/series) RX/TX on Pxx”

    Not factored in EF assignments: polarity / active-level keys
    Top prevalence *_lv keys in this pack (unique KVs with user_param_key):
    - rl1_lv 314
    - bt1_lv 240
    - netled1_lv 203
    - total_bt_lv 116
    - netled_lv 104
    - rl2_lv 101
    - led1_lv 82
    - sel_pin_lv 74
    - wfst_lv 26
    - key_lv 15
    - basic_pin_lv 13
    - status_led_lv 13
    (Plus many others: bt2_lv, rl3_lv, etc.)

    We already have Role vs Role_n variants (Btn/Btn_n, Rel/Rel_n, LED/LED_n, WifiLED/WifiLED_n, BL0937SEL/BL0937SEL_n, etc.), so wiring *_lv into role selection seems like it could be straight forward.

    Obviously all needs checking and proving out.
📢 Listen (AI):

Topic summary

The discussion focuses on the automatic extraction of Tuya JSON configurations to identify GPIO roles in smart devices, enabling automatic setup of IO templates without manual probing as required in Tasmota. The OpenBeken BK7231GUIFlashTool is highlighted for its capability to extract pin functions such as relays, PWMs, and TuyaMCU baud rates from JSON files. A deduplicated list of common GPIO key names extracted from cloudcutter JSON files was shared, including pins like alarm_pin, bt1_pin, relay_pin, and netled_pin, though some keys like "micpin" are missing or inconsistently named (e.g., "micpin" vs. "mic"). The lack of standardized key naming complicates comprehensive support in flashing tools like Easy Flasher. Additionally, the concept of dual-purpose LED pins was discussed, particularly the "netled_reuse" flag indicating shared LED usage for WiFi and device status. Proposals include assigning WiFi LED behavior to specific channels, with considerations on channel numbering constraints and potential use of special values (e.g., 255) to differentiate LED roles. These insights aim to improve automatic GPIO role detection and flexible LED behavior assignment in Tuya-based smart device firmware.
Summary generated by the language model.
ADVERTISEMENT