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:
home-assistant:dev
← home-assistant:knx-expose-value-template
opened 02:00PM - 19 May 24 UTC
## Proposed change
<!--
Describe the big picture of your changes here to com… municate to the
maintainers why we should accept this pull request. If it fixes a bug
or resolves a feature request, be sure to link to that issue in the
additional information section.
-->
Add value_template option to KNX expose.
Some typical KNX value ranges don't match with HAs. Eg. a closed cover in KNX is 100% whereas in HA it is 0% or volume (media player) in KNX is `0..100`% whereas in HA it is `0..1` (float). With `value_template` a user can easily bring HA entity states to KNX.
Example configuration:
```yaml
knx:
expose:
- type: percent
address: "1/1/1"
entity_id: cover.office
attribute: current_position
value_template: "{{ 100 - value }}"
- type: percent
address: "2/2/2"
entity_id: media_player.kitchen
attribute: volume_level
value_template: "{{ value * 100 }}"
```
## Type of change
<!--
What type of change does your PR introduce to Home Assistant?
NOTE: Please, check only 1! box!
If your PR requires multiple boxes to be checked, you'll most likely need to
split it into multiple PRs. This makes things easier and faster to code review.
-->
- [ ] Dependency upgrade
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New integration (thank you!)
- [x] New feature (which adds functionality to an existing integration)
- [ ] Deprecation (breaking change to happen in the future)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [ ] Code quality improvements to existing code or addition of tests
## Additional information
<!--
Details are important, and help maintainers processing your PR.
Please be sure to fill out additional details, if applicable.
-->
- This PR fixes or closes issue: fixes #
- This PR is related to issue:
- Link to documentation pull request: https://github.com/home-assistant/home-assistant.io/pull/32841
## Checklist
<!--
Put an `x` in the boxes that apply. You can also fill these out after
creating the PR. If you're unsure about any of them, don't hesitate to ask.
We're here to help! This is simply a reminder of what we are going to look
for before merging your code.
-->
- [x] The code change is tested and works locally.
- [x] Local tests pass. **Your PR cannot be merged unless tests pass**
- [x] There is no commented out code in this PR.
- [x] I have followed the [development checklist][dev-checklist]
- [x] I have followed the [perfect PR recommendations][perfect-pr]
- [x] The code has been formatted using Ruff (`ruff format homeassistant tests`)
- [x] Tests have been added to verify that the new code works.
If user exposed functionality or configuration variables are added/changed:
- [x] Documentation added/updated for [www.home-assistant.io][docs-repository]
If the code communicates with devices, web services, or third-party tools:
- [ ] The [manifest file][manifest-docs] has all fields filled out correctly.
Updated and included derived files by running: `python3 -m script.hassfest`.
- [ ] New or updated dependencies have been added to `requirements_all.txt`.
Updated by running `python3 -m script.gen_requirements_all`.
- [ ] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
- [ ] Untested files have been added to `.coveragerc`.
<!--
This project is very active and we have a high turnover of pull requests.
Unfortunately, the number of incoming pull requests is higher than what our
reviewers can review and merge so there is a long backlog of pull requests
waiting for review. You can help here!
By reviewing another pull request, you will help raise the code quality of
that pull request and the final review will be faster. This way the general
pace of pull request reviews will go up and your wait time will go down.
When picking a pull request to review, try to choose one that hasn't yet
been reviewed.
Thanks for helping out!
-->
To help with the load of incoming pull requests:
- [ ] I have reviewed two other [open pull requests][prs] in this repository.
[prs]: https://github.com/home-assistant/core/pulls?q=is%3Aopen+is%3Apr+-author%3A%40me+-draft%3Atrue+-label%3Awaiting-for-upstream+sort%3Acreated-desc+review%3Anone+-status%3Afailure
<!--
Thank you for contributing <3
Below, some useful links you could explore:
-->
[dev-checklist]: https://developers.home-assistant.io/docs/development_checklist/
[manifest-docs]: https://developers.home-assistant.io/docs/creating_integration_manifest/
[quality-scale]: https://developers.home-assistant.io/docs/integration_quality_scale_index/
[docs-repository]: https://github.com/home-assistant/home-assistant.io
[perfect-pr]: https://developers.home-assistant.io/docs/review-process/#creating-the-perfect-pr
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.
farmio
(Matthias Alphart)
August 24, 2024, 10:11pm
2
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.
farmio:
{{ value * 0.001 }}
Thank you,
it works now with:
value_template: “{{ value | float * 0.001 }}”
farmio
(Matthias Alphart)
August 25, 2024, 10:13pm
4
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 🙃