homeassistant-attributes icon indicating copy to clipboard operation
homeassistant-attributes copied to clipboard

Objects available for the value_template not listed.

Open gribouk opened this issue 4 years ago • 7 comments

Hello!

This is a request for the information. We all know that templating is simple, fast and fun way to achieve fantastic results, but your docs lack the list of objects accessible from the value_template section, namelly how to retrive the entity's attribute's value to process it, what is the name of that variable (attr/attribute/value...)?

Example: I have an couple of entities with an attribute's name A. How to address this attribute's value in the value_template (I belive there should be a variable to store the value, shouldn't it?)

Tiny example of value_template would explain it all...

Thanks!

gribouk avatar Sep 16 '21 15:09 gribouk

Hi yes the documentation doesn't cover it, i will fix it. Aynyway here is the answer https://github.com/pilotak/homeassistant-attributes/issues/23

pilotak avatar Sep 17 '21 07:09 pilotak

Thanks for reply!

Did not expect such syntax... I need to apply 'split' procedure to a string, then take n-th element out of the array/list, how would I do that?

To be specific: I have an attribute Val in a form: val_float#write_timestamp To show on the frontend I need value only...

gribouk avatar Sep 17 '21 07:09 gribouk

you could try .split("#")[0] | float

here is test

{% set test = "1.2345#write_timestamp" %}

{{ test.split("#")[0] | float  }}

I don't have such an entity to try out, you could try to turn on the debug and see the result template and play with it in HA dev tools

pilotak avatar Sep 17 '21 08:09 pilotak

Thanks for the reply!

There is a confusion. The question was not how to use split, since already use it in my templates, but how to apply split in your value_template section, if there is no name of the variable which stores the attribute value? Just follow your example: test.split... What name should I use in the value_template instead of 'test'? Val/attr/attr_val...? In the sample you've referenced, the syntax was: '* 100' for the multiplication... What is the name of the variable * operand is applied to? This was the question. Thanks!

gribouk avatar Sep 17 '21 08:09 gribouk

I understand what you ask for. All what value_template does is appending it just after extracted value, there is no exposed variable name. If following setting doesn't work, than this is not possible with this component

value_template: ".split("#")[0] | float"

pilotak avatar Sep 17 '21 08:09 pilotak

Unfortunatelly, it is not valid: Could not attribute sensor for variable.engineerineg_irrigation_zone_test_5: TemplateSyntaxError: expected token 'end of print statement', got '.'

Thus, I guess, it shiuld transform into feature request at this stage.

Motivation: Though it can be emplemented using standard template sensor integration, your component has vital feature - batch declaration from the list of entities of similar nature, which drastically decreases the copy-paste size of a code (when vlue template is a complex script of ~20-30 lines or more it does metter), along with mistakes probability.

Thus, having a variable, which stores a string value of the attribute would be usefull to unleash the full scale access to templating.

gribouk avatar Sep 17 '21 10:09 gribouk

Rather then making a future request, you could propose a code via PR. I'm busy at the moment so this will park at the bottom of my todo list unless you write and test the code in which case I will review it asap.

pilotak avatar Sep 17 '21 11:09 pilotak