I have a trigger for electricity use if >4000 then I need to store the start time.
Then when it stops I want to use the start and stop time for a google calendar event.
Use an input datetime.
- service: input_datetime.set_datetime
data:
datetime: "{{ now() }}"
target:
entity_id: input_datetime.electricity_start_time # you need to create this.
If it happens pretty quickly then you can probably just use a threshold sensor. Set upper to 4000 and then do this:
trigger:
platform: state
entity_id: binary_sensor.electricity_threshold
from: 'on'
to: 'off'
action:
service: google.add_event
data:
calendar_id: "*****@group.calendar.google.com"
summary: Electricity Stuff!
start_date_time: "{{ trigger.from_state.last_changed.strftime('%Y-%m-%d %H:%M:%S') }}"
end_date_time: "{{ trigger.to_state.last_changed.strftime('%Y-%m-%d %H:%M:%S') }}"
Since a threshold sensor only changes when its watched sensor crosses the threshold by going from the previous last changed to the current last changed you have the time of your electricity event.
One caveat - last_changed doesn’t survive restarts (hence my “if it happens quickly” at the top). So if these events last a long time this may not work. But if restart is a risk then the input_datetime approach isn’t totally safe either. If electricity goes back below 4000 during a restart then you’ll miss recording an event. And if it goes above 4000 during a restart then you’ll record a totally bogus event (start time will be the start time of the previous electricity event). So something to keep in mind either way.
I am trying to set this up as an action in an automation.
I create an attribute Microwave_On as a date/time stamp.
Then I had to select action->condition
then State
Entrity
Microwave_on
attribute timestamp
state now()