Custom component to declare/set variables

Thanks, but that is not what I am trying to do. I want to use an !include directive with the variables itself, not the packages

You might review the different alternative include mechanisms here https://www.home-assistant.io/docs/configuration/splitting_configuration#advanced-usage if youā€™ve not seen it.

The include mechanisms are generic in the YAML parsing, not specific to any component.

So maybe you could use incude_dir_merge_named ../variables with the way you have each individual file constructed per your example?

1 Like

Thatā€™s what I thought, and I am familiar with the directives but I must have been looking at it for too long that I did not see the wrong include I tried (copy/paste from another file in my config). Thanks for pointing it out and making my look twice (more actually) :smiley:

Has anyone gotten this working with 0.89.1?

Iā€™m getting the following error:

Traceback (most recent call last):
File ā€œ/usr/src/app/homeassistant/setup.pyā€, line 154, in _async_setup_component
component.setup, hass, processed_config) # type: ignore
AttributeError: module ā€˜custom_components.variableā€™ has no attribute ā€˜setupā€™

Iā€™ve moved the variable.py into custom_components/variable/variable.py and also created an empty init.py

Works for me in custom_components/variable.py.

No need for this, because this is no modified core component.

Ah, thanks for this. I must have misunderstood, and thought this was ultimately going to be required for all custom components.

since ha 92.0 I get this error

Unable to find services.yaml for the variable integration

I think is related to this https://github.com/home-assistant/home-assistant/issues/23197

Itā€™s just a warning.
Create an empty services.yaml in your custom_components folder and itā€™s gone.

2 Likes

donā€™t we need a manifest.json, and/or init.py from HA 92.2 upwards? Asking because being at the verge of upgrading my systemHub instanceā€¦

No, this cc works without it.
Only the services warning on HA start.
Setting attributes seems broken since a few versions.
Looks like @rogro82 stopped working on it.

?? really? thatā€™s the crux of this component isnā€™t it?
you mean this isnt available any longer:

    action:
      service: variable.set_variable
      data:
        variable: last_motion
        attributes_template: >
          {
                "history_1": "{{ variable.state }}",
                "history_2": "{{ variable.attributes.history_1 }}",
                "history_3": "{{ variable.attributes.history_2 }}",
                "history_4": "{{ variable.attributes.history_3 }}",
                "history_5": "{{ variable.attributes.history_4 }}"
          }
      data_template:
        value: >
          {{ trigger.to_state.attributes.friendly_name }}

still going strong on 91.2ā€¦

Sorry, attributes work.
Need some more coffee! :coffee:

no worries, thanks for confirming this component is still doing its job! waiting for 92.2 to kick in any time soon now.

just merged https://github.com/rogro82/hass-variables/pull/21

can anyone please confirm this is working as expected? I still need to upgrade HA myself

1 Like

Yepp, works for me in 0.92.2
Thanks @rogro82

HI

since this is in the CC folder itself and not in a custom_components/variable subfolder, wonā€™t the init.py and manifest.json files interfere with other components that reside there?

Or should we move your CC to a dedicated sub folder after all now? I have the custom_updater reside there alsoā€¦

@Mariusthvdb, create the variable folder in custom_components and copy the three files in it.

(ha) homeassi:~$ ll .homeassistant/custom_components/variable/
insgesamt 20
-rw-rw-r-- 1 homeassi homeassi 6890 Mai  2 16:28 __init__.py
-rw-rw-r-- 1 homeassi homeassi  212 Mai  2 16:28 manifest.json
drwxrwxr-x 2 homeassi homeassi 4096 Mai  2 16:29 __pycache__
-rw-rw-r-- 1 homeassi homeassi  814 Mai  2 16:28 services.yaml

yes I have done so. Was merely asking since since the Great Migration all custom components are in their own subfolder with the init.py and manifest.json files, and their names arenā€™t referencing the CC any longer. Which might/will interfere when other components are in that folder too.

A dedicated /variable subfolder would be the logical way forward?

Does this component still work? Iā€™m completely stumped. I have an automation which launches fine but simply wonā€™t set anything in the variables. I have tried all the templates in the template editor and they all return the values perfectly, but the variable just refuses to pick them up. Iā€™ve verified the variable exists and the sensors are all named correctly. Is there something else I can do to debug this? Is there any length limitations for the attributes?

- id: posta_state_change
  alias: 'Posta state change'
  hide_entity: true
  initial_state: 'true'
  trigger:
    platform: state
    entity_id: sensor.cp_packages_coming_today
  condition:
    condition: state
    entity_id: 'sensor.cp_packages_coming_today'
    state: 'delivery'
  action:
    service: variable.set_variable
    data:
      variable: posta_variable
      value_template: "{{ states('sensor.cp_packages_coming_today')}}"
      attributes_template: >
        {
          "from": "{{ state_attr('sensor.cp_packages_coming_today', 'from') }}",
          "date": "{{ state_attr('sensor.cp_packages_coming_today', 'date') }}",
          "subject": "{{ state_attr('sensor.cp_packages_coming_today', 'subject') }}",
          "body": "{{ state_attr('sensor.cp_packages_coming_today', 'body') }}"
        } 

Iā€™m not sure about the attributes (never used them), but at least you need to use data_template instead of data.