Skip to content

state.set not working as expected #715

Closed
@phsdv

Description

@phsdv

While trying to write a script to reduce the charge current of my electric car charger (called amina, connected via Zigbee2MQTT) by changing the number.amina_charge_limit value, I can see it in the graph but it does not actually happen. In the image below you can see in blue that the script changes the values (around 10:35) but the actual current (yellow) stays the same. Later, just after 10:38 in the graph, I changed the number.amina_charge_limit in the HA GUI, and then the actual current, with a small delay, does change, as expected.

It looks like state.set does not work as expected. I am expecting the charger to reduce current and the value in the graph to change, but only the value in the graph does change. The new value is somehow not communicated to the underlying device (zigbee2mqtt in this case). I checked also the zigbee2mqtt logs and values of the charge_limit do not change when running the pyscript, but do change when changing it in the GUI.

My test script:

@service
def test_state_set():
    state.set("number.amina_charge_limit", value=13)
    log.warning(f"1: {number.amina_charge_limit=},   {sensor.amina_current=}")
    task.sleep(10)
    state.set("number.amina_charge_limit", value=10)
    log.warning(f"2: {number.amina_charge_limit=},   {sensor.amina_current=}")
    task.sleep(10)
    state.set("number.amina_charge_limit", value=13)
    log.warning(f"3: {number.amina_charge_limit=},   {sensor.amina_current=}")
    task.sleep(10)
    number.amina_charge_limit=9
    log.warning(f"4: {number.amina_charge_limit=},   {sensor.amina_current=}")
    task.sleep(10)
    number.amina_charge_limit=6
    log.warning(f"5: {number.amina_charge_limit=},   {sensor.amina_current=}")
    task.sleep(10)
    number.amina_charge_limit=13
    log.warning(f"6: {number.amina_charge_limit=},   {sensor.amina_current=}")

log file result:

2025-04-09 10:34:38.451 INFO (MainThread) [custom_components.pyscript.global_ctx] Reloaded /config/pyscript/dev_z2mtest.py
2025-04-09 10:34:47.163 WARNING (MainThread) [custom_components.pyscript.file.dev_z2mtest.test_state_set] 1: number.amina_charge_limit=13,   sensor.amina_current=13.04
2025-04-09 10:34:57.166 WARNING (MainThread) [custom_components.pyscript.file.dev_z2mtest.test_state_set] 2: number.amina_charge_limit=10,   sensor.amina_current=12.97
2025-04-09 10:35:07.169 WARNING (MainThread) [custom_components.pyscript.file.dev_z2mtest.test_state_set] 3: number.amina_charge_limit=13,   sensor.amina_current=13.02
2025-04-09 10:35:17.172 WARNING (MainThread) [custom_components.pyscript.file.dev_z2mtest.test_state_set] 4: number.amina_charge_limit=9,   sensor.amina_current=13.02
2025-04-09 10:35:27.175 WARNING (MainThread) [custom_components.pyscript.file.dev_z2mtest.test_state_set] 5: number.amina_charge_limit=6,   sensor.amina_current=13.07
2025-04-09 10:35:37.177 WARNING (MainThread) [custom_components.pyscript.file.dev_z2mtest.test_state_set] 6: number.amina_charge_limit=13,   sensor.amina_current=13.01

Graph of both number.amina_charge_limit in blue and sensor.amina_current in yellow:
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions