Help with Automation: Error rendering data template: TypeError: argument of type 'NoneType'

Hi,
my alarm automation throws error in the log when i disable the alarm from the Smartphone.
If Alarm is turned off the attributes becomes nothing so homeassistant throws an error.

Could anyone help to solve this please ?

- alias: Wecker - Set Alarm Input Helper
  trigger:
    - platform: state
      entity_id: sensor.martin_smartphone_next_alarm
  action:
    - service: input_datetime.set_datetime
      data_template:
        entity_id: input_datetime.wakeup_martin_smartphone
        datetime: >-
          {% if 'com.sec.android.app.clockpackage' in (state_attr('sensor.martin_smartphone_next_alarm', 'Package' )) %} {{
            states('sensor.martin_smartphone_next_alarm') |as_datetime |as_local }} {% endif %}
 
  mode: restart

Error in Log:

Logger: homeassistant.components.automation.wecker_set_alarm_input_helper
Source: components/automation/init.py:514
Integration: Automatisierung (documentation, issues)
First occurred: 11:49:26 (2 occurrences)
Last logged: 11:49:57

Error while executing automation automation.wecker_set_alarm_input_helper: Error rendering data template: TypeError: argument of type 'NoneType' is not iterable
Error while executing automation automation.wecker_set_alarm_input_helper: Invalid datetime specified: for dictionary value @ data['datetime']

Thanks in advance :wink:

you aren’t providing an else. So when ‘com.sec.android.app.clockpackage’ is not in state_attr(‘sensor.martin_smartphone_next_alarm’, ‘Package’ ), the service executes with None as the datetime: result and the service errors.

You should use choose if you don’t plan on firing the service when your if statement is not met. That way, there won’t be a service and you won’t get the error.

Secondly, data_template is deprecated, just use data. The requirement for data_template is a thing of the past, it’s no longer required.

Thanks petro.

Could you provide a sample how it should look?
I’m a total beginner, sorry.

Take a look at the choose examples