pmazz / ps_hassio_entities

Python script to handle state and attributes of existing sensors and entities
MIT License
58 stars 10 forks source link

New attribute disappears on binary_sensor after state change #7

Closed wilfredsmit closed 2 years ago

wilfredsmit commented 2 years ago

What is your question When I create a attribute on a binary_sensor that triggers the creation of the attribute, the attribute disappears after another state change of the binary sensor.

Is this by design? I would like the attribute to be permanent.

YAML configuration

---
alias: Oprit Hue
id: oprit_hue

trigger:
  - platform: state
    entity_id: binary_sensor.hue_reserve_motion_lumi
    from: "off"
    to: "on"

action:
  - service: python_script.hass_entities
    data:
      action: set_attributes
      entity_id: binary_sensor.hue_reserve_motion_lumi
      attributes: 
        - start: "{{ as_timestamp(now()) |float(0) }}"

Screenshots Animated gif! Screen Recording (Systeemvoorkeuren)

Version v1.1.0

Additional information Logfile info:

2022-05-04 10:44:50 INFO (SyncWorker_2) [homeassistant.components.python_script] Executing hass_entities.py: {'log_enabled': True, 'action': 'set_attributes', 'entity_id': 'binary_sensor.hue_reserve_motion_lumi', 'attributes': [{'start': 1651653890.053345}]} 2022-05-04 10:44:50 DEBUG (SyncWorker_2) [homeassistant.components.python_script.hass_entities.py] Python Script: hass_entities.py -> START of action: set_attributes 2022-05-04 10:44:50 DEBUG (SyncWorker_2) [homeassistant.components.python_script.hass_entities.py] Entity: 'binary_sensor.hue_reserve_motion_lumi' -> New attribute 'start': '1651653890.053345' 2022-05-04 10:44:50 DEBUG (SyncWorker_2) [homeassistant.components.python_script.hass_entities.py] DONE -> Attributes set for entity 'binary_sensor.hue_reserve_motion_lumi' 2022-05-04 10:44:50 DEBUG (SyncWorker_2) [homeassistant.components.python_script.hass_entities.py] Python Script: hass_entities.py -> END of action: set_attributes

pmazz commented 2 years ago

Hi, I would say it depends on how the sensor is internally handled by the HUE integration. It looks like the sensor is recreated from scratch each time, instead of updated.

Consider that the script only set the attribute value, creating the attribute if not exists. It doesn't delete the attribute, unless you don't use the delete_attribute action (which is not the case from what I'm seeing).

I'm afraid you need to adapt the trigger of your automation and set the attribute value for each sensor state change.

wilfredsmit commented 2 years ago

Oké, thanks for your explanation. Then I'll have to work my way around this behaviour. Nice script though!

pmazz commented 2 years ago

👍