Predicting the triggering of a timed automation

Good morning.
This is my irrigation automation:

trigger:
    - platform: time_pattern
      minutes: "/5"
  condition:
    - condition: or
      conditions:
        - condition: and
          conditions:
            - condition: sun
              after: sunrise
              before: sunset
              after_offset: "01:00:00"
            - "{{ (now().hour * 60 + now().minute)  % (states('input_select.choicedaytimewateringinterval')|int +  states('input_number.irrigationduration')|int ) == 0}}"
        - condition: and
          conditions:
            - condition: sun
              after: sunset
              before: sunrise
              after_offset: "-01:00:00"
            - "{{ (now().hour * 60 + now().minute)  % (states('input_select.choicenightwateringinterval')|int +  states('input_number.irrigationduration')|int ) == 0}}"
  action:

Working fine.

As you can see, the automation discriminates watering from one hour before sunset to sunrise and from one hour after sunrise to sunset as the irrigation intervals can be different.

Now I would need to create a label with custom button card that tells me:
Next watering: datetime.
How can I do?

Too stupid or too difficult question?

Here’s something to play around with…

{% set x = states('input_select.choicedaywateringinterval') | int +  states('input_number.irrigationduration') | int  %}
{% set ns = namespace(n_min=[]) %}
{% for h in range(0,24) if h >= now().hour%}
{% for m in range(0, 56, 5) -%}
  {%- set mod0 = ((h*60) + m) % x -%}
  {%- set dt = today_at(h|string~':'~ (m|string if m < 10 else '0'~m|string)) %}
  {%- set ns.n_min = (ns.n_min + [dt]) if (mod0 == 0 and dt >= now()) else ns.n_min %}
{% endfor %}{% endfor %}
{{ (ns.n_min) | reject('<=', state_attr('sensor.sun_rising', 'today') | as_datetime)
| reject('>', state_attr('sensor.sun_setting', 'today') | as_datetime + timedelta(hours=1)) | map("string") | first | default('None Scheduled', 1) }}

NOTE: This template uses sensors from the Sun2 custom integration something similar can be created using a trigger-based template sensor if you don’t want to use Sun2.

EDIT: Fixed typo noted below and updated rising and setting sensor reference to use today attribute.

Thanks for your attention!

The error remains even if I remove a curly bracket at the end of the first line

It’s just a small typo in the first line. Try this…

{% set x = states('input_select.choicedaywateringinterval') | int + states('input_number.irrigationduration') | int %}

Start and end brackets need to match so deleting it wouldn’t help.

There was a “}” instead of “%”

I don’t understand what you mean. Use {%...%}

I corrected it as you indicated.
The error changed…

IMO It’s an entity issue that is most likely input_select.choicedaywateringinterval

Replace it with input_number.irrigationduration just for test reasons and see if you get results.

Like this:

{% set x = states('input_number.irrigationduration') | int +  states('input_number.irrigationduration') | int  %}

Nothing change…

Hmm working for me.

Please post your template code for the input_number and input_select.

irrigationduration:
  name: Durata irrigazione postazione idroponica n° 13
  #initial: 0
  min: 0
  max: 30
  step: 5
  mode: box
  unit_of_measurement: "min"
  icon: mdi:timer
choicedaywateringinterval:
    name: Scelta dell'intervallo fra le irrigazioni diurne per la postazione idroponica n. 13
    options:
      - 0
      - 15
      - 30
      - 45
      - 60
      - 90
      - 120

Can you check Developer Tools for the current state of each of those? HA can’t add the data together suggesting one isn’t a number.

I created similar templates to yours and still get the correct results. I think we can eliminate your sensors. Check the sun sensors in Developer Tools to see if they are currently showing dates

I am at a loss. Just so we cover it, can you post your current code you are using? I’ll test it on my system.

Thank you for your patience.

What code do you need?

The automation code is in the first post.
Works well

The code you are testing.