Time interval triggered switch conditions

Hello,

I made the following automation to trigger a boolean switch when the date is between two input_datetime. So it turns on the switch when we are bewteen and turns off when not.

The switch is turned off when the date is not in the interval so it works for the turn off part.
But when the conditions are met in the turn on part, the switch keeps going for on to off to on to off, etc… every seconds.

I think the problem come from the conditions in the turn_off part but I’m unable to resolve it.

thanks

- id: '1557859466358'
  alias: Chauffage spécial ON
  trigger:
  - platform: time_pattern
    seconds: '/10'
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ states("sensor.date_time") > (states.input_datetime.date_debut.attributes.timestamp | int | timestamp_custom("%Y-%m-%d, %H:%M", True)) }}'
      - condition: template
        value_template: '{{ states("sensor.date_time") < (states.input_datetime.date_fin.attributes.timestamp | int | timestamp_custom("%Y-%m-%d, %H:%M", True)) }}'
      - condition: template
        value_template: '{{ is_state("input_boolean.chauffedate", "off") }}'
  action:
  - alias: 'ON'
    service: input_boolean.turn_on
    entity_id: input_boolean.chauffedate
- id: '1557859716841'
  alias: Chauffage spécial OFF
  trigger:
  - platform: time_pattern
    seconds: '/10'
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ is_state("input_boolean.chauffedate", "on") }}'
      - condition: or
        conditions:
          - condition: template
            value_template: '{{ states("sensor.date_time") < (states.input_datetime.date_debut.attributes.timestamp | int | timestamp_custom("%Y-%m-%d, %H:%M", True)) }}'
          - condition: template
            value_template: '{{ states("sensor.date_time") > (states.input_datetime.date_fin.attributes.timestamp | int | timestamp_custom("%Y-%m-%d, %H:%M", True)) }}'
  action:
    - alias: 'OFF'
      service: input_boolean.turn_off
      entity_id: input_boolean.chauffedate

your time/date comparison won’t work with your date because months and days are not the same length.

You’re doing a string comparison and when that happens, it compares the string left to right, in order. So you will get in a situation where'9' > '10' because 9 is greater than 1. This normally works with times in home assistant because hours and minutes are always length 2, i.e. 01 for 1 o’clock. This does not happen with month and day inside the date. You’ll need to compare the timestamps.

{{ state_attr('input_datetime.date_fin','timestamp') < now().timestamp() < state_attr('input_datetime.date_debut','timestamp') }}

and for your off template, just use not

{{ not state_attr('input_datetime.date_fin','timestamp') < now().timestamp() < state_attr('input_datetime.date_debut','timestamp') }}

Thanks, I changed the conditions with timestamp.

I keep getting the same issue, the switch’s state changes every couple of seconds when it needs to stay activated.

Do you just want this to turn on when the between the dates and turn off when not between the dates? What’s your reasoning for firing this every 10 seconds?

Yes exactly.
I trigger every 10 seconds for the test but I will change to 1 minute when it’ll work

So why don’t you just make a template binary_sensor then? No need for a automation

binary_sensor:
  # do not add a second binary_sensor section, past this into yours if you already have one.
  - platform: template
    sensors:
      chauffedate:
        friendly_name: Chauffe Date
        entity_id:
          sensor.time
        value_template: >
          {{ state_attr('input_datetime.date_fin','timestamp') < now().timestamp() < state_attr('input_datetime.date_debut','timestamp') }}

You’ll aslo need to add this in your sensor section as well:

sensor:
  # do not add a second sensor section, paste this into yours if you already have one.
  - platform: time_date
    display_options:
      - 'time'

This will be on during the dates and off outside the dates.