## The problem
Device: TUYA APP 1P 40A Rail Din WIFI compteur d'énergie intel…ligent consommation d'énergie kWh interrupteur de disjoncteur
(link: [https://www.amazon.fr/dp/B09FPGNYFK/ref=pe_27091421_487030221_TE_SCE_dp_1](https://www.amazon.fr/dp/B09FPGNYFK/ref=pe_27091421_487030221_TE_SCE_dp_1))
When I try to get data in localtuya, the power consumption (which works fine in Tuya-cloud) comes in as rawdata.
I think the string is a Base64-coded value, but I do not known how to convert this to current, voltage and power.
This is the reading I get on datapoint 6 (it comes as rawdata): CWYAAAAAAAA= x
This DP is described in the Tuya Cloud as "phase_a".
Should this value be converted into an array (because I saw this in other devices)? How?
Can anybody help pls? Thank you.
**UPDATE - 11/01/2023**
I have found a solution thanks to @MaximumSU for this device, so I can deconnect completely from TuyaCloud.
### Step 1 - INSTALL YOUR DEVICE IN LOCALTUYA:
- add the device in localtuya as usual, but manually add datapoint 6 (this will result in a base64-encoded string, which containes Voltage, Current and Power)
- make sure your device is getting readings in this variable (...phase_a): ex. "CTgAAAAAAAA="
### Step 2 - ACTIVATE THE PYTHON BASE64-decode SCRIPT:
- make sure pyscript is installed
- check configuration.yaml for these settings:
```
pyscript:
allow_all_imports: true
hass_is_global: true
```
- create a script named base64_int.py, which is located in <config>/pyscript (you can use [VSCode addon](https://community.home-assistant.io/t/home-assistant-community-add-on-visual-studio-code/107863))
- copy this code in your base64_int.py (you can rename your sensors and functions, but stay consequent and change everywhere!!)
```
@state_trigger('sensor.boiler_lavande_phase_a')
def base64_decode_sensor_lavande():
#!/usr/bin/env python3
import base64
import sys
# Decoding value from datapoint 6 which is base64-based (manually activated)
value_phase_a = state.get('sensor.boiler_lavande_phase_a')
value_decoded = base64.b64decode(value_phase_a)
# Calculating Voltage from base64-decoded value
value_voltage = round(int.from_bytes(value_decoded[0:2], byteorder='big')*0.1, 1)
state.set('sensor.boiler_lavande_voltage_py', value_voltage)
# Calculating Current from base64-decoded value
value_current = round(int.from_bytes(value_decoded[3:5], byteorder='big')*0.001, 2)
state.set('sensor.boiler_lavande_current_py', value_current)
# Calculating Power from base64-decoded value
value_power = round(int.from_bytes(value_decoded[6:8], byteorder='big')*0.001, 3)
state.set('sensor.boiler_lavande_power_py', value_power)
```
### Step 3 - PREDEFINE YOUR SENSORS (maybe this needs improvement):
- edit your <config>/.storage/core.entity_registry with VSCode
- paste these sensor-definitions after your current entities for this device (rename where necessary)
```
{
"aliases": [],
"area_id": null,
"capabilities": {
"state_class": "measurement"
},
"config_entry_id": "2e3c6d8ffb3712bdc356c18de5aeb41e",
"device_class": null,
"device_id": "ed2f8541d843b59ce8ef7fb22d4c54e2",
"disabled_by": null,
"entity_category": null,
"entity_id": "sensor.boiler_lavande_current_py",
"hidden_by": null,
"icon": null,
"id": "3b68d9cdddbd967a1fba6a52e531538ca",
"has_entity_name": false,
"name": null,
"options": {},
"original_device_class": "current",
"original_icon": "mdi:current-ac",
"original_name": "Boiler (Lavande) - Phase A - ampère",
"platform": "localtuya",
"supported_features": 0,
"translation_key": null,
"unique_id": "local_bf69893b9f9e8b7ee08vuu_6amp",
"unit_of_measurement": "A"
},
{
"aliases": [],
"area_id": null,
"capabilities": {
"state_class": "measurement"
},
"config_entry_id": "2e3c6d8ffb3712bdc356c18de5aeb41e",
"device_class": null,
"device_id": "ed2f8541d843b59ce8ef7fb22d4c54e2",
"disabled_by": null,
"entity_category": null,
"entity_id": "sensor.boiler_lavande_voltage_py",
"hidden_by": null,
"icon": null,
"id": "3b68d9cdddbd967a1fba6a52e531538ca",
"has_entity_name": false,
"name": null,
"options": {},
"original_device_class": "voltage",
"original_icon": "mdi:sine-wave",
"original_name": "Boiler (Lavande) - Phase A - voltage",
"platform": "localtuya",
"supported_features": 0,
"translation_key": null,
"unique_id": "local_bf69893b9f9e8b7ee08vuu_6vol",
"unit_of_measurement": "V"
},
{
"aliases": [],
"area_id": null,
"capabilities": {
"state_class": "measurement"
},
"config_entry_id": "2e3c6d8ffb3712bdc356c18de5aeb41e",
"device_class": null,
"device_id": "ed2f8541d843b59ce8ef7fb22d4c54e2",
"disabled_by": null,
"entity_category": null,
"entity_id": "sensor.boiler_lavande_power_py",
"hidden_by": null,
"icon": null,
"id": "3b68d9cdddbd967a1fba6a52e531538ca",
"has_entity_name": false,
"name": null,
"options": {},
"original_device_class": "power",
"original_icon": "mdi:lightning-bolt",
"original_name": "Boiler (Lavande) - Phase A - power",
"platform": "localtuya",
"supported_features": 0,
"translation_key": null,
"unique_id": "local_bf69893b9f9e8b7ee08vuu_6pow",
"unit_of_measurement": "kW"
},
```
Enjoy :-)
## Environment
- Localtuya version: 4.1.1
- Home Assistant Core version: Home Assistant OS 9.4, version: 2022.12.8
- [x] Are you using the Home Assistant Tuya Cloud component ?
- [x] Are you using the Tuya App in parallel ?
## Configuration `configuration.yaml` or `config_flow`
```
"bf69893b9f9e8b7ee08vuu": {
"friendly_name": "Tuya disjoncteur/compteur DIN 40A",
"local_key": "c36...a09",
"host": "192.168.0.222",
"device_id": "bf69893b9f9e8b7ee08vuu",
"protocol_version": "3.3",
"manual_dps_strings": "6",
"model": "breaker",
"dps_strings": [
"1 (value: 6747)",
"9 (value: 0)",
"11 (value: False)",
"12 (value: False)",
"13 (value: 0)",
"14 (value: 0)",
"15 (value: 0)",
"16 (value: True)",
"19 (value: 4567325321)",
"21 (value: False)",
"6 (value: -1)"
],
"entities": [
{
"id": 16,
"friendly_name": "Boiler (Lavande)",
"restore_on_reconnect": false,
"is_passive_entity": false,
"platform": "switch"
},
{
"id": 1,
"friendly_name": "Boiler (Lavande) - energieverbruik",
"unit_of_measurement": "kWh",
"device_class": "power",
"scaling": 0.01,
"platform": "sensor"
},
{
"id": 6,
"friendly_name": "Boiler (Lavande) - Base64-encoded raw data",
"unit_of_measurement": "x",
"device_class": "apparent_power",
"scaling": 1.0,
"platform": "sensor"
}
],
"product_key": "hz2o2tnezttmmx15"
```
## DP dump
I added the DP6 manually while adding the device in localtuya because it wasn't detected automaticly.
```
total_forward_energy | Integer | { "unit": "kW·h", "min": 0, "max": 99999999, "scale": 2, "step": 1 }
-- | -- | --
phase_a | Raw | {}
fault | Bitmap | { "label": [ "short_circuit_alarm", "surge_alarm", "overload_alarm", "leakagecurr_alarm", "temp_dif_fault", "fire_alarm", "high_power_alarm", "self_test_alarm", "ov_cr", "unbalance_alarm", "ov_vol", "undervoltage_alarm", "miss_phase_alarm", "outage_alarm", "magnetism_alarm", "credit_alarm", "no_balance_alarm" ] }
switch_prepayment | Boolean | "{true,false}"
energy_reset | Enum | { "range": [ "empty" ] }
balance_energy | Integer | { "unit": "kW·h", "min": 0, "max": 99999999, "scale": 2, "step": 1 }
charge_energy | Integer | { "unit": "kW·h", "min": 0, "max": 999999, "scale": 2, "step": 1 }
leakage_current | Integer | { "unit": "mA", "min": 0, "max": 1000, "scale": 0, "step": 1 }
switch | Boolean | "{true,false}"
alarm_set_1 | Raw | {}
alarm_set_2 | Raw | {}
breaker_number | String | { "maxlen": 255 }
leakagecurr_test | Boolean | "{true,false}"
```