Add knx gas to energy meter

Hello,

I use another knx reading to get the total gas consumption back from a KNX reading.

- name: Gas
  state_address: '4/6/15'
  type: '4byte_float'
  state_class: 'total_increasing'

and I have following template sensors

    gas_consumption_m3:
      friendly_name: "Total Gas Consumption m3"
      unit_of_measurement: 'm3'
      value_template: "{{ (states('sensor.gas')|float / 100) | round(0) }}"
    gas_consumption:
      friendly_name: "Total Gas Consumption"
      unit_of_measurement: '0,01m3'
      value_template: "{{ states('sensor.gas')|float | round(0) }}"

Any idea how I can integrate this total_increasing value inside the energy meter?

2021-09-20 13_23_49-Developer Tools - Home Assistant

Thanks in advance
Stijn

You can use customize to override device_class or unit of the original sensor. Customizing entities - Home Assistant

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)

What exactly do you want to achieve with that script? I’m pretty sure HA can do this out of the box without any extra Python script.

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)

Thanks in advance
Stijn

I’m almost there: creating a long integer is much simpler then floating calculations :slight_smile:

so I only needed to strip the python code and currently I have the following solution.

The only thing I need to change are the leading zeros to create to 4byte float. (the current gas value is only 2 bytes long now)

#!/usr/bin/python
import struct
import os
import sys

def int_to_hex(f):
    return hex(f)

whex = int_to_hex(int(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 "00 00 " + whex
    sys.exit(0)

Please stop. This is leading nowhere.

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

If you still have troubles post a screenshot of the sensors attributes from HAs developer tools → states

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)

2021-09-21 18_49_50-Settings

I’ll try again and let you know the outcome.

thanks in advance
Stijn

Hi Farmio,

I’ve created a new knx_sensor with again a new name and without the additional customize config and it already works :upside_down_face:

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.

- name: gasmeter
  state_address: '4/6/50'
  type: 'volume'
  state_class: 'total_increasing'

With the config above, HA was able to automatically find the correct unit of measurement

2021-09-21 21_03_32-Developer Tools - Home Assistant

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.

2021-09-21 21_05_16-Configuration - Home Assistant

  • Go here: Open your Home Assistant instance and show your state developer tools.
  • Find them and delete them
  • Restart HA

Your entity still has no device_class. I don’t know about gas, but for energy it has to be set. Maybe for gas too.

I can confirm that it works without the “device_class: gas” in my case but i’ve added it anyway so the UI icon changed to a beautiful gas cylinder.

2021-09-21 21_39_08-Developer Tools - Home Assistant

Deleting the old entities is no problem but they still show up (even after an HA restart) inside the energy management

When configuring Gas consumption, how are you able to select m3 as the top level item? Mine does not allow that. It does not list the sensor there.

Also, what does it mean “Use an entity tracking the total costs”? Is this the m3 gas entity or is it some absolute price entitiy?

This is how my config looks like

KNX Sensor

- name: gasmeter
  state_address: '4/6/50'
  type: 'volume'
  state_class: 'total_increasing'

customize.yaml

sensor.gasmeter:
  device_class: gas

sensor.yaml

- platform: template
  sensors:
    gas_consumption_m3:
      friendly_name: "Total Gas Consumption m3"
      unit_of_measurement: 'm³'
      value_template: "{{ states('sensor.gasmeter') }}"
      device_class: "gas"

hopes this helps

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.

thanks for pointing that out

the config doesnt like device_class configured at the knx sensor

→ the state_class is indeed configured at the knx sensor itself (forgot to copy / paste earlier). I have updated the config above

Ah, my bad, I confused it for state_class