Hi everyone! I want to apologize right away, I am not a programmer, I am a radio-electronic engineer. And my native language is not English =(
The situation is like this - I have a TTGO SIM800L (it is an ESP32 module + GSM modem that can receive/send SMS, calls and USSD requests) connected via ESPHome, fully configured and working, among all sensors I have “Ussd Code”, I want to use it to get the balance of the personal account, but I get hex code. And here I experienced difficulties in converting this code to text. I have a programmer friend who doesn’t know what HA is at all, but he wrote me a little code in python to convert it to text and it works fine if I run it in my terminal.
Here it is:
def ucs2_to_utf8(s):
result = ""
i = 0
while i < len(s) - 3:
code = ((int(s[i:i+2], 16) << 8) +
int(s[i+2:i+4], 16))
result += chr(code)
i += 4
return result
hex_string = "hex code from operator"
decoded_string = ucs2_to_utf8(hex_string)
print(decoded_string)
But then I am faced with the problem of automating this code, so that the state of the original sensor is run through this code and shown in a new specially created text sensor.
I realize that I need to use Python Scripts and call this code through it, but I don’t know the syntax and how it all works. =(
Maybe I’m wrong and there are easier ways to do this, but I’m hanging on to this code because at the very least it works!
I will be grateful for any help and advice. Thanks!
P.S. I can share a working code for TTGO SIM800L if anyone is interested.