KNX expose with value template only updates once

Hi,

Im exposing the power production of my inverter on the knx bus in order to display it on the knx displays.

this works but when i try to use the new value template support:

it is only updateing once.

My Config is like this:

  • type: “power_2byte”
    entity_id: “sensor.power_meter_active_power”
    address: “5/2/9”
    value_template: “{{ states(‘sensor.power_meter_active_power’) | float * 0.001 }}”

Is this a bug or do i do something wrong? Im using the latest homeassistant version.

It should update whenever the source entity state changes. If it does not, have a look at the logs if there is any error listed.

The idea of these value_templates is to have the value injected for use. So it would be just

value_template: “{{ value * 0.001 }}”

no need to extract the target entity state again.

Thank you,
it works now with:
value_template: “{{ value | float * 0.001 }}”

So I finally understand why this happens - and will leave that for future persons (including myself when I have forgotten again) wondering.

An expose is triggered on a state change. There is logic that prevents sending the same value multiple times by comparing the previous state with the new state. The state (or attribute if configured) is extracted by a function that is called twice - once with the new state object, once with the old one. It returns the state / attribute as number or string (depending on type DPT) and applies default if applicable.

When you use value_template, that function doesn’t return the state directly, but passes it to the template evaluation engine (value) and returns its result.
Now when you use {{ value | float * 0.001 }} the first run will be using the new state and the second run will be using the old state. If their results are different, it will send to KNX.
Example:

# {{ value | float * 0.001 }}
new_state = 3000
old_state = 4000
new_value = {{ 3000 * 0.001 }} -> 3  # value = 3000
old_value = {{ 4000 * 0.001 }} -> 4  # value = 4000

When using {{ states(‘sensor.power_meter_active_power’) | float * 0.001 }} the state is passed to the template engine, but since value is not used, it is ignored - the state is evaluated directly by the template engine (states()) and that results in the current state - always - returning the same values, not sending anything to KNX since it prevents sending the same value multiple times.
Example:

# {{ states(‘sensor.power_meter_active_power’) | float * 0.001 }}
new_state = 3000
old_state = 4000
# `states(‘sensor.power_meter_active_power’)` resolves to 3000 at this point in time - it is the current state
new_value = {{ 3000 * 0.001 }} -> 3
old_value = {{ 3000 * 0.001 }} -> 3  # `value` is available, but not used

So here we are - now even I'm not sure if this is a bug or a feature 🙃