State attributes (timestamps) to calendar

I’m using the HACS B-Hyve integration which creates a switch entity with a list of timestamps for future watering. Does anyone know how to create an automation to store these timestamps in a Home Assistant Calendar? I’d like to get notified before the sprinker goes off.

You can use a template to assign the list of times in a Repeat for Each action in a automation so that a Calendar event is created for each one.

What would be an appropriate trigger for your use case?

Is that schedule set in stone or will the integration change those values? There isn’t currently a service to delete calendar events, so if the schedule isn’t set you might end up with a number of false notifications.

Well, I was hoping to get notified about 8 hours prior to the sprinker going off to ensure my grill and other items are covered. Also, B-Hyve sometimes triggers a rain delay which would remove some of those timestamps, so it seems that I would need to change/delete those event dynamically.

How would that template look like in an automation to crate the events?

Why not set up a template trigger for 8 hours before the next time in the list?

trigger:
  - platform: template
    value_template: |
      {{ now() >= (state_attr('switch.YOUR_SWITCH', 'program_e').watering_program
      | reject('lt', now()) | sort | first - timedelta(hours=8)) }}
condition: []
action:
  - service: notify.YOUR_NOTIFIER
    data:
      title: Sprinkler Action Planned
      message: The sprinklers will run in 8 hours. Make sure everything is covered.

EDIT: Removed map() function based on OP’s responses below

That’s exactly what I wanted to do. I did try to test that template in dev tools and got this error. Thoughts?

UndefinedError: No first item, sequence was empty.

In the Template editor tool, make sure the following returns the list of datetime strings:

{{ state_attr('switch.YOUR_SWITCH', 'program_e').watering_program }}

In case it isn’t obvious, make sure to use your actual entity ID not switch.YOUR_SWITCH

Here are the results…

Great… the issue is just that the values are already datetime objects, not strings as I assumed. All you should need to do is remove the | map('as_datetime') from the full template. I will correct it in my earlier post.

So it turns out that the B-Hyve integration actually has a script that creates a sensor for the ‘next watering’ with the timestamp state. I was then able to apply your template to that sensor. Works great! Thanks much!

{{ now() >= states("sensor.back_yard_next_watering") | as_datetime - timedelta(hours=8) }}