Dealing with 'null' conditions in an automation template

Hi all,

I’m presently trying to set up an automation that will fire under two conditions:

  • The automation has not been run yet today
  • The automation has not yet been run at all (i.e. when it is new, or resetting the HA database)

I am using the templating tool to set this up, however the issue I am facing is that when the automation has never been fired before (i.e. states.automation.test.attributes.last_triggered == null) the template is returning the following message in big orange letters:

This template listens for all state changed events.

image

I think it is because attempting to use .strftime('%d') on a null value causes problems.
How would I adjust this code to work without raising this warning? I have tried wrapping it in it’s own if block without the strftime call in it, however adding the strftime further in the template just raises the same warning.

This is the code I have thus far:

- alias: TEST
  initial_state: 'on'
  # specify a bunch of times for the check to be done (more than once gives redundancy in the case of server restart/power outage).
  trigger:
    - platform: time
      at:
        - "05:00:00" # Military time format
        - "07:00:00" # Military time format
        - "14:00:00" # Military time format
        - "18:00:00" # Military time format
  # only trigger the automation if it hasn't been triggered already today (or has never been triggered)
  condition:
    - condition: template
      value_template: >-
        {% set dayAutomation = states.automation.test.attributes.last_triggered.strftime('%d') %}
        {% set dayToday = now().strftime('%d') %}
        {% if dayToday != dayAutomation %}
          true
        {% else %}
          false
        {% endif %}
  action:
    - service: notify.mobile_app
      data_template:
        title: "TEST"
        message: "THIS IS A TEST AUTOMATION"

Thank you in advance! :slight_smile:

Isn’t that the same as removing the condition and just having “05:00:00” as the only trigger time?
What could cause it to not trigger at 05:00?

Hi,

As noted in my code:

# specify a bunch of times for the check to be done (more than once gives redundancy in the case of server restart/power outage)

Just in case something along those lines happens! :slight_smile:

So let’s say it runs at 5, then the database is lost, and it runs again. Is that “OK”?

Yes, that is ok :slight_smile:

I can’t even get the error…


The first automation is a new one that has never run.
The second is one that has been run but the template tool returns nothing.
with error I mean the null value or any value…

what version are you using? I’m running 0.115.6

I’m running the same version

It turns out that even though the template comes up with the warning, the automation still triggers in either condition as required.
Thanks for helping look into it :slight_smile:
Here’s the code for anyone who may have the same question in the future:

- alias: TEST
  initial_state: 'on'
  # specify a bunch of times for the check to be done (more than once gives redundancy).
  trigger:
    - platform: time
      at:
        - "05:00:00" # Military time format
        - "07:00:00" # Military time format
        - "14:00:00" # Military time format
        - "18:00:00" # Military time format
  # only trigger the automation if it hasn't been triggered already today (or has never been triggered)
  condition:
    - condition: template
      value_template: >-
        {% if states.automation.test.attributes.last_triggered == none %}
          true
        {% elif now().strftime('%d') != states.automation.test.attributes.last_triggered.strftime('%d') %}
          true
        {% else %}
          false
        {% endif %}
  action:
    - XXXXXXXXXXXXXXXXXXXXX