Change KNX Sensor value with calculation

Hi there,

I am new in Homeassistant and migrate all of my scripts from iobroker to homeassistant.

I have a question for recalculate my gas consumption.

I have the consumption in m³ from a (knx) sensor and want to calculate the kWh.

For that, I have created helper entities with “Brennwert” and “Zustandszahl”. I need both, to calculate the kWh

The calculation looks like this:
gas consumption in m³ * brennwert * zustandszahl = kWh

The result do I have at my entity sensor.gas_zahlerstand_kwh in my knx.yaml

Thanks for helping

Update:

okay, I think I have a first solution. Created an automation

alias: Umrechnung Gas Zählerstand
description: Umrechnung Gas Zählerstand m³ in kwh
trigger:
  - platform: state
    entity_id: sensor.gas_zahlerstand_m3
action:
  - service: knx.send
    data:
      address: 7/4/8
      payload: >-
        {{ float(states('sensor.gas_zahlerstand_m3')) *
        float(states("input_number.gas_brennwert")) *
        float(states("input_number.gas_zustandszahl")) }}
      type: active_energy_kwh
mode: single

If there is a better and easier Way, I will happy to inform about. Because I have many thinks of that, I have to migrate from ioBroker to Home Assistant.

Hi :wave:!

If you need that value on the bus, that way seems fine.

If not, the more generic / common way would be to use a state based template sensor (or maybe a helper, not sure if that can be done via UI yet). See Template - Home Assistant
If you decide you want that value on the bus later, you may use knx expose - that way it is even readable (GroupValueRead) for other devices.

Keep in mind that your base entity may have a state “unavailable”, and you don’t want your derived sensor (this counts for both methods) to use a default value for calculation (like 0) because it messes with the statistics calculation. You want your derived entity to be unavailable or keep its previous state until you get valid numeric state from the base entity again.

Oh, and I think a counter value should have state_class: total_increasing, not “total” :thinking:

Thank you for that. I have changed it in my configuration.yaml :slight_smile: