I’m looking for a python script that can return KNX DPT 12 (unsigned INT) intead of DPT 14 (float) as a hex value when feeding the python script with an INTEGER.
I found a script to generate DPT 14 but I need DPT 12 for the gas consumption.
Any help is welcome
float to hex
#!/usr/bin/python
import struct
import os
import sys
def float_to_hex(f):
return hex(struct.unpack('<I', struct.pack('<f', f))[0])
whex = float_to_hex(float(sys.argv[1]))
wcommand = "echo ".join(whex)
os.system(wcommand)
whex = whex.replace('0x','')
whex=' '.join([whex[i:i+2] for i in range(0, len(whex), 2)])
if whex == "0":
print "00 00 00 00"
sys.exit(0)
else:
print whex
sys.exit(0)
Actually I want to change the DPT that get sent out onto the KNX data bus and this doesn’t run onto the home assistant server itself. Whenever I succeed to transform the datapoint and get the correct hex value sent out over the KNX bus then Home assistant will be able to receive the telegram and configure the gas energy properly.
Home Assistant can receive and convert to the correct value whenever I use
type: ‘4byte_float’
type : ‘volume’
→ then HA treats it like a DPT 15 knx datapoint but this sensor type does not get picked up by the new HA energy management.
I only get the sensor inside the energy management working when I change the type : ‘volume_m3’
then I need a DPT 12 knx datapoint or otherwise I get incorrect values inside HA.
I can send the readings out and I have the correct integer or decimal value but I don’t know how I can convert it to DPT 12 (anything in Bash / Python or something that runs on an RPI will work)
Just read the value as it is received over the bus - with the type that decides the payload properly. And set the state_class attribute.
Then use customize to set an appropriate
I tried but it didn’t work. After further reading today I figured it could have been due to the unit configuration. I’m not sure if I typed m3 or m³ so if you are absolutely sure this should work I will give it another try.
maybe doing it your way will also help reading out decimal values? I’m not sure if the energy management is capable of obtaining 0,01 m³? or will it always be a full cube of gas?
another reason why it didn’t work could be that there are some cleanup problems of older sensors.
I noticed i needed to use a new name to be able to add the integer gas sensor
also there is still old config I cannot get rid off (see screenshot below)
I’ve created a new knx_sensor with again a new name and without the additional customize config and it already works
There must have been something wrong with reusing sensor.gas earlier… I must admit that sensor.gas was not using type: “volume” from the beginning but as you can see on top of this thread i was using 4bytes_float. During the course I changed the type to volume but probably HA was not able anymore to map the correct unit of measurement.
With the config above, HA was able to automatically find the correct unit of measurement
thanks for the support and guiding me in the right direction. Still I would like to do the sensor.gas and sensor.gasteller cleanup so additional support is welcome.
You can configure the decive_class right at the knx sensor - no need to customize.
Your template seems to exactly copy the knx sensor, only difference is that it doesn’t have a state_class. Is it needed to not have a state_class configured?
I’m using the sensor.gasmeter in the energy dashboard. the template sensor doesnt even show up inside the energy configuration / gas selection. I think it must have been a leftover from testing.