Hi everyone.Not sure if this is the right place here. I’m currently working on an epaper display with esphome showing some weather data, washing machine state, garbage days and calendar events. Now I got a little bit of stuck, because I’m new to HomeAssistant, also new to python, new to yaml (and also basically new to c++). I have only some experience in some other programming languages some time ago.
Long topic and 3 questions waiting for an answer.
So, the ePaper itself is not the problem. Working great. My issue is the preparation of the data in the template.yaml for the sensor attributes, which are send to the esp. Here are the code snippets, how it works now:
- service: calendar.get_events
target:
entity_id:
- calendar.fxx
- calendar.kxx
- calendar.jxx
- calendar.exx
data:
start_date_time: "{{ now().strftime('%Y-%m-%d 00:00:00') }}"
end_date_time: "{{ (now() + timedelta(days=14)).strftime('%Y-%m-%d 00:00:00') }}"
response_variable: calendar_events
and this is, how the attributes are created:
event_count: >
{% set all_events = calendar_events['calendar.fxx'].events + calendar_events['calendar.kxx'].events + calendar_events['calendar.jxx'].events + calendar_events['calendar.exx'].events %}
{{ all_events | count() }}
event_summary1: >
{% set all_events = calendar_events['calendar.fxx'].events + calendar_events['calendar.kxx'].events + calendar_events['calendar.jxx'].events + calendar_events['calendar.exx'].events %}
{% if (all_events | count()) >0 %}
{{ (all_events|sort(attribute='start'))[0].summary }}
{% endif %}
event_startdate1: >
{% set all_events = calendar_events['calendar.fxx'].events + calendar_events['calendar.kxx'].events + calendar_events['calendar.jxx'].events + calendar_events['calendar.exx'].events %}
{% if (all_events | count()) >0 %}
{{ ((all_events|sort(attribute='start'))[0].start|as_datetime|as_local).strftime("%d.%m.%Y") }}
{% endif %}
event_starttime1: >
{% set all_events = calendar_events['calendar.fxx'].events + calendar_events['calendar.kxx'].events + calendar_events['calendar.jxx'].events + calendar_events['calendar.exx'].events %}
{% if (all_events | count()) >0 %}
{{ ((all_events|sort(attribute='start'))[0].start|as_datetime|as_local).strftime("%H:%M") }}
{% endif %}
event_tag1: >
{% set all_events = calendar_events['calendar.fxx'].events + calendar_events['calendar.kxx'].events + calendar_events['calendar.jxx'].events + calendar_events['calendar.exx'].events %}
{% if (all_events | count()) >0 %}
{% set wochentag = ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"] %}
{% if ((all_events|sort(attribute='start'))[0].start|as_datetime|as_local).strftime("%H:%M") == "00:00" %}
{% set calday = as_timestamp((all_events|sort(attribute='start'))[0].start|as_datetime|as_local) | timestamp_custom('%w',false) | int %}
{% else %}
{% set calday = as_timestamp((all_events|sort(attribute='start'))[0].start|as_datetime|as_local) | timestamp_custom('%w',false) | int -1 %}
{% endif %}
{{ wochentag[calday] }}
{% endif %}
event_summary2: >
{% set all_events = calendar_events['calendar.fxx'].events + calendar_events['calendar.kxx'].events + calendar_events['calendar.jxx'].events + calendar_events['calendar.exx'].events %}
{% if (all_events | count()) >1 %}
{{ (all_events|sort(attribute='start'))[1].summary }}
{% endif %}
-
As you can see, in every attribute there are some procedures repeated again and again for each attribute. Eg. adding the events togther to get them sorted. This can’t be the way to do it. There has to be a way to do that somewhere above in the yaml file and then just access the variable again and again in each attribute. Due to my missing experience in Python an yaml, I didn’t find a way to do this. I can’t access a variable which is set in another attribute before. How to do this? Were to place the code in the yaml file?
-
I add the events in nr. 1 together in one big array of events to be able to sort them by the startdate. I am not sure, wether this is the best way to do it, but the bigger problem of this is, that the information from which calendar/person the event is from gets lost. Is there a way to add a new field to each event with the name like in a for-loop before all the events are added together?
-
Which book or source of knowledge to use to learn Python and the Home Assistant Yaml the right way?
Thx and best regards