zaratul
(Andreas Marek)
December 23, 2021, 7:09am
1
Hi there,
I am fetching power values from the rest api of solar battery. They come as hex value e.g. 0x4413934F
The conversion via templates failes, because you can only convert hex to int there and this prduces the wrong value.
Via phyton it would be something like this :
import struct; print(struct.unpack("!f", bytes.fromhex(input()))[0])
Can I somhow use a pyhton script inside a template to convert the value?
tom_l
December 23, 2021, 8:58am
2
{{ ('0x4413934F')|int(base=16) }}
→ 1142133583
How is this the wrong value?
koying
(Chris B)
December 23, 2021, 9:10am
3
Likely because it’s a hex representation of a float value (IEEE 754).
Floating Point to Hex Converter gives me 590.302. Is it the actual value?
1 Like
tom_l
December 23, 2021, 9:45am
5
In that case, does the inverter have a modbus interface?
If so, this integration can deal with that data type .
zaratul
(Andreas Marek)
December 23, 2021, 9:46am
6
maybe it has, but I can’t use it
we need this: {{ ('0x4413934F')|float(base=16) }}
koying
(Chris B)
December 23, 2021, 10:00am
7
You could have used a command-line sensor to call a python script to do the conversion, but you’d need template support for the command, and looks like Rationalize command_line template usage by koying · Pull Request #57536 · home-assistant/core · GitHub is in limbo
Would float_fromhex or similar be considered? Anyone’s guess…
Maybe a tad too specific…
Hi, how did you solve the problem?
I have similar problem and I’m going crazy converting a hexadecimal value to a float
Y.Shuvarikov
(Yuriy Shuvarikov)
September 18, 2023, 7:16am
9
Did it. Maybe not as good, as it could be, but it works.
{{unpack(pack(states('sensor.1031_hot_meter_val')[12:-8]| int(base=16), ">I"), "f")}}
1 Like