Perform an action in a value template of a sensor

Hi all,

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?

Thanks,

J.

10 characters answer: No :wink:

Great answer :rofl: :rofl: :rofl:

Didn’t think it was possible. Thanks for confirming :+1:

This may be possible though. What do you mean by this:

unknown?

none?

0?

Hi,

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

And it does not retain it’s previous value when that happens?

It returns an empty string?

Show your file sensor config.

Hi,

This is the config.

- 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.

Please format your pasted config.

Try this:

- 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] }}
    {% else %}
      {{ states('sensor.temperature_sensor_update_time') }}
    {% endif %}

The else case should retain the previous/current state.

I’ve reformatted my code.

Thanks for the suggestion/solution, I’ll give it a try and hopefully it should work :crossed_fingers: :+1: