Issue with an automation giving an error when HA starts

Hello

I need your help as I am not finding what’s wrong with the following automations. This first one is not giving an error in home-assistant.log while the second one does (if I am commenting the second automation, the error is gone)… I do not know why … Bug in HA 0.117.4 ?

- alias: send_email_temp_unknown_m
  initial_state: true
  trigger:
    - platform: template
      value_template: '{{ (as_timestamp(states.sensor.h_temp_piscine_m_corrected.state)) < (((as_timestamp(now())) | int) - 1800) }}'
  condition:
    - condition: state
      entity_id: input_boolean.pool_temp_lost_m
      state: 'off'
  action:
    - service: input_boolean.turn_on
      data:
        entity_id: input_boolean.pool_temp_lost_m
    - service: notify.message_ha
      data_template:
        message: '{{ now().strftime("%Y-%m-%d %H:%M:%S   ") }}**** DM-008.1 Température piscine Montréal inconnue... Sending Email to alarm'
    - service: notify.email_to_alarm
      data:
        title: '** HA - Température Piscine Montréal inconnue **'
        message: '**** DM-M001.1  {{ states.sensor.date.state }} {{states.sensor.time.state}} ****'

#=================================================================================================
#
# Automation for MONTREAL
# send Email si temperature piscine de nouveau connue
#

- alias: send_email_temp_known_m
  initial_state: true
  trigger:
    - platform: template
      value_template: '{{ (as_timestamp(states.sensor.h_temp_piscine_m_corrected.state)) > (((as_timestamp(now())) | int) - 1800) }}'
  condition:
    - condition: state
      entity_id: input_boolean.pool_temp_lost_m
      state: 'on'
  action:
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.pool_temp_lost_m
    - service: notify.message_ha
      data_template:
        message: '{{ now().strftime("%Y-%m-%d %H:%M:%S   ") }}**** DM-009.1 Température piscine Montréal de nouveau connue... Sending Email to alarm'
    - service: notify.email_to_alarm
      data:
        title: '** HA - Température Piscine Montréal de nouveau connue **'
        message: '**** DM-M002.1  {{ states.sensor.date.state }} {{states.sensor.time.state}} ****'

Here is the error when HA starts (no error after that):

2020-11-05 13:02:00 ERROR (MainThread) [homeassistant] Error doing job: Exception in callback async_track_utc_time_change..pattern_time_change_listener() at /srv/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/event.py:1344
Traceback (most recent call last):
File “/usr/local/lib/python3.8/asyncio/events.py”, line 81, in _run
self._context.run(self._callback, *self._args)
File “/srv/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/event.py”, line 1350, in pattern_time_change_listener
hass.async_run_hass_job(job, dt_util.as_local(now) if local else now)
File “/srv/homeassistant/lib/python3.8/site-packages/homeassistant/core.py”, line 434, in async_run_hass_job
hassjob.target(*args)
File “/srv/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/event.py”, line 812, in _refresh_from_time
self._refresh(None, track_templates=track_templates)
File “/srv/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/event.py”, line 961, in _refresh
self.hass.async_run_hass_job(self._job, event, updates)
File “/srv/homeassistant/lib/python3.8/site-packages/homeassistant/core.py”, line 434, in async_run_hass_job
hassjob.target(*args)
File “/srv/homeassistant/lib/python3.8/site-packages/homeassistant/components/template/trigger.py”, line 55, in template_listener
entity_id = event.data.get(“entity_id”)
AttributeError: ‘NoneType’ object has no attribute ‘data’

See the warning here: https://www.home-assistant.io/docs/configuration/templating/#states

1 Like

Thank you @tom_l … So I changed the two automations to take into account the warning you referred to but the error remains at startup… Second automation is now:

#=================================================================================================
#
# Automation for MONTREAL
# send Email si temperature piscine de nouveau connue
#

- alias: send_email_temp_known_m
  initial_state: true
  trigger:
    - platform: template
      value_template: "{{ (as_timestamp(states('sensor.h_temp_piscine_m_corrected'))) > (((as_timestamp(now())) | int) - 1800) }}"
  condition:
    - condition: state
      entity_id: input_boolean.pool_temp_lost_m
      state: 'on'
  action:
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.pool_temp_lost_m
    - service: notify.message_ha
      data_template:
        message: '{{ now().strftime("%Y-%m-%d %H:%M:%S   ") }}**** DM-009.1 Température piscine Montréal de nouveau connue... Sending Email to alarm'
    - service: notify.email_to_alarm
      data:
        title: '** HA - Température Piscine Montréal de nouveau connue **'
        message: "**** DM-M002.1  {{ states('sensor.date') }} {{states('sensor.time')}} ****"

Put this in your template editor and tell me what it returns:

{{ (as_timestamp(states.sensor.h_temp_piscine_m_corrected.state)) }}

I suspect what you really want is the last_changed property.

{{ (as_timestamp(states.sensor.h_temp_piscine_m_corrected.last_changed)) }}

state: 1604597520.0
last changed: 1604597901.653089

Huh. Ok. When I tried the first template with one of my sensors I got : None.

OK… but the entity is a date and normally changing every 20 minutes…
example of states.sensor.h_temp_piscine_m_corrected.state: “2020-11-05 18:39”

in the trigger, the template is identical for the two automations except the “>” and “<” but I only have one error for the second automation…

1 Like

That would explain it. I’m out of ideas.

I have no idea neither… I changed the automation as a work around to avoid the error…
The automation is like this now :

#=================================================================================================
#
# Automation for MONTREAL
# send Email si temperature piscine de nouveau connue
#

- alias: send_email_temp_known_m
  initial_state: true
  trigger:
    - platform: time_pattern
      minutes: '/5'
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: "{{ (as_timestamp(states('sensor.h_temp_piscine_m_corrected'))) > (((as_timestamp(now())) | int) - 1800) }}"
      - condition: state
        entity_id: input_boolean.pool_temp_lost_m
        state: 'on'
  action:
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.pool_temp_lost_m
    - service: notify.message_ha
      data_template:
        message: '{{ now().strftime("%Y-%m-%d %H:%M:%S   ") }}**** DM-009.1 Température piscine Montréal de nouveau connue... Sending Email to alarm'
    - service: notify.email_to_alarm
      data:
        title: '** HA - Température Piscine Montréal de nouveau connue **'
        message: "**** DM-M002.1  {{ states('sensor.date') }} {{states('sensor.time')}} ****"