-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_sensor-template.yaml
51 lines (49 loc) · 2.65 KB
/
binary_sensor-template.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
template:
- binary_sensor:
- name: Charge or not
state: >-
{% if (states.sensor.average_available_ampere_on_grid_over_last_4_hours.state|int(default=0) * 3 * 230 / 1000) >= 3 %}
{% set charge_kw = (states.sensor.average_available_ampere_on_grid_over_last_4_hours.state|int(default=0) * 3 * 230 / 1000) %}
{% else %}
{% set charge_kw = 3 %}
{% endif %}
{% set battery_kwh = 69 %}
{% set charge_limit = states.sensor.tesla_charge_limit_soc.state|int(default=0) %}
{% set current_soc = states.sensor.tesla_battery_level.state|int(default=0) %}
{% set location = states.sensor.tesla_geofence.state %}
{% set current_hour = (now()|string)|as_timestamp | timestamp_custom("%H", True, "%H") | int(default=0) %}
{% set finish_hour = 10 %}
{% set cable_status = states.binary_sensor.tesla_plugged_in.state %}
{% set charge_hours = (( charge_limit - current_soc ) / 100 * battery_kwh / charge_kw)|round(0,'ceil')|int(default=0) %}
{% if (charge_hours != 0 and location == "Landet" and cable_status == "on") %}
{% if current_hour < finish_hour %}
{%- set data =
state_attr('sensor.nordpool_kwh_se3_sek_3_10_025', 'raw_today')[:10]|selectattr('start', '>=', ((now()-timedelta(hours=1))))|sort(attribute='value') +
state_attr('sensor.nordpool_kwh_se3_sek_3_10_025', 'raw_tomorrow')[:8]|rejectattr('value', 'eq', None)|list|sort(attribute='value')
%}
{% else %}
{%- set data =
state_attr('sensor.nordpool_kwh_se3_sek_3_10_025', 'raw_today')|selectattr('start', '>=', ((now()-timedelta(hours=1))))|sort(attribute='value') +
state_attr('sensor.nordpool_kwh_se3_sek_3_10_025', 'raw_tomorrow')[:8]|rejectattr('value', 'eq', None)|list|sort(attribute='value')
%}
{% endif %}
{%- set l=data|sort(attribute='value') %}
{%- set ns = namespace(status=[]) %}
{%- if l|list|count < charge_hours %}
{%- set range_hours = l|list|count %}
{%- else %}
{%- set range_hours = charge_hours %}
{%- endif %}
{%- for i in range(range_hours) %}
{% if l[i].start|string|as_timestamp | timestamp_custom("%Y-%m-%d %H", True, "%Y-%m-%d %H") == (now()|string)|as_timestamp | timestamp_custom("%Y-%m-%d %H", True, "%Y-%m-%d %H") %}
{%- set ns.status = "yes" %}
{%- endif %}
{%- endfor %}
{% if ns.status == "yes" %}
True
{% else %}
False
{% endif %}
{% else %}
False
{% endif %}