Automation with Timestamp Error

I’m getting an error in an automation that includes a timestamp and i simply don’t understand what the error is telling me. Script works fine when tested but the automation when triggered just results in the error below.

Can anyone please advise what I need to do to fix this?

**EDIT: The timestamp was included to ensure the automation only runs once when triggered during the hours stated in the condition

Automation

- alias: Morning Greeting
  id: morning_greeting
  trigger:
  - platform: state
    entity_id: binary_sensor.staircase_sensor_motion
    to: 'on'
  condition:
    condition: and
    conditions:
    - condition: time
      after: '06:00:00'
      before: '15:00:00'
    - condition: state
      entity_id: input_boolean.holiday_mode
      state: 'off'
    - condition: template
      value_template: '{{(as_timestamp(now()) - as_timestamp(state_attr("automation.<automation
        ID>", "last_triggered") | default(0)) | int > 21600 )}}'
    - condition: template
      value_template: '{{ is_state("sensor.iphone_r_battery_state", "Not Charging")
        }}'
    - condition: template
      value_template: '{{ is_state("person.r", "home") }}'
  action:
  - service: script.morning_greeting

Log error

Logger: homeassistant.components.automation
Source: components/automation/__init__.py:1006
Integration: Automation (documentation, issues)
First occurred: 2:53:01 PM (2 occurrences)
Last logged: 2:54:14 PM

Error evaluating condition in 'Morning Greeting': In 'condition': In 'and' (item 3 of 5): In 'template' condition: ValueError: Template error: as_timestamp got invalid input 'None' when rendering template '{{(as_timestamp(now()) - as_timestamp(state_attr("automation.<automation ID>", "last_triggered") | default(0)) | int > 21600 )}}' but no default was specified

According to here:
Templating - Home Assistant (home-assistant.io)

as_timestamp syntax is:
as_timestamp(value, default) converts datetime object or string to UNIX timestamp. If that fails, returns the default value, or if omitted raises an error.

1 Like

Thanks. I saw that before posting but I already have a default defined in the template so I’m not sure why I’m getting the error.

Check the dev tools, or paste the line in the template editor.
Chances are you will find that the automation has never been triggered, thus the attribute is “None” instead of a timestamp.

As for your default, that doesn’t help, because of as_timestamp can’t do it’s job - it will throw the error, as it has here. Your default not related to the as_timestamp function at all. It’s doing a completely different job.

I just tried it in the Dev Tools and it throws up this error:

ValueError: Template error: as_timestamp got invalid input 'None' when rendering template '{{(as_timestamp(now()) - as_timestamp(state_attr("automation.<automation ID>", "last_triggered") | default(0)) | int > 21600 )}} {% set my_test_json = { "temperature": 25, "unit": "°C" } %} The temperature is {{ my_test_json.temperature }} {{ my_test_json.unit }}. {% if is_state("sun.sun", "above_horizon") -%} The sun rose {{ relative_time(states.sun.sun.last_changed) }} ago. {%- else -%} The sun will rise at {{ as_timestamp(state_attr("sun.sun", "next_rising")) | timestamp_local }}. {%- endif %} For loop example getting entity values in the weather domain: {% for state in states.weather -%} {%- if loop.first %}The {% elif loop.last %} and the {% else %}, the {% endif -%} {{ state.name | lower }} is {{state.state_with_unit}} {%- endfor %}.' but no default was specified

Now try - as was suggested:

{(as_timestamp(now()) - as_timestamp(state_attr("automation.<automation ID>", "last_triggered"),0) | int(0) > 21600 ) }}

That returns True

That would make sense, because now() - 0 will be greater than 21600

So I just replace my template with yours? Might sound a stupid question but I’m asking because you mentioned mine wasn’t working because it had never triggered.

I believe, though I may be wrong - but I believe “last_triggered” only gets a value when the automation is triggered by the automation engine. I believe that manually triggered, does not count - and because this condition was preventing the automation from ever successfully running, it will never get a last_triggered time. Yes replacing the code with the working code, which will default properly to a 0 timestamp (1970-01-01) should do the trick.

1 Like

Brilliant, thanks for your help.
It’s the only automation I have that uses timestamp so I’m not that clued up on them.

Working with timestamps can be a pain. Thankfully we now have timedelta so we can do things like:

{% set t = states.light.kitchen.last_changed %}
{{ t + timedelta(minutes=30) < now() }}

evaluates to true if the time the state was last changed + 30 minutes is less than now, eg the state was changed more than 30 minutes ago. No more faffing about with timestamps.

For your automation it would be something like this:

{% set t = state_attr('automation.my_automation','last_triggered') %}
{% if t is not none %}
{{ t + timedelta(hours=6) < now() }}
{% else %}
{{ true }}
{% endif %}
1 Like

I’ve tested this now, making the suggested change to the timestamp template but it’s not working. All conditions were met but nothing happened and, interestingly, nothing in the logs.

This is the automation:

- alias: Morning Greeting
  id: morning_greeting
  trigger:
  - platform: state
    entity_id: binary_sensor.staircase_sensor_motion
    to: 'on'
  condition:
    condition: and
    conditions:
    - condition: time
      after: '06:00:00'
      before: '15:00:00'
    - condition: state
      entity_id: input_boolean.holiday_mode
      state: 'off'
    - condition: template
      value_template: '{(as_timestamp(now()) - as_timestamp(state_attr("automation.<automation ID>", "last_triggered"),0) | int(0) > 21600 ) }}'
    - condition: template
      value_template: '{{ is_state("person.r", "home") }}'
  action:
  - service: script.morning_greeting

Hi. I’ve tried to change my timestamp to your timedelta example but I’m getting the below error in the editor:

missed comma between flow collection entries (101:24)

 100 |     - condition: template
 101 |       value_template: {% set t = state_attr('automatio ...
------------------------------^
 102 |                          {% if t is not none %}
 103 |                         {{ t + timedelta(hours=6) < no ...

Templates have to be enclosed in " if they are on the same line. Because the example I provided was a multiline example you would need to do it like this:

value_template: >-
  {% set t = state_attr('automation.my_automation','last_triggered') %}
  {% if t is not none %}
    {{ t + timedelta(hours=6) < now() }}
  {% else %}
    {{ true }}
  {% endif %}

Notice the indentation too - it’s very important.

1 Like

Just tested it and it works perfectly.

Thanks for your help

1 Like

I’ve been testing this for a few days now and it actually doesn’t work as expected.

The action runs every time the trigger is activated, instead of just the first time it is triggered.