I know this will sound a stupid question (most of mine are)…
But is it possible to perform actions within the value_template section of a sensor?
I have a sensor that occasionally returns no value, when it does I would like to use the last proper value it recorded. So I was thinking of building some logic/function into my sensor that says if it returns a valid value then store this as an input_text and present the current returned value to HA; or if the sensor value is empty then get the sensor to use the input_text value.
I know I could do this via a seperate automation and change my main automation to use the value from the input_text entity instead of the sensor value, but I was just curious if I could do a neat/one-less automation solution and build it all into the sensor?
It is the file sensor, it is parsing the log file looking for a specific string/line and then extracting sections of that line. If it does not match the string/line then it returns nothing
- platform: file
name: Temperature Sensor update time
file_path: /config/home-assistant.log
value_template: >-
{%- if ('homeassistant.components.konnected.handlers' in value) and ('Temperature Sensor' in value) -%}
{{ value.split(' ')[0] }}, {{ value.split(' ')[1] }}
{%- endif -%}
If the last line of the log file does not contain the required strings it returns nothing. I could change the value_template and include an else clause.