How to use value_template with custom component "homeassistant-attributes"

Hi community,

I am new here and to home-assistant and I am trying to wrap my head around the concept of “value_templating”, especially in context of the “homeassistant-attributes” component (https://github.com/pilotak/homeassistant-attributes/)…

What I want to achieve:

  • get sensors for the valve positions of all my thermostats. Those are within the ‘level’ attribute of the thermostat as a number between 0 and 1.
  • Get the value not as a number between 0 and 1 but between 0% and 100%
  • I dont want to create a sensor definition for each one of them. (I know how to do that with the desired outcome, but I would like to do that elegantely with the homeassistant-attributes component.

Problem:
the value_template definition of the component does not produce any number, whatever definition I use:

sensor:  
  - platform: attributes
    friendly_name: "Ventilposition"
    attribute: "level"
    unit_of_measurement: "%"
    **value_template: "{{ (value*100) | float }}"**  
    entities:
      - climate.heizkoerper_arbeitszimmer
      - climate.heizkoerper_bad
      - climate.heizkoerper_kueche
      - climate.heizkoerper_schlafzimmer
      - climate.heizkoerper_wohnzimmer
sensor:  
  - platform: attributes
    friendly_name: "Ventilposition"
    attribute: "level"
    unit_of_measurement: "%"
    **value_template: "{{ (value | float *100) }}"**  
    entities:
      - climate.heizkoerper_arbeitszimmer
      - climate.heizkoerper_bad
      - climate.heizkoerper_kueche
      - climate.heizkoerper_schlafzimmer
      - climate.heizkoerper_wohnzimmer

These are all producing “unknown” as a result. My guess was that I cannot use something like

value_template: "{{ (state_attr(climate.heizkoerper_bad, 'level')| float *100) }}

as this would defeat the purpose of the generic setup of this component, so there should be some kind of generic reference “value” for each entity. Am I correct, or is that supposed to be used in a different way.
The help text is actually not very helpful in this case:

  • value_template ( Optional ): In case you need to do a math with the value ie. offset, bit gain, etc. (will be the same for all entities specified) .

Thanks!

Ben

Hi Benjamin,
have you ultimately found any solution to this? As I seem to have been coming exactly the same road you had went down one year ago…^^
Trying to achieve basically the same, played around with any “generic entity/object state” to refer to in the value_template, but no luck so far.
Any hint would be highly appreciated!
Bests, jan

Sorry for the late reply, but I had to organize and carry out a move between cities in the last weeks.

I indeed found a solution which looked like this:

value_template: "* 100 | int"

Hope this helps.

Ben

1 Like

Ben, thanks a lot - and no worries, those things go first. Wishing you a good start in the new city!

Will try your solution a little later, looks promising (and different enough from what I tried so far to spark new hope).

Update: unfortunately, this doesn’t do the trick for me. My calculation is a bit different from yours (want to translate battery voltage into percentage) - so my template would be something like
(([attribute_value] - 2) / 0.011) | round(0)
To follow the precedence rules, I’ll need the brackets, but this seems to kill the template implementation here…

I think there are 2 problems:

  • attribute_value: keyword is probably value. If you need to access an attribute, probably you need to expose that attribute in a sensor state through a template sensor?
  • ‘/’: I am not sure if the divider symbol works in tempaltes. Maybe try:
    ((value) - 2) | multiply(90.9090) | round(0)