Is there a way to create custom attributes for entities?

I want to create my own “custom_initial_state” attribute for every automation, because when I start Home Assistant, I disable every automation until initialization of the system is completed. Then I turn on every automation. I’d like to be able to use custom_initial_state to decide which automations should be enabled.

Is there a way to create such custom attributes that I can, with native HA functionality

  • configure the initial value in a yaml file
  • modify the value with code
  • store the state in the database

I’ve seen some people use AppDaemon, but I’d like to avoid going “outside” of HA.

1 Like

Also intetested

Yes, just add them via customize as you can see here: https://github.com/PedroLamas/home-assistant-config/blob/acb58a2b9d6cec24d2c5f2e88548df155ed43352/config/customize.yaml#L25

I do it myself and use those extra attributes in automation and scripting!

4 Likes

I am trying to fathom how this works but cannot fully understand.
I have covers that I would like to add an attribute open_position: to which would be the value from an input_number helper by the same name i.e. [cover|input_number].kitchen blind_1 for example and trying to dtermine the cleanest way to do this in customize.yaml…

Thanks!

Sorry, I should have made it clearer… what that allows you is to create new attributes with static values in them.

What you can then do is create new sensors that will use those static values: in my case, I am using those extra attributes to have “companion” devices that I will then monitor for specific automations (for example, when door A is open, turn on the indicated companion lamp)

Nevermind, I was trying to read more into what you’re doing than what is simply there!
I found 2 other entities I gleaned from somewhere which template their friendly_name: in a strange way so I copied that code and now have

cover.kitchen_blind_1:
  device_class: cover
  templates:
    open_position: >
     return entities['input_number.kitchen_blind_1'].state;
cover.kitchen_blind_2:
  device_class: cover
  templates:
    open_position: >
     return entities['input_number.kitchen_blind_2'].state;
cover.kitchen_blind_3:
  device_class: cover
  templates:
    open_position: >
     return entities['input_number.kitchen_blind_3'].state;
cover.living_room_blind:
  device_class: cover
  templates:
    open_position: >
     return entities['input_number.living_room_blind'].state;
cover.patio_door_blind:
  device_class: cover
  templates:
    open_position: >
     return entities['input_number.patio_door_blind'].state;
cover.bedroom_blind:
  device_class: cover
  templates:
    open_position: >
     return entities['input_number.bedroom_blind'].state;

I would like to return the value as an integer rather than a string with .0 but have not figured out if that is possible or not…

image

I’ve successfully been using this strategy for storing string data as custom attributes for a while and just realized that some of these are now being cleared. I assume that this happened after I updated something.

This happens on a zigbee2mqtt device binary_sensor entity (a motion sensor). After reloading my customize.yaml, I see the custom attributes, but after about a minute, they are gone. I was relying on these for templating in automations and to avoid LONG templating with a ton of IF/ELSE statements. When the motion triggers, I want to read the attribute to know what to do with that state change.

Anyone have any idea why this is happening or a better way of doing this? I tried looking into how to add the attributes from z2m, but didn’t get anywhere. I assume that when the MQTT message is sent to update the device’s entities that it is wiping out my custom attributes.

I have the same problem: need a number, not a string…

edit:

try this:

return parseInt(entities['input_number.bedroom_blind'].state);