SQL Sensor - help with syntax needed

Hi, I’m working with some sql sensors which is very effective.
Now, I have a new one, but with the state of an input_datetime entity:

- name: "last_irrigation_per_m2"
      query: >
        SELECT COALESCE(round((Sum(state) /12),1),0) 'value' FROM states 
            WHERE entity_id = 'variable.last_water_consumption' and strftime('%Y-%m-%d %H:%M:%S',created) > '{{states('input_datetime.last_irrigation_started}}';

I know, that this can not work. I have to take over the entity as text.
Can sombody help me how to build the sql text and the datetime entity together?
The sql works, when I replace the entity with a simple date and time.

Indentation and syntax is wrong, but the main issue is that the query only accepts a string, not a template.

Perhaps you could do a nested SELECT to pull out the current state of input_datetime.last_irrigation_started from the database? See if this works on the command line:

SELECT COALESCE(round((Sum(state) /12),1),0) 'value' FROM states 
            WHERE entity_id = 'variable.last_water_consumption' and strftime('%Y-%m-%d %H:%M:%S',created) > (SELECT state FROM states WHERE entity_id = 'input_datetime.last_irrigation_started' LIMIT 1);

Ah, thank you. I did that before in another statement. That’s the way…
Why didn’t I simply look some sensors above :wink:
Important: the input_datetime has to be recorded!