snarky-snark / home-assistant-variables

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

Variable attribute is lost on reboot/reload #81

Closed cipacda closed 7 months ago

cipacda commented 2 years ago

I have a variable for which I set some attributes dynamically from a custom frontend panel, but the attribute gets lost after each restart or variable reload.

Here is my config


var:
  configuration:
    friendly_name: "Configuration"
    icon: "mdi:tune-variant"
    restore: true```
snarky-snark commented 2 years ago

I think this is now fixed in the latest dev version of the var component, if you're willing to give it a try.

cipacda commented 2 years ago

I am on the latest release, but I was setting the state wrong. Now I have the state being persisted, but the attributes are lost.

cipacda commented 2 years ago

I think that works as well, but you need to have the attribute set in the config, you cannot set new/dynamic attributes as those will not be persisted

snarky-snark commented 2 years ago

Just a few minutes ago, I made a potential fix on the develop branch.

cipacda commented 2 years ago

Thank you for the quick attempt. That did not seem to work though. The attributes seem to be reset on restart :(

xuko commented 1 year ago

Any progress on this? I can't find any reason at first to remove the attributes on reboot

snarky-snark commented 1 year ago

Can you explain your problem and include a minimal reproducible example?

bob-tm commented 1 year ago
var:
  attr:
    attributes:
      test1: 
      test2:

attributes test1 and test2 will survive reboot in this case

but we want to skip attribute declaration in yaml. Just declare

var:
  attr:

then set var.attr.test1 and var.attr.test2 dynamically from pyscript or automation. In this case all attributes not survive the reboot.

I use attributes of var.attr as quick dynamic variables, because don't want to declare new variable each time.

snarky-snark commented 1 year ago

Thank you. I think I see what the problem is.

danielbrunt57 commented 10 months ago

@snarky-snark Any progress on this issue? I want to track battery replacement dates as sensor sttributes in a variable with a date which I want to var.set when the bettery gets replaced. The problem I have is... a) If I dont define the attributes in my var.yaml they do not get restored. b) If I define them then they acquire the value that I am forced to set in var.yaml and then I cannot change them. Defining this way they do not restore:

  batteries_replaced:
    friendly_name: Batteries Replaced
    unique_id: batteries_replaced
    restore: true

Defining this way they are fixed and cannot be changed:

  batteries_replaced:
    friendly_name: Batteries Replaced
    unique_id: batteries_replaced
    restore: true
    attributes:
      sensor.front_door_lock_battery_level: ""
      sensor.garage_2_battery_level: ""
      sensor.garage_3_battery_level: ""
      sensor.garage_door_battery_level: ""
      sensor.laundry_room_battery_level: ""
      sensor.garage_door_lock_battery_level: ""
      sensor.4_in_1_multisensor_battery_level: ""
      sensor.front_door_multisensor_battery_level: ""
      sensor.garage_1_battery: ""
      sensor.office_battery: ""
      sensor.bedroom_battery: ""
      sensor.kitchen_battery: ""
      sensor.bathroom_battery: ""
      sensor.button_1_battery: ""
      sensor.landing_2_battery: ""
      sensor.front_porch_battery: ""
      sensor.landing_1_5_battery: ""
      sensor.living_room_battery: ""
      sensor.powder_room_battery:  ""
      sensor.south_patio_battery: ""
      sensor.kitchen_mijia_battery: ""
      sensor.outside_weather_battery: ""
      sensor.southeast_patio_battery: ""
      sensor.southwest_patio_battery: ""
      sensor.rear_smart_timer_battery: ""
      sensor.front_smart_timer_battery: ""
      sensor.phillips_hue_switch_battery: ""

and defining this way the integration fails to load:

  batteries_replaced:
    friendly_name: Batteries Replaced
    unique_id: batteries_replaced
    restore: true
    attributes:
      sensor.front_door_lock_battery_level: 
      sensor.garage_2_battery_level: 
      sensor.garage_3_battery_level: 
      sensor.garage_door_battery_level: 
      sensor.laundry_room_battery_level: 
      sensor.garage_door_lock_battery_level: 
      sensor.4_in_1_multisensor_battery_level: 
      sensor.front_door_multisensor_battery_level: 
      sensor.garage_1_battery: 
      sensor.office_battery: 
      sensor.bedroom_battery: 
      sensor.kitchen_battery: 
      sensor.bathroom_battery: 
      sensor.button_1_battery: 
      sensor.landing_2_battery: 
      sensor.front_porch_battery: 
      sensor.landing_1_5_battery: 
      sensor.living_room_battery: 
      sensor.powder_room_battery:  
      sensor.south_patio_battery: 
      sensor.kitchen_mijia_battery: 
      sensor.outside_weather_battery: 
      sensor.southeast_patio_battery: 
      sensor.southwest_patio_battery: 
      sensor.rear_smart_timer_battery: 
      sensor.front_smart_timer_battery: 
      sensor.phillips_hue_switch_battery: 
danielbrunt57 commented 10 months ago

I have successfully replaced this home-assistant-variables integration with the Variables+History integration (hass-variables) and it's working perfectly. You can dynamically add attributes and they survive a restart if Restore on Restart is true. Plus, no yaml required as it is all configured in HA Integrations frontend.