Please help with automation and input datetime

Hi,
I’m triying to made a realtiv complex automation for a sprinker system.

One goal was to select the start and stop time in the UI with an input_datetime helper. I’m having a pretty hard time to get this to work. (why is thats so incredible complicated with template etc…?)

Now I need to compare input_datetime with < and >. Is that even possbile without template? I’m pretty bad at templating…

It should look something like this. (Sorry I’m REALY bad :slight_smile: )

{% if states('sensor.time') > (states.input_datetime.start_bewaesserung_erd.attributes.timestamp) and
{% if states('sensor.time') < (states.input_datetime.start_bewaesserung_erd.attributes.timestamp)
Then = TRUE
{% endif %}

And this should be a condition in this automation

- id: auto_steuerung_erd
  alias: Erdbeeren Auto Steuerung
  description: ''
  trigger:
  - entity_id: input_boolean.auto_steuerung_erd_1
    platform: state
    to: 'on'
  condition: []
  action:
  - data: {}
    entity_id: input_boolean.manuelle_steuerung_erd_1
    service: input_boolean.turn_off

Please help.

It might be easier to define start time and duration time separately. Example of start time code

  - id: 023
    alias: 'Pump On'
    trigger:
      - platform: template
        value_template: "{{ states('input_datetime.pump')[:5]
          == states('sensor.time') }}"

You can define a datetime with date and time and this will pick off the time portion to determine when to start. Then create separate automation to step through each circuit and probably utilizing a timer. Just a thought.

Here’s the config I use for turning heating on and off. It may give you a start.

### Heating on 1 timer ###
- platform: template
  sensors:
    heating_on_1_hour:
      value_template: '{{ states.input_number.heating_on_1_hour.state | int }}'
    heating_on_1_minute:
      value_template: '{{ states.input_number.heating_on_1_minute.state | int }}'
    heating_on_1_time:
      value_template: >-
        {{ states.sensor.heating_on_1_hour.state }}:
        {%- if states.sensor.heating_on_1_minute.state|length == 1 -%}
          0
        {%- endif -%}
          {{ states.sensor.heating_on_1_minute.state }}
    heating_on_1_long_time:
      value_template: >-
        {% if states.sensor.heating_on_1_hour.state|length == 1 -%}
          0
        {%- endif -%}
          {{ states.sensor.heating_on_1_hour.state }}:
        {%- if states.sensor.heating_on_1_minute.state|length == 1 -%}
          0
        {%- endif -%}
          {{ states.sensor.heating_on_1_minute.state }}

### Heating off 1 timer ###
- platform: template
  sensors:
    heating_off_1_hour:
      value_template: '{{ states.input_number.heating_off_1_hour.state | int }}'
    heating_off_1_minute:
      value_template: '{{ states.input_number.heating_off_1_minute.state | int }}'
    heating_off_1_time:
      value_template: >-
        {{ states.sensor.heating_off_1_hour.state }}:
        {%- if states.sensor.heating_off_1_minute.state|length == 1 -%}
          0
        {%- endif -%}
          {{ states.sensor.heating_off_1_minute.state }}
    heating_off_1_long_time:
      value_template: >-
        {% if states.sensor.heating_off_1_hour.state|length == 1 -%}
          0
        {%- endif -%}
          {{ states.sensor.heating_off_1_hour.state }}:
        {%- if states.sensor.heating_off_1_minute.state|length == 1 -%}
          0
        {%- endif -%}
          {{ states.sensor.heating_off_1_minute.state }}

Thanks for reply.
I already have an automation for the start timer. Works great.

But I want to control the water in manual and auto. I already made an automation for this. But the problem is:
IF manual mode is on and I switch to auto mode, AND its between START and STOP time, to Cycle won’t start, because I need this in my First Post.

UI looks like this:

Automations:

- id: '1589453557193'
  alias: Bewässerung Start (Lars)
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.time'') == (states.input_datetime.start_bewaesserung.attributes.timestamp
      | int | timestamp_custom(''%H:%M'', False)) }}'
  condition:
  - condition: state
    entity_id: input_boolean.auto_steuerung_him_1
    state: 'on'
  action:
  - data: {}
    entity_id: input_boolean.manuelle_steuerung_him_1
    service: input_boolean.turn_off
  - data: {}
    entity_id: switch.ventil_1
    service: switch.turn_on
  - data: {}
    entity_id: script.bewaesserung_ein_zeit
    service: script.turn_on
- id: '1589483951575'
  alias: Bewässerung Stop (Lars)
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.time'') == (states.input_datetime.stop_bewaesserung.attributes.timestamp
      | int | timestamp_custom(''%H:%M'', False)) }}'
  condition: []
  action:
  - data: {}
    entity_id: script.bewaesserung_aus_zeit
    service: script.turn_off
  - data: {}
    entity_id: script.bewaesserung_ein_zeit
    service: script.turn_off
  - data: {}
    entity_id: switch.ventil_1
    service: switch.turn_off
- id: '1589637021637'
  alias: Manuelle Steuerung
  description: ''
  trigger:
  - entity_id: input_boolean.manuelle_steuerung_him_1
    platform: state
    to: 'on'
  condition: []
  action:
  - data: {}
    entity_id: input_boolean.auto_steuerung_him_1
    service: input_boolean.turn_off
  - data: {}
    entity_id: script.bewaesserung_aus_zeit
    service: script.turn_off
  - data: {}
    entity_id: script.bewaesserung_ein_zeit
    service: script.turn_off
  - data: {}
    entity_id: switch.ventil_1
    service: switch.turn_off
- id: '1589637712611'
  alias: Auto Steuerung
  description: ''
  trigger:
  - entity_id: input_boolean.auto_steuerung_him_1
    platform: state
    to: 'on'
  condition: []
  action:
  - data: {}
    entity_id: input_boolean.manuelle_steuerung_him_1
    service: input_boolean.turn_off

Scripts:

bewaesserung_ein_zeit:
  alias: Bewässerung ein Zeit Him
  sequence:
  - delay:
      minutes: '{{ states("input_number.bewaesserung_an_in_minuten") | round() }}'
  - data: {}
    entity_id: switch.ventil_1
    service: switch.turn_off
  - data: {}
    entity_id: script.bewaesserung_aus_zeit
    service: script.turn_on
bewaesserung_aus_zeit:
  alias: Bewässerung aus Zeit Him
  sequence:
  - delay:
      minutes: '{{ states("input_number.bewaesserung_aus_in_minuten") | round() }}'
  - data: {}
    entity_id: script.bewaesserung_ein_zeit
    service: script.turn_on
  - data: {}
    entity_id: switch.ventil_1
    service: switch.turn_on

@ashscott
Sorry I dont get this exactly. Your config shows a input_number but I need input_date.

My apologies if I’m completely missing what you are trying to achieve but just in case it’s useful, there’s this addon HA Scheduler Addon

You could use an automation, with your given conditions, to turn the schedule on and off.

1 Like

Hi,
thanks a lot. That looks good. Maybe I can give it a try.

A good tip is to use the template tool in developer tools.
If you paste what you are trying to compare there it becomes obvious why it doesn’t work.

I used variables I have in my setup so obviously they can be different.

But sensor.time is the time
And input datetime is either a time or a datetime, in my case a datetime.

And here we see how you try to compare a time with a unix timestamp

However you can use sensor date time iso as timestamp and compare that to your input datetime.


That results in two numeric values that can be easily compared.

Hi,

the HA Schedular Addon did exactly what I want. The hole automation is a lot easier now.
The only problem is, that I cant insert the time value via Lovelace.
I asked the developer to add this feature. I’m not sure its possible^^

@Hellis81
Thanks! I know this tool. But my template skills are simple not powerful enough to achieved my goal :frowning:

1 Like

You can try with this:

{{ states.input_datetime.test.attributes.timestamp  | timestamp_custom("%H:%M", false) }}
{{ states.sensor.time.state }}

In the template tool.
My datetime here is a datetime with only time so by default it’s something like 11:55:00.
Sensor.time is something like 12:01.

By using the attribute timestamp we get the second of the day that the datetime is, then we convert that back to H:M format (in reality we remove the “:00” at the end).
This should be two formats that can be compared.

Just as an example I made this:


The last line that does not fit the screen is:
{{ states.input_datetime.test.attributes.timestamp | timestamp_custom("%H:%M", false) > states.sensor.time.state }}

With that type of format your automation should work.

Ok wow. Thanks for explaining this. It was easier than I thought. Thats the resault and it works fine!

- id: auto_steuerung_erd
  alias: Erdbeeren Auto Steuerung
  description: ''
  trigger:
  - entity_id: input_boolean.auto_steuerung_erd_1
    platform: state
    to: 'on'
  condition:
  - condition: template
    value_template: "{{states.input_datetime.start_bewaesserung_erd.attributes.timestamp\
      \  | timestamp_custom(\"%H:%M\", false) \n<=\nstates.sensor.time.state}}"
  - condition: and
    conditions:
    - condition: template
      value_template: "{{states.input_datetime.stop_bewaesserung_erd.attributes.timestamp\
        \  | timestamp_custom(\"%H:%M\", false) \n>=\nstates.sensor.time.state}}"
  action:
  - data: {}
    entity_id: switch.ventil_2
    service: switch.turn_on