Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Latest commit

 

History

History
121 lines (81 loc) · 3.06 KB

File metadata and controls

121 lines (81 loc) · 3.06 KB

Customize.py

Found in /src/config/customize.py

Required: parse_power_payload

# Convert ongoing power reading payload to float (negative = export)
def parse_power_payload(payload: bytes, command_min: float, command_max: float) -> float | None:

This function must be edited to return the power reading as float. Return None to discard the reading

Example 1: Tasmota

Payload comes from tasmota while the device name is set to "em" and the value to "power_total":

Payload:

{"Time": "2022-10-20T20:58:13", "em": {"power_total": 230.04 }}

Function

def parse_power_payload(payload: bytes, command_min: float, command_max: float) -> float | None:
    tasmota_device = "em"
    tasmota_value = "power_total"

    jobj = json.loads(payload)
    if tasmota_device in jobj:
        em_jobj = jobj[tasmota_device]
        if tasmota_value in em_jobj:
            value = em_jobj[tasmota_value]
            if isinstance(value, float):
                return value
            elif isinstance(value, int):
                return float(value)

    return None
Example 2

Payload is just the number

Payload:

230.04

Function

def parse_power_payload(payload: bytes, command_min: float, command_max: float) -> float | None:
    return float(payload.decode())

Required: command_to_payload

# Convert calculated new limit to mqtt payload
def command_to_payload(command: float, command_type: int, command_min: float, command_max: float) -> str | None:

This function must be edited to return the mqtt payload as string. Return None to discard the limit.

Example

Just round the limit to 2 decimals

def command_to_payload(command: float, command_type: int, command_min: float, command_max: float) -> str | None:
    return f"{round(command,2):.2f}"

Optional: parse_status_payload

# Convert ongoing status update payload to bool (True = Active /False = Inactive)
def parse_status_payload(payload: bytes, current_status: bool) -> bool | None:

Only required if config.mqtt.topics.inverterStatus is not empty

This function can be be edited to return the status from the payload of config.mqtt.topics.inverterStatus as bool (True=Active / False=Inactive). Return None to discard message

Example

Test if playload is 'truthy'

def parse_status_payload(payload: bytes, current_status: bool) -> bool | None:
    s = payload.decode().lower()
    return s == "1" or s == "true"

Optional: command_to_generic

# Send your command to anywhere
def command_to_generic(command: float, command_type: int, command_min: float, command_max: float, config:dict) -> None:

This function will get called whenever a command would be published on config.mqtt.topics.writeCommand. The whole config.customize.command object is passed as parameter. Send the limit over http, sql or whatever, go wild.