So I’m trying to create a template sensor to “figure out” what kind of session is currently happening in F1. I am using the F1 HACS integration ( Nicxe/f1_sensor: A Home Assistant Custom Integration that provides information about the next Formula 1 race and the full season calendar). The integration has many useful sensors, but (as of this writing) does not provide a sensor to indicate what type of session is active, so I want to try and ‘guess’ based on other data. Here is the logic I’m trying (and failing) to implement:
Create a sensor.f1_session_type template sensor that behaves as follows based on the state of sensor.f1_session_status:
If sensor.f1_session_status is ended then f1_session_type should be None. Otherwise, set f1_session_type based on the closest (absolute before or after) session start time, which are listed as attributes of sensor.f1_next_race as follows:
now() is closest to sensor.f1_next_race.first_practice_start = “Free Practice 1”
now() is closest to sensor.f1_next_race.second_practice_start = “Free Practice 2”
now() is closest to sensor.f1_next_race.third_practice_start = “Free Practice 3”
now() is closest to sensor.f1_next_race.sprint_qualifying_start = “Sprint Qualifying”
now() is closest to sensor.f1_next_race.sprint_start = “Sprint Race”
now() is closest to sensor.f1_next_race.qualifying_start = “Qualifying”
now() is closest to sensor.f1_next_race.race_start = “Race”
Note, some of those attributes can be “null” based on what session types are happening during a given race weekend.
I haven’t been able to find an elegent (or in-elegant) way to do this in a template sensor script, and hoped one of you experts might take a crack at it?
Thanks in advance!