Skip to content

Commit

Permalink
With Tesla management
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Marc Collin committed Jun 11, 2023
1 parent 2b1cc72 commit 8ed8780
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 111 deletions.
36 changes: 19 additions & 17 deletions .devcontainer/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,6 @@ switch:
data:
entity_id: input_boolean.fake_device_i

input_select:
fake_tesla_1:
name: "Fake Tesla 1"
options:
- "0 A"
- "1 A"
- "2 A"
- "3 A"
- "4 A"
- "5 A"
- "6 A"

input_number:
consommation_brut:
name: Puissance brut consommée
Expand Down Expand Up @@ -123,6 +111,15 @@ input_number:
icon: mdi:percent
unit_of_measurement: '%'
mode: slider
tesla_amps:
name: Tesla Amps
min: 0
max: 32
icon: mdi:ev-station
unit_of_measurement: A
mode: slider
step: 1


solar_optimizer:
refresh_period_sec: 10
Expand Down Expand Up @@ -194,15 +191,20 @@ solar_optimizer:
activation_service: "input_boolean/turn_on"
deactivation_service: "input_boolean/turn_off"
- name: "Equipement H"
entity_id: "input_select.fake_tesla_1"
entity_id: "input_boolean.fake_device_h"
power_entity_id: "input_number.tesla_amps"
power_min: 660
power_max: 3960
power_step: 660
check_active_template: "{{ not is_state('input_select.fake_tesla_1', '0 A') }}"
# check_active_template: "{{ not is_state('input_select.fake_tesla_1', '0 A') }}"
check_usable_template: "{{ True }}"
duration_sec: 30
action_mode: "event"
change_power_service: "fake/change_power"
duration_sec: 60
duration_power_sec: 10
action_mode: "service_call"
activation_service: "input_boolean/turn_on"
deactivation_service: "input_boolean/turn_off"
change_power_service: "input_number/set_value"
convert_power_divide_factor: 660
- name: "Equipement I"
entity_id: "switch.fake_switch_1"
power_max: 20
Expand Down
30 changes: 10 additions & 20 deletions config/automations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
- platform: event
event_type: solar_optimizer_state_change_event
id: state_change
- platform: state
entity_id:
- input_number.consommation_brut
- input_number.production_solaire
condition: []
action:
- choose:
Expand All @@ -21,26 +25,6 @@
requested_power {{ trigger.event.data.requested_power }}. (current_power
is {{ trigger.event.data.current_power }})'
title: ChangePower Event de Solar Optimizer
- service: input_select.select_option
data:
option: '{{ (trigger.event.data.requested_power | float(0) / 660) | round(0)
}} A'
target:
entity_id: input_select.fake_tesla_1
- if:
- condition: state
entity_id: input_select.fake_tesla_1
state: 0 A
then:
- service: input_boolean.turn_off
data: {}
target:
entity_id: input_boolean.fake_device_h
else:
- service: input_boolean.turn_on
data: {}
target:
entity_id: input_boolean.fake_device_h
- conditions:
- condition: trigger
id: state_change
Expand All @@ -51,5 +35,11 @@
requested_power {{ trigger.event.data.requested_power }}. (current_power
is {{ trigger.event.data.current_power }})'
title: StateChange Event de Solar Optimizer
- service: input_number.set_value
data:
value: '{{ states(''input_number.consommation_brut'') | float(0) - states(''input_number.production_solaire'')
| float(0) + states(''sensor.total_power'') | float(0) }}'
target:
entity_id: input_number.consommation_net
mode: parallel
max: 50
55 changes: 15 additions & 40 deletions custom_components/solar_optimizer/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

_LOGGER = logging.getLogger(__name__)

SIMUL = True


def get_safe_float(hass, entity_id: str):
"""Get a safe float state value for an entity.
Expand Down Expand Up @@ -98,6 +96,7 @@ async def _async_update_data(self):
"name": device.name,
"is_active": device.is_active,
"is_usable": device.is_usable,
"is_waiting": device.is_waiting,
"current_power": device.current_power,
"requested_power": device.requested_power,
}
Expand All @@ -110,22 +109,10 @@ async def _async_update_data(self):
calculated_data["device_states"] = device_states

# Add a power_consumption and power_production
power_production = calculated_data["power_production"] = get_safe_float(
calculated_data["power_production"] = get_safe_float(
self.hass, self._power_production_entity_id
)

if SIMUL:
conso_brut = get_safe_float(self.hass, "input_number.consommation_brut")
# if conso_brut is not None and power_production is not None:
# await self.hass.services.async_call(
# "input_number",
# "set_value",
# {
# "entity_id": "input_number.consommation_net",
# "value": str(int(conso_brut - power_production)),
# },
# )

calculated_data["power_consumption"] = get_safe_float(
self.hass, self._power_consumption_entity_id
)
Expand Down Expand Up @@ -155,24 +142,27 @@ async def _async_update_data(self):
calculated_data["best_objective"] = best_objective
calculated_data["total_power"] = total_power

# Uses the result to turn on or off
# Uses the result to turn on or off or change power
for _, equipement in enumerate(best_solution):
_LOGGER.debug("Dealing with best_solution for %s", equipement)
for _, device in enumerate(self._devices):
if device.name == equipement["name"]:
name = equipement["name"]
if device.name == name:
requested_power = equipement.get("requested_power")
state = equipement["state"]
is_active = device.is_active
if is_active and not equipement["state"]:
_LOGGER.debug("Extinction de %s", equipement["name"])
if is_active and not state:
_LOGGER.debug("Extinction de %s", name)
await device.deactivate()
elif not is_active and equipement["state"]:
_LOGGER.debug("Allumage de %s", equipement["name"])
elif not is_active and state:
_LOGGER.debug("Allumage de %s", name)
await device.activate(requested_power)
elif (
is_active
and equipement["state"]
and device.current_power != requested_power

# Send change power if state is now on and change power is accepted and (power have change or eqt is just activated)
if (
state
and device.can_change_power
and (device.current_power != requested_power or not is_active)
):
_LOGGER.debug(
"Change power of %s to %s",
Expand All @@ -184,19 +174,4 @@ async def _async_update_data(self):

_LOGGER.debug("Calculated data are: %s", calculated_data)

if (
SIMUL
and conso_brut is not None
and total_power is not None
and power_production is not None
):
await self.hass.services.async_call(
"input_number",
"set_value",
{
"entity_id": "input_number.consommation_net",
"value": str(int(conso_brut + total_power - power_production)),
},
)

return calculated_data
Loading

0 comments on commit 8ed8780

Please sign in to comment.