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
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.
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
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.
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 %}
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.
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 %}