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