Count all events on calendar for next upcoming workday, and fudging response variables for the Template Editor

What I’d like

I have a local calendar—let’s call it “Work Sessions”—which contains one event for each client I will see that day. I also have a Workday sensor, binary_sensor.spencer_workday that defines the days of the week I work (or, rather, lists them as an attribute).

I would like to script three different responses that can be used in TTS or conversation responses:

  1. Clients on next workday: “Next Tuesday, you have 3 clients scheduled.”
  2. Detailed agenda today: “Today, you have 3 clients scheduled, starting at 10am and ending at 3pm.”
  3. Week summary: “You have a total of 6 clients scheduled this week, on Tuesday and Thursday.”

When the script is built, I’m hoping not to permanently store the response variable data in sensors, because that’s a lot of data that I only really need to reference at runtime.

I’m also trying to avoid hardcoding days of the week when possible, for the sake of future flexibility—this is why I’m trying to get workdays from the Workday sensor’s attributes instead of just declaring in the script or template that I want data for Tuesdays and Thursdays.

The issue I’m running into

Formulating any of this would require tinkering with Jinja filters. I am not nearly practiced enough at manipulating response data from the calendar.get_events action or using Jinja filters to know how to do this offhand, which means I want to rely on the Template Editor to preview and build this template.

But the calendar data is delivered as a response variable, and I don’t believe there’s any way to call the calendar.get_events action to get that variable in the Template Editor. I’m flying blind.

I know I can call calendar.get_events from the Developer Tools settings, and get a response. That response would be perfectly suitable as dummy content for the purposes of troubleshooting; I don’t need the content to actually be up-to-date while I’m writing the template, I just need it to be accurate to the form and structure. But I don’t know how to properly define the hardcoded variable using that response data.

My asks

I could use help in either of these ways:

  1. Can you suggest how I can store the response variable so I can use it in the Template Editor? I assume this will look like storing it in a template sensor—but I’d love a direct example of how to write that template. I’m also open to just storing a “dummy” version of the data in a manually defined variable in the editor, as mentioned above, but I don’t know quite how to take the response I get from the actions dev tool and properly store it in a variable.
  2. If someone is feeling particularly generous, I’d love a nudge suggesting how to write the Jinja2 to do what I’ve described above. Even just walking me verbosely through the steps of the filters would be a big help, since I’m only barely grasping what data the returned variable will contain and how to manipulate it to get what I want.

These are the two biggest thorns; I’m fairly confident that I can start piecing things together if I can just figure out how to make some data accessible for use in the Template Editor.

Thanks in advance.

You can use the Action tool in Developer tools to call calendar.get_events… at the bottom of the “Response” section there will a button “Copy to Clipbord (Template)” which will save the response variable in a format that can be pasted directly into the Template Editor tool. The default variable name for the object is action_response.

1 Like

Ah, perfect! Thanks, Drew.

Is the workday sensor created by the workday integration? That would allow you to use the workday.check_date action to find the next workday.

If you’re using a custom template sensor instead, you’ll have to describe in detail what the attributes are. Is it a single attribute with a list of strings that are weekday names ? Or perhaps a list of Python Datetime objects? How long is the list and how is it ordered? Etc…

And for your wish list, it would be better if you could rephrase it into terms that make sense in the HA world that could actually be coded. Here is my attempt at interpreting what you want:

  • Clients on next workday: Count of events on next workday
  • Detailed agenda today: Count of events today, and earliest start time, and latest end time
  • Week summary: Use a week that starts Monday. If today is Monday thru Friday summarize the current week. If today is Saturday thru Sunday summarize the next week. The summary is: Count of events in the 7 day week (Mon-Sun), not taking into account wether they are workdays or non-workdays. Determine the first day (Monday=earliest) and the last day (Sun=Latest) that any event occurs.

Does that sound like what you want?

1 Like

Is the workday sensor even necessary? Does the "Work Sessions” calendar have events on non-workdays?

2 Likes

Good call, I was getting tunnel vision and failed to recognize this. Thanks.

I guess that depends what response you want for the next workday response (response #1). If the next workday is Tuesday, and you don’t have clients that day, do you want the response to be “you have no clients on Tuesday” or do you want the response to be “you have 2 clients on Wednesday”?

If you want the former, then you’ll need to know which day is a workday.

2 Likes