Automation trigger strange behaviour

I have this automation:

- alias: Meteo ed Agenda
  trigger:
    - platform: state
      entity_id: input_boolean.mauhome
      to: 'on'
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d0001a92ca1
      to: 'on'
  condition:
    - condition: time
      after: '08:30'
      before: '10:00'
    - condition: template
      value_template: '{{(as_timestamp(now()) - as_timestamp(states.automation.meteo_ed_agenda.attributes.last_triggered | default(0)) | int > 5400 )}}'
  action:
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.googlehome0461
        volume_level: 0.50 
    - service: tts.google_say
      data_template:
        entity_id: media_player.googlehome0461
        message: >-

… and follows the message i want to be said…

It works good if the motion sensor is activated between the time configured in condition, but if the motion sensor is triggered before that time, the automation start randomly and automatically between that time.
How to change it so it is triggered only if the motion sensor is activated between that time?

I’m not seeing anything definite but maybe try adding the seconds to the time conditions?

‘08:30:00’
‘10:00:00’

Hmm i think it’s not the problem as i have other automations with that condition and work good…

I believe there is an issue with using now() in a template. Something about it not updating and staying static to the time the automation was deployed or something.

Warning: Rendering templates with time (now()) is dangerous as updates only trigger templates in sensors based on entity state changes.

You could try modifying your template to use the time_date sensor instead of now().

Not absolutely sure this is your issue, but it would be the first thing I would check.

Can you give me an example how to insert that entity in the automation?
Maybe adding a

  • entity_id: time_date

But where in that automation?

Looking at the automation i see i have it as sensor, so i think it’s enough to make the template update, or not:

states.automation.meteo_ed_agenda.attributes.last_triggered

The issue is using it as part of the sensors (i.e. effectively the trigger) in a template_sensor (since version 0.81). Here it is being used in the condition of an automation.

1 Like

Thanks, the wording of the warning was confusing to me.

I just recently had an issue using now() in a condition template that was resolved by using the date_time sensor instead and thought it might apply.

Hence it was fresh on my mind.

When Home Assistant restarts, what is the value of last_triggered? My guess is it is undefined.

Is that why you used default(0)? So that if the calculation of the elapsed time cannot be determined, the value defaults to zero?

Second question, aren’t Unix timestamps 32-bit integer values? I’m wondering why you chose to use int > 5400 if the timestamp is already an integer.

BTW, if this was a 16-bit system then the elapsed time could easily exceed the maximum limit for int16.

Sorry, but my knowledge is not so deep about your thinking… Can you be more easy for me? :rofl:
And if you are right, how to correct the automation?

All I’ve done is put into words what you wrote here:

    - condition: template
      value_template: '{{(as_timestamp(now()) - as_timestamp(states.automation.meteo_ed_agenda.attributes.last_triggered | default(0)) | int > 5400 )}}'

Did you create this value_template or did you copy it from somewhere?

I copied from another automation as i still am not able to create so complex code.

Whats happenening is last_triggered is not defined, it’s defaulting to now()-zero resulting in a number greater than 5400.

@maurizio53 use this:

- condition: template
  value_template: >
    {% if states.automation.meteo_ed_agenda.attributes.last_triggered != None %}
      {{ (now()-states.automation.meteo_ed_agenda.attributes.last_triggered).seconds > 5400 }}
    {% else %}
      False
    {% endif %}

EDIT: Had to adjust it because it will always be defined. Need to check for null/None.

OK, no problem. Here is what I think is happening.

This part means uses the value for last_triggered or, if it is undefined, it uses zero.

as_timestamp means convert the value into Unix timestamp format. That’s just a big number representing the number of seconds since January 1, 1970.

This part calculates the elapsed time, namely the number of seconds from now() and when the automation triggered previously (last_triggered).

If that integer number is greater than 5400 seconds (int > 5400) then the template evaluates to true.

However, the problem is that if this:
as_timestamp(states.automation.meteo_ed_agenda.attributes.last_triggered | default(0)
is zero then the elapsed time is just equal to the current timestamp which is a number much larger than 5400. So the template also evaluate to true even though the elapsed time is not truly 5400 seconds.

So the template’s logic is flawed.

While typing all this, I noticed @petro has already summarized it and posted a solution so I’m going to stop typing now …. :slight_smile:

Sorry, I was a little straight to the point. Not trying to steal your thunder. Good explanation!

Thanks to all for the patience… i will try what @petro and @123 adviced me and let you know what happens…

Haha! No problem! You stated the core problem perfectly here:

My guess is last_triggered is undefined immediately after restarting Home Assistant. Effectively, the first time the automation is executed, it has no last_triggered and the template’s faulty logic causes a false-positive (between 8:30 and 10:00).

Your guess is correct. I just restarted, luckily I have 1 automation that isn’t in appdaemon. That automation hasn’t been fired and last_triggered is defined but set to null. Which is why I updated the post. The only concern I have is that I hope last_triggered is a date time object.

May i ask another thing to you always about templates?

I have this sensor:

- platform: template
  sensors:
    date_template:
      entity_id: sensor.time__date
      friendly_name: 'Oggi'
      value_template: >
        {{ as_timestamp(now()) | timestamp_custom("%d/%m/%y", True) }}

But i always get this error:

Could not render template Oggi, the state is unknown.

Where in this case i am wrong?

If i try it in dev-template i get the right day…

EDIT: Solved… data_template and NOT date_template !!!
EDIT: Not solved, that is the name of the sensor…

Not sure, it looks correct to me. Might need to change it to use the sensor.time__date instead