Problem with notification message

So I have a notification when my garage door is closed and my alarm is set. Looks like this:

      message: '{%- if (as_timestamp(now()) - as_timestamp(states.switch.sonoff63719.last_changed)
        < 60 ) and as_timestamp(states.switch.sonoff51083.last_changed) 
        < 60 -%} Alarm disabled & Garage opened Verified {%- else -%} Error occurred disarming {%- endif -%}'

In the template editor, it can show TRUE but I still get an error notification. What am I doing wrong?

@petro you’re the man with this stuff…

I usually break these up if they are complicated. And in doing so with your template, i found your issue. You aren’t subtracting the second last_changed from now().

{%- set t = as_timestamp(now()) %}
{%- set sonoff63719 = t - as_timestamp(states.switch.sonoff63719.last_changed) %}
{%- set sonoff51083 = t - as_timestamp(states.switch.sonoff51083.last_changed) %}
{%- if sonoff63719 < 60 and sonoff51083 < 60 %}
  Alarm disabled & Garage opened Verified
{%- else %}
  Error occurred disarming 
{%- endif %}
    data_template:
      message: '{%- if (as_timestamp(now()) - as_timestamp(states.switch.sonoff63719.last_changed)
        < 60 ) and (as_timestamp(now()) - as_timestamp(states.switch.sonoff51083.last_changed) 
        < 60 ) -%} Garage closed & Alarm Set Verified {%- else -%} Error occurred arming {%- endif -%}'

No idea how I missed it but it won’t validate

Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected '%') for dictionary value @ data['action'][4]['data_template']['message']. Got None. (See /config/configuration.yaml, line 354). Please check the docs at https://home-assistant.io/components/automation/
Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected '%') for dictionary value @ data['action'][2]['data_template']['message']. Got None. (See /config/configuration.yaml, line 354). Please check the docs at https://home-assistant.io/components/automation/

Solved with help from Petro on discord. Here’s what I ended up with:

- id: 'deactivatealarmopengarage'
  alias: Deactivate Alarm and Open Garage
  trigger:
  - entity_id: input_boolean.alarmgaragedoor
    from: 'on'
    platform: state
    to: 'off'
  action:
  - service: switch.turn_on
    data:
      entity_id: switch.sonoff63719
  - service: switch.turn_on
    data:
      entity_id: switch.sonoff51083
  - delay: "00:00:10"
  - service: notify.notify
    data_template:
      message: >
        {%- set t = as_timestamp(now()) %}
        {%- set sonoff63719 = t - as_timestamp(states.switch.sonoff63719.last_changed) %}
        {%- set sonoff51083 = t - as_timestamp(states.switch.sonoff51083.last_changed) %}
        {%- if sonoff63719 < 60 and sonoff51083 < 60 %}
          Alarm disabled & Garage opened Verified
        {%- else %}
          Error occurred disarming 
        {%- endif %}
  - service: persistent_notification.create
    data_template:
      message: >
        {%- set t = as_timestamp(now()) %}
        {%- set sonoff63719 = t - as_timestamp(states.switch.sonoff63719.last_changed) %}
        {%- set sonoff51083 = t - as_timestamp(states.switch.sonoff51083.last_changed) %}
        {%- if sonoff63719 < 60 and sonoff51083 < 60 %}
          Alarm disabled & Garage opened Verified
        {%- else %}
          Error occurred disarming 
        {%- endif %}

Similar for setting. I was getting conflicting notifications until I put the delay in there FWIW.