Yaml coding help

Hi,
Just after some assistance with my obvious loack of coding knowledge.
I have a shelly uni connected to a water level sensor. This is all run by a 12v car battery that is toppped up with a solar panel.
The device during cloudy days sucks the battery too low and then powers off making the shelly uni become unavailable as a sensor.
I had thought that I could create a ‘dummy’ retained sensor in my config and then have an automation running that would keep reading valid outputs and keep a retained value. The goal is to to use this dummy sensor to ensure that I can clearly display the last known value on a dashboard. The following is the two bits of yaml I have.
In my config is the ‘dummy’ sensor.

- platform: template
    sensors:
      retained_sensor_value:
        value_template: "{{ states('sensor.top_tank_water_level_adc') }}"
        friendly_name: "Retained top tank voltage"

In my automations I have built this

alias: update water tank sensor
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.top_tank_water_level_adc
condition:
  - condition: template
    value_template: "{{ is_number(state_attr('sensor.top_tank_water_level_adc', 'state')) }}"
action:
  - service: homeassistant.update_entity
    data: {}
    target:
      entity_id: sensor.retained_sensor_value
mode: single

I’m obviously doing something wrong because the ‘dummy’ sensor doesn’t retain the last valid value.
Pat

Your automation is ok but your retained sensor is the issue. That sensor will keep updating its value when the top_tank_water_level_adc updates due to the value_template and as such, effectively be identical to the water level adc sensor.

I would use a number helper entity and change your automation to set that to the valid tank water level values as this will only then update from your automation.

You’ve got a couple issues, likely stemming from a misunderstanding of how template sensors work. First, there is no need for an automation to update a template sensor, a state-based template sensor like yours updates every time the entities referenced in the template update. Your automation really isn’t doing anything.

To get a what you want, you would need to switch to a trigger-based template sensor.

template:
  - trigger:
      - platform: state
        entity_id: sensor.top_tank_water_level_adc
        not_to:
          - unknown 
          - unavailable 
    sensor:
      - name: Retained Sensor Value
        state: "{{ trigger.to_state.state }}"

Drew and Mark,
thanks for the replies - yes I have a confused knowledge.

This code is in my config file and works - when the sun is shining.

sensor:
  - platform: template
    sensors:
      top_tank_volume:
        value_template: >
          {% set voltage = states('sensor.top_tank_water_median_voltage') | float %}
          {{ ((voltage / 4.3) * 20000) | round (0) }}
        unit_of_measurement: litres
        friendly_name: Top Tank Litres
  - platform: statistics
    name: "top tank water median voltage"
    entity_id: sensor.top_tank_water_level_adc
    state_characteristic: median
    max_age:
      hours: 2
    precision: 2

  - platform: template
    sensors:
      template_toptank_example:
        value_template: "{{ states('sensor.top_tank_volume') }}"

Drew - does your code go in here or do I make a new template in developer tools?

It can go in your configuration.yaml file, but you may want to split off a file for your template sensors.

Drew
just tried to insert it and get a bad indentation of a sequence error - using studio code server

There was an extraneous : in the name, I have removed it above. When you paste it into your configuration make sure template is all the way to the left. Sometimes in Studio Code the top line of a paste will get thrown off.

Drew
no errors thrown now thanks. Just restarting and I will check tomorrow. I usually have the source unavailable from about 3am to 8am. Battery doesn’t get through the night.
Appreciate your help

Drew,
I might have to interrupt my battery to check now. Because we are now receiving good amounts of sun at the appropriate angle my battery is now going the full 24 hours.
Pat