Help with template trigger, please

Hi All, I have just tried to create a new packaged automation but it isn’t working when I try to test it. Since the date state isn’t going to change and therefore will not trigger the automation, I used the manual trigger option from the GUI in the hope that the rest of the automation would then work once the current time met that of the input_datetime, but it didn’t. Can someone please tell me what I have wrong here? I also don’t know if the date trigger is quite right since I haven’t tested that yet.

alarm_off_for_rent_inspection.yaml (in packages folder)

input_datetime:
  rent_inspection_date:
    name: Rent Inspection Date
    has_date: true
    has_time: false
  
  rent_inspection_start_time:
    name: Rent Inspection Start Time
    has_date: false
    has_time: true


automation:
  - alias: Alarm auto disarm for rent inspection
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.date
        to: '2018-07-31'
      - platform: template
        value_template: '{{ now().date() == "input_datetime.rent_inspection_date" }}'
    
    action:
      - wait_template: "{{ is_state('sensor.time', 'input_datetime.rent_inspection_start_time') }}"
      - service: alarm_control_panel.alarm_disarm
        entity_id: alarm_control_panel.house
        data:
          code: 111111111 #not my real code


group:
  rent_inspection_alarm_control:
    control: hidden
    name: 'Rent Inspection Alarm Control'
    entities:
      - input_datetime.rent_inspection_date
      - input_datetime.rent_inspection_start_time

The templates are only evaluated when the entities inside change, and now() is excluded from that list, to avoid triggering every fraction of a second.

The answer is to include a template like:

    value_template: '{{ now().date() == "input_datetime.rent_inspection_date" }}{# states('sun.sun') #}'

This makes the state of the sun.sun entity evaluated by the template engine, but ignored because it’s in a comment. That will result in your template being evaluated every minute.

Of course, given that you’re only checking the date, I’d use a time trigger for (say) 02:00 and move that check to a condition.

good idea. I guess I was mistaken in thinking that the template would only evaluate on the change from one day to the next. Is there not a way to do that? (perhaps without a template?) I was trying to use the event of a date change to check if the date matches that of the input_datetime and then wait until the corresponding time to complete the action

Honestly, I’d probably use a calendar event.

I will likely set up the calender solution as the final, however in the meantime I’m trying to get it to work using a date condition but nothing i try passes the config checker. how can i make this work? help please :slight_smile:

input_datetime:
  rent_inspection_date:
    name: Rent Inspection Date
    has_date: true
    has_time: false
  
  rent_inspection_start_time:
    name: Rent Inspection Start Time
    has_date: false
    has_time: true


automation:
  - alias: Alarm auto disarm for rent inspection
    initial_state: 'on'
    trigger:
      - platform: time
        at: '01:00:00'

#      - platform: state
#        entity_id: sensor.date
#        to: '2018-07-31'
#      - platform: template
#        value_template: '{{ now().date() == "input_datetime.rent_inspection_date" }}'
    condition:
      - condition: time
        entity_id: sensor.date
        to: 'input_datetime.rent_inspection_date'

    
    action:
      - wait_template: "{{ is_state('sensor.time', 'input_datetime.rent_inspection_start_time') }}"
      - service: alarm_control_panel.alarm_disarm
        entity_id: alarm_control_panel.house
        data:
          code: 11111111


group:
  rent_inspection_alarm_control:
    control: hidden
    name: 'Rent Inspection Alarm Control'
    entities:
      - input_datetime.rent_inspection_date
      - input_datetime.rent_inspection_start_time

I have also tried this as the condition but it doesn’t pass config check either :tired_face:

condition:

  • condition: template
    value_template: ‘{{ now().date() == “input_datetime.rent_inspection_date” }’

I think I found the issue, a missing }

EDIT: the automation passes the config checker but doesnt actually work

Assuming that the string for now().date() and the state for input_datetime.rent_inspection_date have the same format, it would be:

condition:
  - condition: template
    value_template: "{{ now().date() == states('input_datetime.rent_inspection_date') }}"

thanks for that, I tried it but it still isn’t going through with the action.

Looking at the town entities, the input_datetime returns a format 06:18:00 whereas the sensor.time is 06:18 How would I need to change the template to accommodate this? Sorry but my knowledge / skill with templates is extremely limited

well you aren’t using sensor.time, you are using now().date(). Also, I assume you are using the input_datetime that returns a date. So What is the format of the dates?

oops sorry. yeah I wasnt thinking. date is 2018-07-24 , input_datetime.rent_inspection_date is 2018-07-31 so the same format…:thinking:

EDIT: although these dates above wont cause a trigger, I had previously had them both set to today for testing