snarky-snark / home-assistant-variables

A custom Home Assistant component for declaring and setting generic variable entities dynamically.
Apache License 2.0
274 stars 16 forks source link

variable entity is briefly removed when hot reloading #74

Closed zanna-37 closed 1 year ago

zanna-37 commented 2 years ago

When I hot reload the configuration, a weird behavior happens. When a variable is used in a state trigger, the trigger is fired from a state null [see 1]. When used in a numeric state, an error is logged [see 2]

I suspect that the underlying reason is that the variable entity is briefly removed and then re-added to home assistant. This causes all sorts of problems. Maybe a better approach would be to update the entity without removing it.


1

In the automation with the state trigger:

platform: state
entity_id:
  - var.my_var1

When I hot reload the configuration, the automation is triggered and this is the info found in the debug section: Notice the strange from_state: null.

trigger:
[...]
  platform: state
  entity_id: var.my_var1
  from_state: null # <--------------------------- HERE
  to_state:
    entity_id: var.my_var1
    state: '1'
    [...]

2

In the automation with a numeric trigger:

platform: numeric_state
entity_id: var.my_var2
below: '100'

When I hot reload the configuration, this error is logged:

Logger: homeassistant.components.homeassistant.triggers.numeric_state
Source: components/homeassistant/triggers/numeric_state.py:188
[...]
Error in 'my automation' trigger: In 'numeric_state' condition: no entity specified
RoboMagus commented 2 years ago

I'd have to re-check to be sure, but this seems almost identical to problems I ran into some time ago with HA's built in template sensor reloading as well.

This seems to just be the way HA deals with hot reloading of entities whose state is restored on reload; temporarily undefined until restored.

snarky-snark commented 2 years ago

Yeah, I don't see a way to persist entities across reload. HA seems to remove the entities requiring the component to create new ones. I'm not sure there is anything that can be done to address this problem from within this component.

I will leave this ticket open in case others run into this and in case there is a future development that allows me to address this issue.

zanna-37 commented 2 years ago

@RoboMagus the strange thing here is that the entity is not undefined, unavailable, or unknown, it literally does not exist for a brief moment. I never saw something like that. That's why I opened the issue, I though you were removing the entity and then adding it back instead of updating it.

snarky-snark commented 2 years ago

The entities are being recreated each time refresh is triggered. I don't know how one would update the entities. HA doesn't seem to persist the entities across reloads.

kannkann commented 2 years ago

Hello, I am struggling with this behavior right now. I use a counter similar to your example (just without the initial_value) and every time I reload the integration or the home assistant the counter increments. I was wondering if there is a way to catch these unintended variable updates. Thank you!

snarky-snark commented 2 years ago

Please post the relevant YAML, and we might be able to resolve your problem more quickly.

kannkann commented 2 years ago

You are right. This is my code. However, I just tested and I think it has to do with the tracked entities and not the counter itself. Let me investigate a bit further.

var:
  linktap1_channel:
    friendly_name: "LinkTap1 Channel"
    value_template: >-
      {% if ( states('var.linktap1_channel') | int(1) ) >= 4 %}
        1
      {% else %}
        {{ ( states('var.linktap1_channel') | int(1) ) + 1 }}
      {% endif %}
    tracked_entity_id:
      - var.zone1_vol
      - var.zone2_vol
      - var.zone3_vol
      - var.zone4_vol
    icon: mdi:switch
kannkann commented 2 years ago

Ok I had trouble to reproduce my problem (see below), but now I managed to reproduce a minimal example. Just reload the variable yaml configuration to increment the counter.

var:

  linktap1_channel:
    friendly_name: "LinkTap1 Channel"
    value_template: >-
      {% if ( states('var.linktap1_channel') | int(-1) ) >= 4 %}
        1
      {% else %}
        {{ ( states('var.linktap1_channel') | int(-1) ) + 1 }}
      {% endif %}
    tracked_entity_id:
      - var.test_raw
    icon: mdi:switch
  test_raw:
    friendly_name: "test raw"
    initial_value: ""

Interestingly, the problem does not occur if the ORDER of in the YAML configuration is changed:

var:
  test_raw:
    .......
  linktap1_channel:
    ......

So it seems I found a way to fix my problem by reordering the order of definition.