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

Drew,
Many thanks champ - that works a treat.
Pat

1 Like

Didge
Something weird is now happening. I receive the retained value no worries.
This retained value then updates the sensor.top_tank_volume.
However the automation I have in place is not starting when expected.
In config.yaml I have

sensor:
  - platform: template
    sensors:
      top_tank_volume:
        value_template: >
          {% set voltage = states('sensor.retained_sensor_value') | float %}
          {{ ((voltage / 4.3) * 20000) | round (0) }}
        unit_of_measurement: litres
        friendly_name: top tank volume

and

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 }}"

My automation then has this yaml coding

alias: fill water tank
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.top_tank_volume
    below: 11000
condition:
  - condition: time
    after: "10:00:00"
    before: "15:00:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.sonoff_1000c029a7
  - service: notify.mobile_app_pixel_6_pro
    data:
      message: The tank pump has turned on
  - delay:
      hours: 3
      minutes: 0
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.sonoff_1000c029a7
  - service: notify.mobile_app_pixel_6_pro
    data:
      message: The tank pump is now off
mode: single

I’m confused. The sensor.top_tank_volume is below the threshold of 11000 but the automation doesn’t start.
When I push run in automation it will start - even though I’m outside the hours i want it to run?

Pat

If it is below the threshold then it won’t trigger. It triggers when the value crosses the threshold.

image

Running the automation manually skips the conditions.

Add another couple of triggers to fire on restart / reload and a condition to check the value is below the threshold for when it’s these triggers that fire:

trigger:
  - YOUR EXISTING TRIGGERS
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded
condition:
  - YOUR EXISTING CONDITIONS
  - condition: numeric_state
    entity_id: sensor.top_tank_volume
    below: 11000

You may also want a trigger at 10:00. At the moment, if the value drops below 11000 at 09:30, it won’t run the action either.

Note that if you restart HA during the three hour runtime, the pump will never switch off. I would implement this by moving the pump-off logic to a new automation, that triggers when the pump has been on for three hours.

Troon
thanks for that
The value that is being reported has some fluctuations - even though I have used median values over a time range. The sensor is a shelly uni matched with a voltage water pressure sensor.
So I guess I want the automation to see what the value reported for this sensor is and if below 11000 and the time is between 10 and 3 then turn the pump on.
It worked for a couple of cycles until I got help with didge on the retained value.
The sensor is likely to report this value below 11000 in the hours after 1500. So I would want it to recognise this the following day and start the pump at 1000
Pat

My modifications above would do that. As an alternative, use a state trigger which will fire any time the value changes, then use conditions like this:

trigger:
  - platform: state
    entity_id: sensor.top_tank_volume
condition:
  - condition: numeric_state
    entity_id: sensor.top_tank_volume
    below: 11000
  - condition: time
    after: "10:00:00"
    before: "15:00:00"
  - condition: state
    entity_id: switch.sonoff_1000c029a
    state: "off"

Troon
thanks for your help.
have modified my code and will wait to see what 10.00 am tomorrow brings
Thanks
Pat

traces indicate possible success. fingers crossed

Went past 10am this morning and automation wasn’t triggered. Sensor … Volume was at about 8700. Below the threshold of 11000
I know it’s something simple but I can’t see it.
Pat

Paste in the full YAML for the automation that “wasn’t triggered”; and a screenshot of Developer Tools / States showing your sensor.

Troon
thanks heaps…
Automation code

alias: fill water tank
description: automatically fill the top water tank when level below 11000
trigger:
  - platform: state
    entity_id: sensor.top_tank_volume
condition:
  - condition: numeric_state
    entity_id: sensor.top_tank_volume
    below: 11000
  - condition: time
    after: "10:00:00"
    before: "15:00:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.sonoff_1000c029a7
  - service: notify.mobile_app_pixel_6_pro
    data:
      message: The tank pump has turned on
  - delay:
      hours: 3
      minutes: 0
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.sonoff_1000c029a7
  - service: notify.mobile_app_pixel_6_pro
    data:
      message: The tank pump is now off
mode: single

And here is the screenshot of the state.

Pat

That automation will trigger whenever the sensor value changes, which it hasn’t done since 07:09. It has no trigger for 10:00. As soon as the water level changes it should run.

If you want it to run at 10:00 (as I suggested above), add a trigger to do so:

trigger:
  - platform: state
    entity_id: sensor.top_tank_volume
  - platform: time
    at: "10:00:00"