twrecked / hass-virtual

Virtual Components for Home Assistant
GNU General Public License v3.0
155 stars 25 forks source link

Virtual Binary Sensor resets itself after certain time #36

Open Draganis opened 2 years ago

Draganis commented 2 years ago

I have Tuya WiFi Window Sensors in use. Though they are shown as "not available" when they idle around I made automations for every window sensor to update a virtual binary_sensor on state change. Works like a charm in Winter. In Summer the windows are mostly open (sometimes for days) and now the virtual binary sensors reset theirselves to the initial value from time to time.

HA Version: Home Assistant 2022.8.6 Virtual Components Version: 0.7.7

Virtual sensor:

- platform: virtual
  name: "Schlafzimmer Fenster"
  initial_value: 'off'
  class: window
  initial_availability: true

Automations:

- id: '1660157912871'
  alias: Schlafzimmer Fenster auf
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.schlafzimmer_fenster_tuya
    to: 'on'
  condition: []
  action:
  - service: python_script.set_states
    data:
      entity_id: binary_sensor.virtual_schlafzimmer_fenster
      state: 'on'
      icon: mdi:window-open
  mode: single
- id: '1660157966855'
  alias: Schlafzimmer Fenster zu
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.schlafzimmer_fenster_tuya
    to: 'off'
  condition: []
  action:
  - service: python_script.set_states
    data: {}
  mode: single

python_scripts/set_state.py :

inputEntity = data.get('entity_id')
inputStateObject = hass.states.get(inputEntity)
inputState = inputStateObject.state
inputAttributesObject = inputStateObject.attributes.copy()

newState = data.get('state')
if newState is not None:
    inputState = newState

newIcon = data.get('icon')
if newIcon is not None:
    inputAttributesObject['icon'] = newIcon

hass.states.set(inputEntity, inputState, inputAttributesObject)
Tony763 commented 2 years ago

Hi, could you check memory consumption? For me, similar is happening when OOM killer take action.

Draganis commented 2 years ago

image I don't think this is a memory issue

Draganis commented 2 years ago

Some off behaviour i saw. If i remove the initial_value: 'off' then after i changed the value of the binary_sensor to on it immediately (3 seconds after) changes back to off

Draganis commented 2 years ago

And here is the answer to this topic. I found that the virtual envorinment hast it's own services (if someone has tzhe ability to read, they should do it XD ) I found that setting the binary sensors with virtual.turn_on / virtual.turn_off makes them stay in their position no matter what. Just like this:

service: virtual.turn_on
data:
  entity_id: binary_sensor.virtual_schlafzimmer_fenster
twrecked commented 2 years ago

Glad you found service call.

As you've guessed you can't set a sensor or binary_sensor directly, Home Assistant expects them to change under external influence, hence the new virtual.* service calls.

github-actions[bot] commented 3 weeks ago

This issue is stale because it has been open for 365 days with no activity. This issue will be automatically closed within 28 days if there is no further activity.