Hi Martijn,
I’m sorry I’m so on and off this thread. I’ve probably got a bit to much going on
Last time you suggested to make a trigger based sensor which is indeed a good idea. So to realize this I made some sensors to do this. The bellow code works, it successfully charged the car for a week now. But I’ve got a bit of unexpected behavior. Let’s start with some data from last night.
(All ‘long bars’ are continues, I verified that there are no tiny changes by looking at the start and end times)
- Status description is an output from the charger
ev_plugged_in
is on when the car is plugged inev_charge_duration
calculates how long we need to charge till 100% (not in screenshot)ev_charge_task
lets me define different scenarios/tasks to decide what to do. If there is not enough time to fully charge the car I don’t want to calculate the start time but just continue charging the car after it got plugged in. (Eventually I want to expand this with more options)ev_charge_planned_start_time
holds the start time. Only if the charge task has determined that I can do a planned charging session.
Next to these sensors I have two automations.
- Pause charging if
ev_plugged_in = true
for 1 minute andev_charger_task = "planned"
- Start charging if current time matches
ev_charge_planned_start_time
Below are the templates for these sensors. As you can see in the above screenshot ev_charge_planned_start_time
is changing during charging. Based on my templates I would expect ev_charge_planned_start_time
only to get calculated when plugging in the car.
I expect a change of ev_charger_task
to get triggered only once after plugging in the car. Because I have not specified a “to” in the trigger it technically could trigger on an attribute change, but ev_plugged_in
doesn’t have changing attributes (as far as I can see only “friendly_name”). If it indeed does only change once it should also only trigger a change of ev_charge_planned_start_time
once.
I’ve been googling/searching for a few nights now, but I can’t figure out why ev_charge_planned_start_time
changes during charging. Am I missing something in my templates or could this be something in the macro itself? If you have any idea for a direction to search in that would be greatly appreciated. (But if you don’t I obviously still appreciate your work on this macro and this thread! )
template:
- binary_sensor:
- name: EV plugged in
unique_id: ev_plugged_in
state: >
{{ not is_state('sensor.wallbox_pulsarplus_sn_xxxxxx_status_description', 'Ready') }}
- sensor:
- name: EV charge duration
unique_id: ev_charge_duration
state: >
{% set charge = states('sensor.leaf1emie_battery') | float %}
{{ ((100-charge)/13) * 60 }}
- trigger:
- platform: state
entity_id: binary_sensor.ev_plugged_in
- sensor:
- name: EV charge task
unique_id: ev_charger_task
state: >
{% set finish_time = today_at('7:00') + timedelta(days=1 if now() > today_at('7:00') else 0) %}
{% set time_left = ((finish_time - now()).total_seconds())/60 | float %}
{% set time_needed = states('sensor.ev_charge_duration') | float %}
{% if states('binary_sensor.ev_plugged_in') == "on" %}
{% if time_left - time_needed > 0 %}
Planned
{% else %}
Immediate
{% endif %}
{% else %}
Waiting
{% endif %}
- trigger:
- platform: state
entity_id: sensor.ev_charge_task
to: "Planned"
- sensor:
- name: EV charge planned start time
unique_id: ev_charge_planned_start_time
device_class: timestamp
state: >
{% if states('sensor.ev_charge_task') == "Planned" %}
{% set hours = states('sensor.ev_charge_duration') | float %}
{% set end = today_at('7:00') + timedelta(days=1 if now() > today_at('7:00') else 0) %}
{% from "cheapest_energy_hours.jinja" import cheapest_energy_hours %}
{{ cheapest_energy_hours(sensor='sensor.zonneplan_current_electricity_tariff', attr_all='forecast', value_key="electricity_price", hours=hours/60, start=now(), end=end, mode='start')}}
{% else %}
NA
{% endif %}