Is it possible to reset manually added attributes of an entity, including headers? Or alternatively, return the entity to the initial conditions, when the attributes had not yet been added?
What is a âheaderâ?
Can you give an example of what you are trying to do?
I would like to transform this attributes state:
last_triggered: â2021-10-26T16:34:00.591184+00:00â
mode: single
current: 0
id: sensor rain
friendly_name: sensor rain
icon: âmdi:waterâ
in this:
id: sensor rain
friendly_name: sensor rain
icon: âmdi:waterâ
Why do you want to delete the first three attributes?
it would take a long time to explain
Then the short answer is ânoâ.
solved!!
I enabled pyton scripts by adding the line âpython_script:â inside the configuration.yaml file.
Then I created the âpython_scriptsâ folder inside the âconfigâ folder.
Inside the âpyton scripsâ folder I have pasted these two files:
âservices.yamlâ
# set_state.py
set_state:
description: Imposta lo stato e/o gli attributi di una entitĂ ai valori desiderati
fields:
entity_id:
description: Nome completo dell'entitĂ da aggiornare
example: device_tracker.mio_tracker
state:
description: Nuovo valore dello stato dell'entitĂ (opzionale)
example: not_home
other:
description: Qualunque altro valore verrĂ impostato negli attributi dell'entitĂ
and 'set_state.py'
#--------------------------------------------------------------------------------------------------
# Set the state or other attributes for the entity specified in the Automation Action
#--------------------------------------------------------------------------------------------------
inputEntity = data.get('entity_id')
if inputEntity is None:
logger.warning("===== entity_id is required if you want to set something.")
elif hass.states.get(inputEntity) is None:
logger.warning("===== unknown entity_id: %s", inputEntity)
else:
inputStateObject = hass.states.get(inputEntity)
inputState = inputStateObject.state
inputAttributesObject = inputStateObject.attributes.copy()
for item in data:
newAttribute = data.get(item)
logger.debug("===== item = {0}; value = {1}".format(item,newAttribute))
if item == 'entity_id':
continue # already handled
elif item == 'state':
inputState = newAttribute
else:
inputAttributesObject[item] = newAttribute
hass.states.set(inputEntity, inputState, inputAttributesObject)
I created a sensor2 template with the same status as sensor1 and with only the attribute that interests me not to delete, always the same as that of sensor1
- platform: template
sensors:
sensor2:
value_template: {{ states('sensor.sensor1')}}
attribute_templates:
attributename2: {{state_attr('sensor.sensor1', 'attributename2')}}
Then I created an âinput_boolean.sensor2â and these 2 automations, the first to update sensor2 to the new attributes and the second to reset it to the initial values.
- id: 'sensor2 update'
alias: sensor2 update
trigger:
- entity_id: input_boolean.sensor2
platform: state
to: 'on'
action:
- service: python_script.set_state
data_template:
entity_id: sensor.sensor2
attributename1: {{state_attr('sensor.sensor1', 'attributename1')}}
attributename2: {{state_attr('sensor.sensor1', 'attributename2')}}
attributename3: {{state_attr('sensor.sensor1', 'attributename3')}}
- id: 'sensor2 reset'
alias: sensor2 reset
trigger:
- entity_id: input_boolean.sensor2
platform: state
to: 'off'
action:
- service: homeassistant.update_entity
data: sensor.sensor2