Water level sensor no litres

I have tried to show volume or litres but just get unavailable also errors when trying to use gauge. Dashboard card shows adc and median voltages. Using a Shelly-Uni and is showing adc voltage. Sensor 0-10Volts. Yaml shows no errors. Is there errors in the yaml. Code as follows

- platform: template
    sensors:
      tank_one_litres_shelly:
        value_template: > 
          state:"{% set voltage = states('sensor.tank_one_level_median_voltage')|float %}"
          "{{ ((voltage / 10.0) * 1023)|round(0) }}"
        unit_of_measurement: L
        unique_id: tank_one_litres 
 
  - platform: statistics
    name: "tank one level median voltage"
    entity_id: sensor.tank_one_adc
    state_characteristic: median
    max_age:
      hours: 2
    precision: 2type or paste code here

You’ve combined legacy (value_template) and modern (state) syntax. Here’s what it should look like as legacy config:

      value_template: > 
          {% set voltage = states('sensor.tank_one_level_median_voltage')|float %}
          {{ ((voltage / 10.0) * 1023)|round(0) }}

Hi Troon many thanks for your advice, this solved my problem. I include this yaml config, would this be the modern way for this yaml config?

template:
    - sensor:
      - name:  "Tank One"  
        unique_id: tank_one_litres
        unit_of_measurement: L 
        device_class: volume
        state_class: measurement
        state: >
          {% if states('sensor.tank_one_adc') | float %}
            0
          {% else %} 
            {{ states('sensor.tank_one_adc') }}  
          {% endif %}
        availability: "{{ has_value_('sensor.tank_one_adc') }}"   
      - platform: statistics
        name: "tank one water median voltage"
        entity_id: sensor.tank_one_water_level_adc
        state_characteristic: median
        max_age:
          hours: 2
        precision: 2

I spot one error: has_value(...) not has_value_(...); and your state template is valid but bizarre.

As it stands, it’ll return zero if the sensor has any numeric value that isn’t zero; otherwise it’ll return the sensor value directly.

In other words, you’ll get zero or the error values unavailable / unknown.

What do you want it to do with the tank_one_adc value?

Also, your statistics sensor needs to move back under the sensor: heading in your config, not under your new template: heading.

Hi Troon again many thanks for your time and expertise. In anwser to your question “What do you want to do with the tank_one_adc value” I want a litres output. However I am getting a litres output from the original yaml. I have two tanks both outputting tank volume in litres. Now trying to put a custom canvas card gauge onto dashboard via HACS. Thank you.

Let’s start from the beginning. You have a Shelly ADC giving “raw” and median output voltages. How do you want to turn that into litres?

Find a template that works in the template editor, perhaps like the one in your first post:

{% set voltage = states('sensor.tank_one_level_median_voltage')|float %}
{{ ((voltage / 10.0) * 1023)|round(0) }}

Then put that in the state: of a modern-format template sensor, like this:

template:
    - sensor:
      - name:  "Tank One"  
        unit_of_measurement: L 
        device_class: volume_storage
        state: >
          [YOUR TEMPLATE HERE]

Hi Troon
Thanks for your last post, I will do what you suggested, and get back to you with the results.

Hi Troon
I put the revised code into Developers template all ok and returned number of liters. Put the code into config yaml got the following error (duplicate mapping key, refers to the line unit_of_ measurement). Developer Tools returns error (Invalid config for temlpate) . On a totally different subject can you explain why when I downloaded from HACS custom canvas card I get the error in Developer Tools (Intergration error intergration’cards’ not found, intergration ‘type’ not found).

Do you have two top-level template: headings perchance? If that’s not it, please post your configuration.yaml here.

That’s a new topic then please :slight_smile:

Hi Troon
I disable the original tank one ymal code when I try the modern tank one yaml code.

# Water Tanks    
    
  - platform: template
    sensors:
      tank_one_litres_shelly:
        value_template: > 
          state:"{% set voltage = states('sensor.tank_one_level_median_voltage')|float %}"
          "{{ ((voltage / 10.0) * 1000)|round(0) }}"
        unit_of_measurement: L
        unique_id: tank_one_litres 
 
  - platform: statistics
    name: "tank one level median voltage"
    entity_id: sensor.tank_one_adc
    state_characteristic: median
    max_age:
      hours: 2
    precision: 2
 
    
  - platform: template
    sensors:
      tank_two_litres_shelly:
        value_template: > 
          state:"{% set voltage = states('sensor.tank_two_level_median_voltage')|float %}"
          "{{ ((voltage / 10.0) * 900)|round(0) }}"
        unit_of_measurement: L
        unique_id: tank_two_litres 
 
  - platform: statistics
    name: "tank two level median voltage"
    entity_id: sensor.tank_two_adc
    state_characteristic: median
    max_age:
      hours: 2
    precision: 2
 
 
 #  Modern yaml
 
    template:
    - sensor:
      - name:  "Tank One"  
        unit_of_measurement: L 
        device_class: volume_storage
        state: >
          {% set voltage = states('sensor.tank_one_level_median_voltage') | float %}
          {{ ((voltage / 10.0) * 1000) | round (0) }}
        unit_of_measurement: litres
        friendly_name: Tank One Litres
  - platform: statistics
    name: "tank one level median voltage"
    entity_id: sensor.tank_one_adc
    state_characteristic: median
    max_age:
      hours: 2
    precision: 2

Your template: needs to be in the first, left-most column — same (non-)indentation as the sensor: that you hopefully have before the config you posted.
Then you need to move your statistics sensor config up to sit under sensor: not under template:.

Your upper two legacy-format template sensors (tank_one_litres_shelly and tank_two_litres_shelly) are still wrong and need fixing or removing.

Hi Troon
I tried what you suggested with regard to the change of indentation and (tank_one_litres_shelly and tank_two_litres__shelly) and returns errors in
Developer Tools. I do not understand what you mean by Then you need to move your statistics sensor config up to sit under sensor: not under template: . The following is my config ymal in full.

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
mobile_app:
lovelace:
  mode: yaml
  resources:
    - url:  /local/custom_cards/canvas-gauge-card.js
      type: module 


# Hot Water Tank


sensor:
  - platform: template
    sensors:
      temperature_difference:
#        name: "Temperature Difference"
        unit_of_measurement: '°C'
        value_template: >
          {{ states('sensor.panel')|float - states('sensor.tank')|float }}
          
  
# Water Tanks    
 
 
  - platform: template
    sensors:
      tank_one_litres_shelly:
        value_template: > 
          {% set voltage = states('sensor.tank_one_level_median_voltage')|float %}
          {{ ((voltage / 10.0) * 1000)|round(0) }}
        unit_of_measurement: L
        unique_id: tank_one_litres 
 
#template:
#  sensors:
#  - name:  "Tank One"  
#    unit_of_measurement: L 
#    device_class: volume_storage
#    state: >
#      {% set voltage = states('sensor.tank_one_level_median_voltage') | float %}
#      {{ ((voltage / 10.0) * 1000) | round (0) }}
#    unit_of_measurement: L
#    friendly_name: Tank One Litres
 
  - platform: statistics
    name: "tank one level median voltage"
    entity_id: sensor.tank_one_adc
    state_characteristic: median
    max_age:
      hours: 2
    precision: 2
   
   

  - platform: template
    sensors:
      tank_two_litres_shelly:
        value_template: > 
          {% set voltage = states('sensor.tank_two_level_median_voltage')|float %}
          {{ ((voltage / 10.0) * 900)|round(0) }}
        unit_of_measurement: L
        unique_id: tank_two_litres 
 
  - platform: statistics
    name: "tank two level median voltage"
    entity_id: sensor.tank_two_adc
    state_characteristic: median
    max_age:
      hours: 2
    precision: 2
    
 
#type: horizontal-stack
#cards:
#  - type: custom:canvas-gauge-card
#    entity: sensor.tank_one_median_voltage
#    card_height: 210
#    background_color: '#FFF'
#    gauge:
#      type: radial-gauge
#      borderShadowWidth: 0
#      borderOuterWidth: 0
#      borderMiddleWidth: 0
#      borderInnerWidth: 0
#      width: 220
#      height: 220
#      minValue: 0
#      maxValue: 1000
#      startAngle: 90
#      ticksAngle: 180
#      valueBox: false
#      majorTicks:
#        - '0'
#        - '100'
#        - '200'
#        - '300'
#        - '400'
#        - '500'
#        - '600'
#        - '700'
#        - '800'
#        - '900'
#        - '1000'
#      minorTicks: 2
#      strokeTicks: true
#      highlights:
#        - from: 800
#          to: 1000
#          color: rgba(200, 50, 50, .75)
#      colorPlate: '#ddd'
#      borders: false
#      needleType: arrow
#      needleWidth: 2
#      needleCircleSize: 7
#      needleCircleOuter: true
#      needleCircleInner: false
#      animationDuration: 1500
#      animationRule: linear

Like this:

sensor:
  [ things that belong under "sensor"
    like legacy template sensors and
    statistics sensors ]

template:
  [ modern format template sensors]

Here’s the relevant bit of your config, re-written (obviously, without any sort of checking or testing as this is just a forum post and not my system). Notes:

  • no friendly_name in modern format config: always check against the docs.
  • Look at the start of the template: section: it’s template: then a YAML list of sensors (and others if needed), not the legacy-format sensors:.
  • You had two unit_of_measurement: lines in your commented-out modern-format template sensor, which was the cause of that error.
  • You also had a tab character in name: "Tank One" — don’t do that
  • I’ve added a random unique_id to your template sensor: it’s good to have one but it doesn’t need to mean anything. I use this to generate them.
sensor:
  - platform: template
    sensors:
      temperature_difference:
        unit_of_measurement: '°C'
        value_template: >
          {{ states('sensor.panel')|float - states('sensor.tank')|float }}
  
  - platform: template
    sensors:
      tank_one_litres_shelly:
        value_template: > 
          {% set voltage = states('sensor.tank_one_level_median_voltage')|float %}
          {{ ((voltage / 10.0) * 1000)|round(0) }}
        unit_of_measurement: L
        unique_id: tank_one_litres 
 
  - platform: template
    sensors:
      tank_two_litres_shelly:
        value_template: > 
          {% set voltage = states('sensor.tank_two_level_median_voltage')|float %}
          {{ ((voltage / 10.0) * 900)|round(0) }}
        unit_of_measurement: L
        unique_id: tank_two_litres 

  - platform: statistics
    name: "tank two level median voltage"
    entity_id: sensor.tank_two_adc
    state_characteristic: median
    max_age:
      hours: 2
    precision: 2

  - platform: statistics
    name: "tank one level median voltage"
    entity_id: sensor.tank_one_adc
    state_characteristic: median
    max_age:
      hours: 2
    precision: 2

template:
  - sensor:
      - name: "Tank One litres"  
        unit_of_measurement: L
        device_class: volume_storage
        unique_id: f755d953-3744-4c5b-9d7c-ee1542e44b8a
        state: >
          {% set voltage = states('sensor.tank_one_level_median_voltage') | float %}
          {{ ((voltage / 10.0) * 1000) | round (0) }}   

Hi Troon
All working good now. Many thanks for you time and effort.