Using time that an automation was triggered inside the automation as part of a condition

Background: I have an automation that should trigger every other minute between 7:30pm and 8:00pm. I have this part working by triggering every 2 minutes (/2) of 7pm then “and if” after 7:29pm.

Next I’d like to test the value of a sensor against a value that varies based on when the trigger happens. So, at 7:30 the “and if” condition is sensor value > 130 - minute_time_of_trigger. I’m thinking this might work for me ‘{{ sensor > 130 - strptime(now(), “%M”) }}’

What I would like to know: is there is a way to access the actual time that the automation was triggered within a condition rather than using now() ?

Use the last_triggered attribute.

Available Trigger Data - Time Pattern Trigger

If you are using a Time or Time Pattern trigger, trigger.now holds the datetime object of when the trigger fired.

FWIW, a Time trigger with discrete times is probably more efficient than using Time Pattern:

Time trigger every two minutes from 7:30-8:00pm
trigger: time
at:
  - "19:30"
  - "19:32"
  - "19:34"
  - "19:36"
  - "19:38"
  - "19:40"
  - "19:42"
  - "19:44"
  - "19:46"
  - "19:48"
  - "19:50"
  - "19:52"
  - "19:54"
  - "19:56"
  - "19:58"
  - "20:00"

I think you mean now().strftime('%M')… but that returns a string. Using the minute method would be better…

{{ states('sensor.example') | int(0) > 130 - trigger.now.minute }}

Are you sure there is no better way to trigger the automation?
Time pattern is the last way in most automations.

Can you post what you have or what you are expecting?
Preferably with entity names.

Thanks for all your help & suggestions.

Here’s what I’m doing now:

### Trigger every 2 minutes of the 19th hour

### and if the time is after 7:29 PM and before 7:57 PM and the day is Monday, Tuesday, Wednesday, Thursday, or Friday

### and if template:
{{ int(states('sensor.envoy_XXXXXXXXXXXX_battery')) > 130-state_attr('automation.solar_evening_dg_activate', 'last_triggered').minute }}
### the battery item is a simple percentage (0-100% of battery charge)
### this state_attr is referring to itself (this automation)
...
load new enphase tariff to export battery to grid
...
 send persistent message: >-
    Activated at \:{{ state_attr('automation.solar_evening_dg_activate',
    'last_triggered').minute }} with {{
    int(states('sensor.envoy_XXXXXXXXXX_battery')) }}\% left.

The idea is to export battery power to the grid starting near the end of my peak time, working forwards from 7:30pm with formula that varies by time of day and available battery % to estimate if we have enough battery to both export to grid and run the home load until 8pm

This is for winter electric rates where peak $$ is about 22% higher than off-peak. During summer, peak $$ is 81% higher than off-peak, so I will adjust the formula to supply more battery to the grid, by starting sooner to enable longer discharge to grid, and also allow more of battery to be used, while still saving some for home load and emergency usage.

I have yet to try trigger.now.minute, will try that instead of named reference to automation that is self.

Yes, I can consider inverting the trigger and "and if"s so that the formula is the main trigger, and timing is secondary (and if).