Time remaining template sensor

Hi guys,

I need to create a template sensor (I guess) to show the remaining time for my sprinkler system.
To set the irrigation time I use an input_number helper, but the irrigation time is not the same every day, it depends on other variables like precipitation and previous day highest temperature. Could this be possible?

BR,
Cosmin

Yes, if the end-time is predictable (can be calculated) you can probably template it.

If you share the variables and the logic you want to use, we can help create your sensor.

Hi Cosmin F.,

Maybe you can get some inspiration from what I did.
https://whatarewefixing.today/1514/zigbee-hose-valve-automation-home-assistant/.

Questions, let me know.

Let’s say yesterday highest temp. was 30°C and no rain, today the sprinklers will run for 30 minutes. If yesterday the highest temp. was 25°C and also no rain, the sprinklers would run for 20 min.
That is the logic of the automation and time input.

Ok, that’s quite easy.

Can you share your sensors (max* temp yesterday, rain yesterday etc)?

[Edit: typos*]

Yes I can, but the pump (switch) running time is set by an script wich uses an input_number to set the time delay. So I think this input_number is the reference, right? Or am I wrong? The whole automation is a bit more complex and uses a bunch of template sensors, binary sensors, scripts, helpers. What exactly should I share?

Thanks. I already have the automation working, I only need a sensor to show me the time remaining till the end of the irrigation time. Just like a countdown till the end of the irrigation time.

Why not just use a timer instead

It gives you a visible countdown state and will survive reloads and reboots (as delays won’t).

Share the script and the automation.

I need to input the time for each scenario.

Automation:

alias: “Aspersoare: Verifică dacă trebuie irigat”
description: Verifică dacă trebuie pornită irigarea.
trigger:

  • platform: time
    at: “07:30:00”
    condition:
  • condition: time
    weekday:
    • tue
    • sat
      action:
  • choose:
    • conditions:
      • condition: numeric_state
        entity_id: input_number.aspersoare_temperatura_maxima_masurata
        above: 30
      • condition: numeric_state
        entity_id: sensor.aspersoare_precipitatii_totale
        below: “20.1”
        sequence:
      • data:
        value: 40
        target:
        entity_id: input_number.aspersoare_durata_irigare
        action: input_number.set_value
      • data: {}
        action: script.aspersoare_pornire_irigare
    • conditions:
      • condition: numeric_state
        entity_id: input_number.aspersoare_temperatura_maxima_masurata
        above: 25
        below: 30
      • condition: numeric_state
        entity_id: sensor.aspersoare_precipitatii_totale
        below: “15.1”
        sequence:
      • data:
        value: 25
        target:
        entity_id: input_number.aspersoare_durata_irigare
        action: input_number.set_value
      • data: {}
        action: script.aspersoare_pornire_irigare
    • conditions:
      • condition: numeric_state
        entity_id: input_number.aspersoare_temperatura_maxima_masurata
        above: 20
        below: 25
      • condition: numeric_state
        entity_id: sensor.aspersoare_precipitatii_totale
        below: “10.1”
        sequence:
      • data:
        value: 15
        target:
        entity_id: input_number.aspersoare_durata_irigare
        action: input_number.set_value
      • data: {}
        action: script.aspersoare_pornire_irigare
    • conditions:
      • condition: numeric_state
        entity_id: input_number.aspersoare_temperatura_maxima_masurata
        above: 15
        below: 20
      • condition: numeric_state
        entity_id: sensor.aspersoare_precipitatii_totale
        below: “10.1”
        sequence:
      • data:
        value: 10
        target:
        entity_id: input_number.aspersoare_durata_irigare
        action: input_number.set_value
      • data: {}
        action: script.aspersoare_pornire_irigare
        default:
        mode: single

Script:

alias: “Aspersoare: Pornire irigare”
sequence:

  • data:
    message: >-
    Aspersoarele au pornit pentru {{
    states(“input_number.aspersoare_durata_irigare”) | int }} minute pe
    fiecare zonă.
    action: notify.mobile_app_s_galaxy_s24_ultra
  • type: turn_on
    device_id: e5e7ea5cf32c84f67d7e7bd5aa601544
    entity_id: d75809e12a1a2d8afac75d4eb72c6539
    domain: switch
  • delay:
    hours: 0
    minutes: 0
    seconds: 10
    milliseconds: 0
  • type: turn_on
    device_id: 9cdec95d0d469e873707a0dc0e74550d
    entity_id: 7d6abd6f08e1fc73fa24045c352910c8
    domain: switch
  • delay:
    minutes: |
    {{ states(“input_number.aspersoare_durata_irigare”) | int }}
  • type: turn_off
    device_id: 9cdec95d0d469e873707a0dc0e74550d
    entity_id: 7d6abd6f08e1fc73fa24045c352910c8
    domain: switch
  • type: turn_on
    device_id: 9cdec95d0d469e873707a0dc0e74550d
    entity_id: f9d029e7e4d7d1b5a3297ea148dd48b6
    domain: switch
  • delay:
    minutes: |
    {{ states(“input_number.aspersoare_durata_irigare”) | int }}
  • type: turn_off
    device_id: 9cdec95d0d469e873707a0dc0e74550d
    entity_id: f9d029e7e4d7d1b5a3297ea148dd48b6
    domain: switch
  • type: turn_off
    device_id: e5e7ea5cf32c84f67d7e7bd5aa601544
    entity_id: d75809e12a1a2d8afac75d4eb72c6539
    domain: switch
  • data: {}
    enabled: true
    action: script.aspersoare_reseteaza_valorile
  • data:
    message: Irigare finalizată!
    title: Aspersoare
    action: notify.mobile_app_s_galaxy_s24_ultra
    mode: single
    icon: mdi:sprinkler

And your saying a timer can’t be changed?

I don’t know. Never used it in scripts or automations.

Can you perhaps think very hard and you might figure it out.
A good clue is that I suggested timer.

If you want to cheat then you are allowed to open the documentations and read.

I’ve read the documentation and I will give it a try. I will make a test automation to see how it performs. Thanks.

I use this in a script to set a variable time for my grill timer:

data:
  duration: 00:{{ states('input_number.grill_time') | int }}:00
target:
  entity_id: timer.grill
action: timer.start

Very simplistic, though, because I only need to set it between 1 and 99 minutes - and the card I’m using to show it even ‘converts’ it to h:mm:ss when I set it for more than 59min.

type: custom:timer-bar-card
entities:
  - entity: timer.grill
    name: Time
  - entity: input_select.announce_timer
    name: 'Action:'
bar_direction: rtl
bar_foreground: '#eee'
bar_background: 'var(--mdc-theme-primary, #620088)'
sync_issues: ignore

Here’s how I do it with an input number:

template:
  - sensor:
      - name: "Zone 1 Time Remaining"
        unique_id: 79d11323-4107-40c3-bfb6-2e1dcf65e711
        icon: "mdi:timelapse"
        unit_of_measurement: "min"
        state: >
          {% if is_state('switch.zone_1', 'on') %}
            {{ [ (states('input_number.zone_1_run_time')|int(0) - (as_timestamp(now()) - as_timestamp(states.switch.zone_1.last_changed))/60)|round(0, default = 0) ,0 ]|max }}
          {% else %}
            0
          {% endif %}

Hi,

Thanks for your answers. I will try your variants but for the moment I am having some wardware problems. I will post as soon as I work them out.
BR,
Cosmin