Template Condition does not work

Hi everybody.

I have an automation that makes me mad. It’s very simple. I don’t want the automation executes more than once in a period of 15 seconds. But it does not work. To undestand what is happening, i have written a ‘system_log.write’ with a copy paste of the condition, like this :

  - id: '1562516212611'
    alias: Llaman al portero
    description: Notifica por telegram las llamadas al portero
    trigger:
    - entity_id: binary_sensor.portero
      platform: state
      from: 'off'
      to: 'on'
    condition:
    - condition: template
      value_template: '{{ (as_timestamp(now()) - as_timestamp(state_attr("automation.llaman_al_portero", "last_triggered")) |int(20) ) > 15 }}'
    action:
    - data:
        message: "\U0001F4DE Han llamado al portero"
      service: notify.telegram_casa
    - data_template:
        user: !secret user_telegram_sergio
        message: Hola_Sergio,_han_llamado_al_portero
      service: shell_command.telegram_call
    - service: system_log.write
      data_template:
        level: warning
        logger: homeassistant.components.mylogger
        message: 'Condition of automation.llaman_al_portero is {{ (as_timestamp(now()) - as_timestamp(state_attr("automation.llaman_al_portero", "last_triggered")) |int(20) ) > 15 }}'

This morning has happened again, the automation executed twice in less than 15 seconds. When i have seen the log, i have found this:

2020-01-31 17:04:58 WARNING (MainThread) [homeassistant.components.mylogger] Condition of automation.llaman_al_portero is True
2020-01-31 17:05:03 WARNING (MainThread) [homeassistant.components.mylogger] Condition of automation.llaman_al_portero is False

The firt log is OK, but the second log tells me that the condition is False. So, why the automation is being executed??

Why are you using int(20) ?

To get a value of 20 if I receive a NaN value like ‘unknown’. In that way the automation will be executed (20 > 15).

1 Like

try : -
as_timestamp(states.automation.llaman_al_portero.last_triggered)

not at a terminal

Sorry there’s no such attribute

Why not check the last changed on an entity ‘touched’ by this automation ?
an input boolean or similar ?

last_triggered is found within the State object’s attributes property. So to reference it you would use:

{{ states.automation.llaman_al_portero.attributes.last_triggered }}

or via the state_attr() function.

You may be thinking of last_updated or last_changed which are properties of the State object (i.e. not attributes). They can be referenced like this:

{{ states.automation.llaman_al_portero.last_updated }}
1 Like

Ah !
Thank you !

never needed last_anything for myself, just used last changed on forum for others
dang, and nearly there with a guess, just missed a word out :rofl:

Edit: isn’t the state_attr( what he’s using ?

is this your doorbell being rung twice quickly?

Thanks everybody.

I think I’m using a correct statement.

I have also used:

{{ states.automation.llaman_al_portero.attributes.last_triggered }}

But the result is the same. In the log the condition appears ‘False’ but the automation executed! Wow, it’s a mystery…

No, it doesn’t @petro. Between each ring there could be about 5 seconds

but it is a doorbell. Guessing the ESP one. Just wondering if I can set the automation up at my house and test. I’d be willing to bet it has to do with when last_triggered gets populated. I also suspect that your system_log.write is always off and that it doesn’t truly reflect the condition because last_triggered may be updated by the time it’s reached.

I remember another post by a guy whose daughter stands at the door …
Ring … Ring … Ring … Ring … Ring … Ring …
and as you’re not allowed to beat your child sensless anymore …

Hey do you use appdaemon? I just made a doorbell notifier there, I could easily add this functionality.

You are right. It is a doorbell detected by an ESP32 using the ESPHome firmware.

Home Assistant registry said the following:

17:04 Portero cleared (no sound detected)

17:04 Llaman al portero has been triggered

17:04 Portero detected sound

17:04 Portero cleared (no sound detected)

17:04 Llaman al portero has been triggered

17:04 Portero detected sound

I don’t know if you are right, My system_log.write is always off?? It’s strange…

It could be off. It really depends on when last_triggered is populated. From what I always understood: It’s populated when actions start. But it’s been a year or so since I examined the code and my memory is not always correct.

Ok I understand. i’m going to investigate this trying to get the clue.

Any suggestion is welcome.

Thanks!!

I’d start my moving the action section into a stand alone script and only call the script in the action section.

Ok. I’will try it. I will post the result

I have tried moving the action section to an script, but de result is the same… :frowning: