Yaml coding help

Troon
so what i’m hearing is that I need some form of code that keeps monitoring the ‘actual’ value and when that value falls below my threshold continue on with the automation.
How do I write that as a “less than” argument rather than waiting for a state change?
I’m struggling to know where it went wrong as the automation i had was successfully triggering about two months back, but i was seeing the issue with the sensor value reading unknown (solar power issues) hence went the retained value sensor route which provides a 24 hour reading to see water level - but I can’t get this back to the automation.
I am no coder sorry.
Pat

Troon
I have inserted the code you suggested into the automation as follows (abbreviated automation as no changes other than the trigger section). Isd this right?

alias: fill water tank
description: automatically fill the top water tank when level below 11000
trigger:
  - platform: state
    entity_id: sensor.top_tank_volume
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded
condition:
  - condition: numeric_state
    entity_id: sensor.top_tank_volume
    below: 11000
  - condition: time
    after: "10:00:00"
    before: "15:00:00"

Pat

You’ve completely ignored what I said in my last post, despite saying “what I’m hearing”. I understand you see yourself as “not a coder” but at least read what I wrote.

Let me be clear:

YOU NEED TO ADD A TRIGGER THAT FIRES AT 10:00.

If you don’t, it will only trigger when the sensor value changes and then only run if the value if below 11000 and the time is in the valid range.

If the sensor doesn’t change, the automation will not trigger. That’s why you need an additional trigger at 10:00.

Your screenshot shows the sensor not changing for almost three hours (07:09 last updated versus your post suggesting it’s after 10:00 in your time zone).

So:

alias: fill water tank
description: automatically fill the top water tank when level below 11000
trigger:
  - platform: state
    entity_id: sensor.top_tank_volume
  - platform: time
    at: "10:00:00"
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded
condition:
  - condition: numeric_state
    entity_id: sensor.top_tank_volume
    below: 11000
  - condition: time
    after: "10:00:00"
    before: "15:00:00"

Troon
I do appreciate your advice and apologies if my typing indicated otherwise.
As a non coder I see your example and correct me if I’m wrong the flow should be…

My intiial trigger is time
Then at 10.00am the automation checks to see if my sensor is reporting the value within the threshold I’m looking for.
Then the automation continues.
… turn on switch
… notify

etc
Pat

In my example, there are several triggers:

  • it’s 10:00
  • the sensor value changes
  • HA has been restarted
  • automations have been reloaded

Any of these will trigger the automation, which will then check the conditions:

  • sensor below 11000
  • time between 10:00 and 15:00
  • (switch is off: this was in an earlier post of mine and is optional, to avoid sending an unnecessary “turn on” signal to a switch that is already on)

If those conditions all pass, the action is run.

You need the time trigger to account for a low but not changing sensor value (like you had this morning); and you need the sensor state trigger in case the level goes low at say 10:05.

Troon
I guess I’ll see what tomorrow brings at 10.00am.
If water starts flowing I will gladly shout you a beer.
Here is the automation yaml I have.

alias: fill water tank
description: automatically fill the top water tank when level below 11000
trigger:
  - platform: state
    entity_id: sensor.top_tank_volume
  - platform: time
    at: "10:00:00"
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded
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

Pat

That should do it.

As I warned before: if you restart HA whilst the pump is running (in the 3-hour delay), it won’t turn off.

Troon
thanks for the heads up on that. Would prefer to not have the pump going for more than 3 hours. That time provides a safety barrier so I don’t overfill. If it goes for longer then not only do I overfill the tank but I empty the feeder tanks.
I don’t want to overstay my advice corner but is there a way to ensure that if this automation is started then another monitor is watching to ensure that the pump is turned off after 3 hours even if a restart happened?
Or should I just delete the home assistant restart trigger to avoid this?
Pat

It’s not the restart trigger causing it. If you restart when the automation is in the middle of its three-hour delay, it’ll forget what it was doing. As per my original post advice:

You can make this via the UI, but in YAML:

trigger:
  - platform: state
    entity_id: switch.sonoff_1000c029a7
    to: 'on'
    for: "03:00:00"
  - platform: homeassistant
    event: start
condition:
  - condition: numeric_state
    entity_id: sensor.top_tank_volume
    above: 11000
action:
  - service: switch.turn_on
    entity_id: switch.sonoff_1000c029a7

In plain English: when the switch has been ON for three hours, or if you restart HA, turn it off if the level is now above 11000.

It doesn’t matter if the pump hasn’t finished filling up if you restart HA and turn it off, as the original automation will turn it back on again.

Or, something that will survive restarts, without caring about homeassistant starting or not.

Just need to make an input_datetime helper that has a date and time, with the name Tank Pump Off

alias: fill water tank
description: automatically fill the top water tank when level below 11000
trigger:
  - platform: numeric_state
    entity_id: sensor.top_tank_volume
    below: 11000
    id: 'on'
  - platform: time
    at: "10:00:00"
    id: 'on'
  - platform: time
    at: input_datetime.tank_pump_off
    id: 'off'
condition: []
action:
  - choose:
    - conditions:
      - condition: trigger
        id: 'on'
      - 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
      sequence:
       - 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
       - service: input_datetime.set_datetime
         target:
           entity_id: input_datetime.tank_pump_off
         data:
           datetime: "{{ now() + timedelta(hours=3) }}"

    - conditions:
      - condition: trigger
        id: 'off'
      sequence:
      - service: switch.turn_off
        target:
          entity_id: switch.sonoff_1000c029a7
      - service: notify.mobile_app_pixel_6_pro
        data:
          message: The tank pump is now off
mode: single
1 Like

Nice, although still has an edge condition if HA is in the middle of being restarted at pump-off time…

Just depends how bulletproof you want the automation to be, I guess.

1 Like

Really appreciate all the help.
If there is still a chance that a restart may somehow allow the pump to continue - would I be better to have another small script “listening” for when the tank volume gets to say 20000 - then turns the pump off?
This will allow any restart process - which in my case a full restart takes about 1-2 minutes to still have the abililty to listen for such event and still give me margin for error (22500 is tank full).
Pat

If it’s super-important, you should have a float switch that cuts the power to the pump, independent of any software.

Yep that is part of the plan. Automation worked without issue today. Thanks for your time.
Pat

1 Like

Hi
Can you comment this code please?
((voltage / 4.3) * 20000) - 20000=capacity of tank? 4.3=?
median sensor?
Thanx

Kastro, I’m away for a few weeks. I tried to use my mobile to copy the code I have without success. I found great help in another forum topic here ESPHome water level sensor - #259 by rusty_away

Pat

1 Like