alias: Calendar Export Monthly
description: ""
triggers:
- trigger: calendar
entity_id: calendar.system_automation
event: start
offset: "0:0:0"
conditions:
- condition: state
entity_id: calendar.system_automation
attribute: message
state: "Export: Calendar"
actions:
- sequence:
- variables:
endts: "{{ as_timestamp(states('sensor.date_end_last_month'))|float }}"
enddate: "{{ as_datetime(endts + 86400).strftime('%Y-%m-%d') }}"
startts: "{{ as_timestamp(as_datetime(endts).strftime('%Y-%m-01'))|float }}"
startdate: "{{ as_datetime(startts).strftime('%Y-%m-%d') }}"
days: "{{ ((endts - startts)/86400)+1|int }}"
- repeat:
sequence:
- variables:
index: "{{ repeat.index|int -1}}"
day_offset: "{{ index * 86400 }}"
dt: >-
{{ as_datetime(startts + day_offset).strftime('%A %d-%B-%Y')
}}
count: "{{ days|int }}"
mode: single
FYI, this didn’t get hits because you put it into development, which is a section about writing code for HA core. It’s not a section about writing automations. I’m going to update the tags to the proper section to get more hits from other users.
On to your problem, please verify that the index is actually starting at 12. You can do this by modifying the code this way:
alias: Calendar Export Monthly
description: ""
triggers:
- trigger: calendar
entity_id: calendar.system_automation
event: start
offset: "0:0:0"
conditions:
- condition: state
entity_id: calendar.system_automation
attribute: message
state: "Export: Calendar"
actions:
- sequence:
- variables:
endts: "{{ as_timestamp(states('sensor.date_end_last_month'))|float }}"
enddate: "{{ as_datetime(endts + 86400).strftime('%Y-%m-%d') }}"
startts: "{{ as_timestamp(as_datetime(endts).strftime('%Y-%m-01'))|float }}"
startdate: "{{ as_datetime(startts).strftime('%Y-%m-%d') }}"
days: "{{ ((endts - startts)/86400)+1|int }}"
- repeat:
sequence:
- variables:
index: "{{ repeat.index|int -1}}"
day_offset: "{{ index * 86400 }}"
dt: >-
{{ as_datetime(startts + day_offset).strftime('%A %d-%B-%Y')
}}
- action: persistent_notification.create
data:
message: Repeat Index {{ repeat.index }}
count: "{{ days|int }}"
mode: single
Now your automation can also be optimized, there’s a couple ways to remove some of the math.
- Use as_datetime & as_local over using as_timestamp. It’s easier for math.
- Use timedeltas to offset datetime objects
- Variables assign types to items they resolve, there’s no reason to cast things with
| int
or|float
if the result is coming from a variable or the repeat object. The type will already be converted. The only caveat to this rule isstates()
are always strings and you must cast them.
alias: Calendar Export Monthly
description: ""
triggers:
- trigger: calendar
entity_id: calendar.system_automation
event: start
offset: "0:0:0"
conditions:
- condition: state
entity_id: calendar.system_automation
attribute: message
state: "Export: Calendar"
actions:
- sequence:
- variables:
end: >
{{ (states('sensor.date_end_last_month') | as_datetime | as_local).isoformat() }}
start: >
{{ (end | as_datetime).replace(day=1).isoformat() }}
days: >
{{ (end | as_datetime).day }}
- repeat:
sequence:
- variables:
date: >-
{{ (start | as_datetime + timedelta(days=repeat.index)).strftime('%A %d-%B-%Y') }}
count: "{{ days }}"
mode: single
Lastly, if you’re really struggling with repeat.index you can completely skip it by generating your list ahead of time in jinja and using a for_each repeat.
alias: Calendar Export Monthly
description: ""
triggers:
- trigger: calendar
entity_id: calendar.system_automation
event: start
offset: "0:0:0"
conditions:
- condition: state
entity_id: calendar.system_automation
attribute: message
state: "Export: Calendar"
actions:
- sequence:
- variables:
dates: >
{% set end = states('sensor.date_end_last_month') | as_datetime | as_local %}
{% set start = end.replace(day=1) %}
[{% for i in range((end - start).days + 1) %}
"{{ (start + timedelta(days=i)).isoformat() }}",
{% endfor %}]
- repeat:
sequence:
- variables:
date: >-
{{ (repeat.item | as_datetime).strftime('%A %d-%B-%Y') }}
for_each: "{{ dates }}"
mode: single