For me an impossible job. Maybe can help me with this?
Below you see a message that is added to a sensor by imap_content.
Output of the sensor:
Message: "Donderdagse Week van 33-2023\r\n\r\n390757 PM OUDENES\r\n\r\nDatum Dienst Begin Einde Fun Stdp\r\n________ ______ _____ _____ ___ ____\r\nma 14-08 6031 09:30 17:30 NRP NVT\r\ndi 15-08 R \r\nwo 16-08 6002 07:00 15:00 NRP NVT\r\ndo 17-08 6002 07:00 15:00 NRP NVT\r\nvr 18-08 * -- \r\nza 19-08 R \r\nzo 20-08 R"
friendly_name: Imap full info sensor
Here easy readable way:
Donderdagse Week van 33-2023
390757 PM OUDENES
Datum Dienst Begin Einde Fun Stdp
________ ______ _____ _____ ___ ____
ma 14-08 6031 09:30 17:30 NRP NVT
di 15-08 R
wo 16-08 6002 07:00 15:00 NRP NVT
do 17-08 6002 07:00 15:00 NRP NVT
vr 18-08 * --
za 19-08 R
zo 20-08 R
- trigger:
- platform: event
event_type: "imap_content"
id: "custom_event"
event_data:
sender: !secret imap_sender_2
sensor:
- name: "Imap full info sensor"
state: >-
{% if 'DiSys' in trigger.event.data["subject"] %}
Nieuwe DW
{% endif %}
attributes:
Message: "{{ trigger.event.data['text'] }}"
I want extract the week and use the into to add a event in a local calendar.
Given example will be than:
Every week I receive this overview and working and rest days are different also start and end times.
Als the first line change every week. Week number change also.
Can someone code this into automation? Im really grateful if this can be possible…
Asked ChatGTP to create something and gave me this:
Tried it. Didn’t work. If this was working only i needed to do was add the info from a sensor into the text part inside the script.
# configuration.yaml (of een vergelijkbaar bestand)
sensor:
- platform: command_line
name: planning_parser
command: "python3 /config/parse_planning.py" # Het pad naar het Python-script
# parse_planning.py (plaats dit script in de configuratiedirectory van Home Assistant)
import re
from datetime import datetime
from dateutil.parser import parse
import json
text = """
Donderdagse Week van 33-2023
390757 PM OUDENES
Datum Dienst Begin Einde Fun Stdp
________ ______ _____ _____ ___ ____
ma 14-08 6031 09:30 17:30 NRP NVT
di 15-08 R
wo 16-08 6002 07:00 15:00 NRP NVT
do 17-08 6002 07:00 15:00 NRP NVT
vr 18-08 * --
za 19-08 R
zo 20-08 R
"""
lines = text.split('\n')
data = []
for line in lines:
if re.match(r'^[a-z]{2}\s\d{2}-\d{2}', line): # Lijn bevat een datum en dienst
parts = line.split()
date_str = f"{datetime.now().year}-{parts[1]} {parts[0]}"
date = parse(date_str)
service = parts[2]
begin_time = parts[3]
end_time = parts[4]
function = parts[5]
data.append({
"date": date.strftime("%Y-%m-%d"),
"service": service,
"begin_time": begin_time,
"end_time": end_time,
"function": function
})
print(json.dumps(data))
2023-08-12 19:17:56.488 ERROR (MainThread) [homeassistant.components.automation.export_dw_to_calendar] Export DW to Calendar: Repeat at step 2: If at step 1: Error executing script. Invalid data for call_service at pos 1: Could not parse date for dictionary value @ data['start_date']
2023-08-12 19:17:56.491 ERROR (MainThread) [homeassistant.components.automation.export_dw_to_calendar] Export DW to Calendar: Repeat at step 2: Error executing script. Invalid data for if at pos 1: Could not parse date for dictionary value @ data['start_date']
2023-08-12 19:17:56.495 ERROR (MainThread) [homeassistant.components.automation.export_dw_to_calendar] Export DW to Calendar: Error executing script. Invalid data for repeat at pos 2: Could not parse date for dictionary value @ data['start_date']
2023-08-12 19:17:56.503 ERROR (MainThread) [homeassistant.components.automation.export_dw_to_calendar] Error while executing automation automation.export_dw_to_calendar: Could not parse date for dictionary value @ data['start_date']
It seems to be an issue with the calendar.create_event service and date-only inputs… I altered the automation above to create and use date time strings for all the events.
Only the Rest is added to calendar the rest isn’t. Can this because its not “2023-14-08 09:30:00” with :00 at the end?
2023-08-12 20:30:14.429 ERROR (MainThread) [homeassistant.components.automation.export_dw_to_calendar] Export DW to Calendar: Repeat at step 2: Error executing script. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data['start_date_time']
2023-08-12 20:30:14.435 ERROR (MainThread) [homeassistant.components.automation.export_dw_to_calendar] Export DW to Calendar: Error executing script. Invalid data for repeat at pos 2: not a valid value for dictionary value @ data['start_date_time']
2023-08-12 20:30:14.442 ERROR (MainThread) [homeassistant.components.automation.export_dw_to_calendar] Error while executing automation automation.export_dw_to_calendar: not a valid value for dictionary value @ data['start_date_time']
I think I found the issue, the “Rest” entries were using “YYYY-MM-DD” date format but the others were using “YYYY-DD-MM”… I’ve adjusted it above, and tested it on my instance and the calendar populates as expected. Hopefully your results are the same.
Yes, that did the work. Thank you so much. For me a good reason to try to learn this myself. Can do a lot but if it comes to this kind of complex coding im lost… haha