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

Attributes have null as value #77

Open majkers opened 2 years ago

majkers commented 2 years ago

Hi,

here is my config:

var:
  gaz1:
    friendly_name: "Rachunek za gaz"
    entity_picture: '/local/rachunki/pgnig.png'  
    initial_value: '2022-07-11'
    attributes:
      do_zaplaty: '449,70 zł'
      czy_oplacony: 'nie'

And this is what I see in HA:

image

So attributes get null as value...

majkers commented 2 years ago

Maybe this is related?

invalid key: "OrderedDict([('449,70 zł', None)])" in "/config/configuration.yaml", line 807, column 0

snarky-snark commented 2 years ago

Perhaps a text encoding issue? What happens if you change '449,70 zł' to '449,70?

majkers commented 2 years ago

No, still the same

vigilcoma commented 2 years ago

Have the same issue with attributes

snarky-snark commented 2 years ago

Have the same issue with attributes

If you post more details about your problem, we may have a better chance of determining the cause.

vigilcoma commented 2 years ago

Have this config

var:
  lexman_1_data:
    friendly_name: "Lexman 1 data"
    initial_value: '{"current_light": ""}'
    attributes:
      scene_1: '{{ {"counter": 1} }}'
      scene_2: '{{ {"counter": 2} }}'
      scene_3: '{{ {"counter": 3} }}'
      scene_4: '{{ {"counter": 4} }}'

Wanted to store simple json

snarky-snark commented 2 years ago

Okay, I think I know what is happening here. Variable attributes are templates, and they are only updated when a tracked event fires. Neither of your variables specify any tracked events, so the attributes are never evaluated.

You can fix this by specifying the following tracked event which will update your variable attributes when home assistant starts.

tracked_event_type: homeassistant_start

Alternatively, you can set the attributes using the var.set service.

As an aside, this behavior can be inferred from the documentation, but it is not obvious. A proper fix for this is to create a separate attribute_templates property for templates and leave attributes for values only.

majkers commented 2 years ago

OK but how about reloading this integration? By watching this event, reloading rom YAML section won't work right? This is essential for my and the reason why I wanted to switch to your integration from similar...

snarky-snark commented 2 years ago

https://www.home-assistant.io/docs/configuration/events/#component_loaded

majkers commented 2 years ago

OK but how can I pass component attribute?

snarky-snark commented 2 years ago

I am not sure what you are asking.

majkers commented 2 years ago

This event (COMPONENT_LOADED) requires additional data to be passed -> component (Domain of the integration that has just been initialized. Example: light.) How can I pass it from your integration?

snarky-snark commented 2 years ago

The domain of the Variable component is var.

majkers commented 2 years ago

Yes I know that but how can I pass this param via var integration? tracked_event_type: COMPONENT_LOADED and then what?

snarky-snark commented 2 years ago

Ah, I see. That is not supported at the moment.

There might be another way to solve your problem, but I'm not sure I understand your use case.