[solved] Script with template as_timestamp(now())

hello, i start with hassio. I am looking to automate the reception of parcels or the reading of the mailbox.
I need to know if I opened my front door within 4 minutes after or before opening the door of the mailbox.

if open script 1, if nothing script 2.

my template from below answers me in template true or false but no script starts.

  action:
  - delay:
      seconds: 120
  - service: script.turn_on
    data_template:
      entity_id: >-
        {%- if states.binary_sensor.detecteur_porte_entree.last_changed -%}
          {{ (as_timestamp(now())-as_timestamp(states.binary_sensor.detecteur_porte_entree.last_changed)) < 180 }}
        script.boite_aux_lettres_releve_courrier
        {%- else -%}
        script.boite_aux_lettres_colis_recu
        {%- endif -%}

Thanks for help me.

  action:
  - delay:
      seconds: 120
  - service_template:
      {% set now = as_timestamp(now()) %}
      {% set last_change = as_timestamp(states.binary_sensor.detecteur_porte_entree.last_changed) %}
      {%- if ( now - last_changed )|abs < 180 ) }}
        script.boite_aux_lettres_releve_courrier
      {%- else -%}
        script.boite_aux_lettres_colis_recu
      {%- endif -%}

Thanks @tom_l for reply.

There is a formatting problem. The Gabarit return erreur.

{%- if ( now - last_changed )|abs < 180 ) }}

That is indeed incorrect. Should be:

 {% if ( now - last_changed )|abs < 180 ) %}

So:

 action:
  - delay:
      seconds: 120
  - service_template:
      {% set now = as_timestamp(now()) %}
      {% set last_change = as_timestamp(states.binary_sensor.detecteur_porte_entree.last_changed) %}
      {% if ( now - last_changed )|abs < 180 ) %}
        script.boite_aux_lettres_releve_courrier
      {% else %}
        script.boite_aux_lettres_colis_recu
      {% endif %}

small typo left: should have an extra d after {% set last_change

1 Like

Right, hopefully this is it:

action:
- delay:
seconds: 120
- service_template:
{% set now = as_timestamp(now()) %}
{% set last_change = as_timestamp(states.binary_sensor.detecteur_porte_entree.last_changed) %}
{% if ( now - last_change )|abs < 180 ) %}
script.boite_aux_lettres_releve_courrier
{% else %}
script.boite_aux_lettres_colis_recu
{% endif %}

All wrong. See 3rd last post.

well, if it were up to me Id do this, add the d:

 action:
  - delay:
      seconds: 120
  - service_template:
      {% set now = as_timestamp(now()) %}
      {% set last_changed = as_timestamp(states.binary_sensor.detecteur_porte_entree.last_changed) %}
      {% if ( now - last_changed )|abs < 180 ) %}
        script.boite_aux_lettres_releve_courrier
      {% else %}
        script.boite_aux_lettres_colis_recu
      {% endif %}

:man_shrugging: TomATEo, TomAREto. PotATEo, PotAREto.

whats the point of the abs? Now-last_change should always be positive or in rare cases, slightly negative by microseconds.

Well shit. Re-reading the initial requirements I realised I’m not checking for the right time:

now() should be the mailbox last changed. That would require |abs for the before/after bit.

Yeah but before is impossible. You can’t predict the future. Last_changed will always be in the past in relation to now() or it will be equal (with a small chance of epsilon error).

Yeah your post alerted me to my mistake, I should not be using now(). It’s one sensor vs the other, not vs now(). @dckiller51 did not provide the mailbox door entity id so I made one up (which should be replaced with your actual mailbox door binary sensor @dckiller51 ). Hopefully this makes more sense:

 action:
  - delay:
      seconds: 120
  - service_template:
      {% set mailbox_last_changed = as_timestamp(states.binary_sensor.mailbox_door.last_changed) %}
      {% set front_door_last_changed = as_timestamp(states.binary_sensor.detecteur_porte_entree.last_changed) %}
      {% if ( mailbox_last_changed - front_door_last_changed )|abs < 180 ) %}
        script.boite_aux_lettres_releve_courrier
      {% else %}
        script.boite_aux_lettres_colis_recu
      {% endif %}

Script script.boite_aux_lettres_releve_courrier should now run if

opened my front door within 4 minutes after or before opening the door of the mailbox .

This is the bit you have to replace with your actual mailbox door sensor @dckiller51:

{% set mailbox_last_changed = as_timestamp(states.binary_sensor.mailbox_door.last_changed) %}

                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
1 Like

Ah, now the abs makes sense! I suppose I should have read more of the original post.

1 Like

You were 100% right though and put me on the correct path. Despite watching Primer many times I have yet to perfect time travel.

Thank you thank you and thank you. It works finally.
Thank you for your proposal to add the sensor door mailbox.
You have thoroughly analyze my need.

I just correct

 {% if ( mailbox_last_changed - front_door_last_changed )|abs < 180 ) %}
 {% if (mailbox_last_changed - front_door_last_changed)|abs < 180 %}
180 ) delete )
1 Like

I added a delay before reading the template.
Now it works that I pick up the mail before coming home or if I’m at home is that I will pick up the mail. Thank you for participating in everyone’s ideas is important to move forward.

I will test in real this week to see if it works. Then I close the post