litinoveweedle / hass-template-climate

❄️Templatable Climate Device for Home Assistant, Supports Running Actions On Service Calls.
MIT License
24 stars 1 forks source link

Changes on device appear to trigger service again in a loop. #9

Closed CV8R closed 2 months ago

CV8R commented 4 months ago

The problem

State changes from the climate device triggers the climate service again which causes loops, races and general hassles. I have had to disable the template devices because I am unable to turn off the climate device. The device tries to turn off, but heat mode is set again and the unit stays on, continues to loop.

Below is an example where the set point was changed on the AC units panel, resulting in triggering the setpoint service.

2024-04-17 21:48:17.457 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC attribute target_temperature triggered update with value: 21.0.
2024-04-17 21:48:17.457 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC updating attribute target_temperature to value: 21.0.
2024-04-17 21:48:17.457 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC executing script set_action_temperature variables: {'temperature': 21.0}.
2024-04-17 21:48:18.463 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC execution of script set_action_temperature finished.

I believe this issue is related https://github.com/jcwillox/hass-template-climate/issues/14

Below is an example of trying to turn the unit off from heat mode using the template - note it triggers the "Already running"

Shall I create a new issue in your repo for this?

2024-04-17 21:54:47.093 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC attribute hvac_mode triggered update with value: off.
2024-04-17 21:54:47.093 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC updating attribute hvac_mode to value: off.
2024-04-17 21:54:47.093 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC executing script set_action_hvac_mode variables: {'hvac_mode': 'off'}.
2024-04-17 21:54:48.106 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC attribute hvac_mode triggered update with value: off.
2024-04-17 21:54:48.106 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC attribute hvac_mode is already set to value: off.
2024-04-17 21:54:48.106 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC executing script set_action_hvac_mode variables: {'hvac_mode': 'off'}.
2024-04-17 21:54:48.106 WARNING (MainThread) [homeassistant.helpers.script.panasonic_ducted_ac] Panasonic Ducted AC: Already running
2024-04-17 21:54:48.106 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC execution of script set_action_hvac_mode finished.
2024-04-17 21:54:48.107 ERROR (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC attribute target_temperature returned invalid value: 0.0, which is smaller than min setpoint: 16.0.
2024-04-17 21:54:48.107 ERROR (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC attribute target_temperature value 0.0 is invalid.
2024-04-17 21:54:48.113 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC attribute hvac_mode triggered update with value: heat.
2024-04-17 21:54:48.113 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC updating attribute hvac_mode to value: heat.
2024-04-17 21:54:48.113 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC executing script set_action_hvac_mode variables: {'hvac_mode': 'heat'}.
2024-04-17 21:54:48.114 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC attribute target_temperature triggered update with value: 21.0.
2024-04-17 21:54:48.114 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC attribute target_temperature is already set to value: 21.0.
2024-04-17 21:54:48.114 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC executing script set_action_temperature variables: {'temperature': 21.0}.
2024-04-17 21:54:48.121 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC execution of script set_action_hvac_mode finished.
2024-04-17 21:54:48.647 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC attribute hvac_mode triggered update with value: off.
2024-04-17 21:54:48.648 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC updating attribute hvac_mode to value: off.
2024-04-17 21:54:48.648 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC executing script set_action_hvac_mode variables: {'hvac_mode': 'off'}.
2024-04-17 21:54:48.648 WARNING (MainThread) [homeassistant.helpers.script.panasonic_ducted_ac] Panasonic Ducted AC: Already running
2024-04-17 21:54:48.648 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC execution of script set_action_hvac_mode finished.
2024-04-17 21:54:48.648 ERROR (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC attribute target_temperature returned invalid value: 0.0, which is smaller than min setpoint: 16.0.
2024-04-17 21:54:48.648 ERROR (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC attribute target_temperature value 0.0 is invalid.
2024-04-17 21:54:49.130 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC execution of script set_action_temperature finished.
2024-04-17 21:54:51.138 DEBUG (MainThread) [custom_components.climate_template.climate] Entity Panasonic Ducted AC execution of script set_action_hvac_mode finished.

What version of Template Climate has the issue?

No response

What version of Home Assistant are you running?

No response

What type of installation are you running?

None

Example YAML snippet

- platform: climate_template
    name: "Panasonic Ducted AC"
    unique_id: eef60d30-4e1c-11ee-be56-0242ac120002
    mode_action: single
    modes:
      - "heat"
      - "cool"
      - "dry"
      - "fan_only"
      - "off"
    fan_modes:
      - "auto"
      - "1"
      - "2"
      - "3"
    min_temp: 16
    max_temp: 30
    temp_step: 1
    precision: 0.1
    current_temperature_template: "{{ states('sensor.multisensor_6_family_room_air_temperature_3') | round(0) }}"
    current_humidity_template: "{{ states('sensor.multisensor_6_family_room_humidity_3') | round(0) }}"
    hvac_mode_template: "{{ states('climate.cc3f1d041688') }}"
    target_temperature_template: >-
      {% if state_attr('climate.cc3f1d041688','temperature') | is_number %}
        {{state_attr('climate.cc3f1d041688','temperature') | round(0) }}
      {% else %}
        0
      {% endif %}
    fan_mode_template: "{{ state_attr('climate.cc3f1d041688','fan_mode') }}"
    availability_template: >
      {% if is_state('climate.cc3f1d041688', 'unavailable') -%}
        False
      {%- elif is_state('climate.cc3f1d041688', 'unknown') -%}
        False
      {%- elif is_state('binary_sensor.panasonic_ducted_ac_ping', 'off') -%}
        False
      {%- else -%}
        True
      {%- endif %}
    set_hvac_mode:
      - service: climate.set_hvac_mode
        target:
          entity_id: climate.cc3f1d041688
        data:
          hvac_mode: "{{ hvac_mode }}"
    set_fan_mode:
      - service: climate.set_fan_mode
        target:
          entity_id: climate.cc3f1d041688
        data:
          fan_mode: "{{ fan_mode }}"
    set_temperature:
      - service: climate.set_temperature
        target:
          entity_id: climate.cc3f1d041688
        data:
          temperature: "{{ temperature }}"
    icon_template: >-
      {% if is_state('climate.cc3f1d041688', 'cool') -%}
        mdi:snowflake
      {%- elif  is_state('climate.cc3f1d041688', 'heat') -%}
        mdi:white-balance-sunny
      {%- elif  is_state('climate.cc3f1d041688', 'dry') -%}
        mdi:water-off
      {%- elif  is_state('climate.cc3f1d041688', 'fan_only') -%}
        mdi:fan
      {%- endif %}

Anything in the logs that might be useful for us?

No response

Additional information

No response

litinoveweedle commented 4 months ago

Hello, thank you very much for opening issue.

Just for future reference, why this is happening. First the WARNING message Already running comes from the HA native script entity. As default climate_template schedule script to run in mode_action Single. This is also visible in your configuration:

mode_action: single

This means, that is script is still running and if executed again, it will raise this warning message and will not start. This is as per HA specification. So this is HA native function, which is outside of the climate-template reach.

I personally use climate-template to handle my boiler, which has very very very slow interface as the Siemens OZW webserver needs tens of second to write value and another tens of second to update the read value in HA. So in my case, there is pretty long window of script execution and i meanwhile script is executed again (for whatever reason) I will get Already running warning. In such case I should probably use different mode_action, the best is probably:

mode_action: queued

I some special cases, like the one you described - when the update of the value by script is read by the template and cause described loops, we can probably expect such change to happen and at least not fire the script again, if we already running script with the same attributes value. This can theoretically cause some divergence between the real device and the climate_template. This would be caused by the need, to update internal value of the attributes before starting first script run, so if the script is triggered second time (even before first is finished) we see, that it would be called with the same value and do not run it. But what will happen, if the first script execution will fail later and the managed device attribute is not updated? The value in climate template is already updated and even if you try to re-trigger the script run with the same attribute value, it will not start again. This means, that if the execution of the script fails, your climate_template and managed device attribute will not be in sync, until the attribute will not change its value again, causing script to be run.

So far I implemented this as it very simple logic. You can try it in the latest climate.py of the development branch. This should solve most obvious case of the loop as you reported.

I would like to explore also other possibility - when executing script create first "expectation" for the updated value, but do not update internal climate_template attribute. Only when update will be received from template in under configurable timeout time, update internal attribute value. I would also allow to re-trigger script run, if the expectation is not fulfilled before timeout. In case script will be re-triggered during the timeout with different attribute value we will need to add new expectation to the list and allow for the new script to be queued to be executed after first script finishes. In such case we shall set mode_action to queued and max_action parameter to some reasonable value to limit the queue size. I would like to explore this logic, but probably in the different development branch.

CV8R commented 4 months ago

Hi, I had a go at using the latest version but unfortunately I was having issues with turning the AC units off/on so I have had to stop using it. They ACs are not always immediate to respond and I think that must mess with it.

I can have another go at testing if it helps you identify the cause.

litinoveweedle commented 4 months ago

That should not be issue at all. My own usage depends on the boiler controller, which is terrible slow and set command and update state is taking up to 30sec. You have to understand, that this has nothing to do with integration itself.

In my case adding delay and also triggering refresh of the changed status solve my issue with slow response of the controlled device:

   set_hvac_mode:
      - condition: template
        value_template: "{{ hvac_mode is defined and (( hvac_mode == 'heat' and states('switch.thrs19_dhw_operating_mode') != 'on' ) or ( hvac_mode == 'off' and states('switch.thrs19_dhw_operating_mode') != 'off' )) }}"
      - service: switch.toggle
        target:
          entity_id: switch.thrs19_dhw_operating_mode
      - delay:
          milliseconds: 500
      - service: homeassistant.update_entity
        target:
          entity_id: switch.thrs19_dhw_operating_mode

But IMHO what issue you describe is not related. I am not aware of any related change in the code in the latest version. If you need any help, you need to switch debug logging on and provide more detail description of the issue.

dmatik commented 3 months ago

Hi, I am not really sure that this is the same but I am also having issues lately, that were not there before for sure. I have custom card (custom button card) build for all my ACs. The cards are actually configured to work with template climate entities, and turn on/off ACs by running HA service, as simple as that. Recently when I turn on/off the AC using such card, it is being actually turning on/off only the template entity, but not the actual climate entity of that AC, and few seconds later the template climate entity also being changed back to its previous state. On the second try with the very same card it works as expected, both entities changing the state. So this is non consistent.

Might be that I missed something in configuration? But then it would not work at all, right... Also, I am using this for very long time, and it all was working fine. I guess something changed with latest HA updates?

Please let me know if you want a separate issue for that.

litinoveweedle commented 3 months ago

@dmatik Hello, please enable debug logging and post the log - with comments what what logged after what action. Also please post your configuration. Thanks

litinoveweedle commented 2 months ago

Missing feedback from @dmatik , closing as stale.