How to change an attribute of an entity within an automation()

a python_script can be used to set the state and attributes of any entity programmatically.

You can also add or modify attributes associated with the entity in the "Attributes" field

Enable python script integration in configuration.yaml

Add the component to your configuration.yaml file.

python_script:

 

 

Python script

 

# /config/python_scripts/update_entity_attributes.py
# author:matterxiaomi
# blog:

entity_id = data.get('entity_id')
attributes_to_update = data.get('attributes')

if entity_id is None:
    logger.error("entity_id is required for updating attributes.")
elif attributes_to_update is None:
    logger.warning("No attributes provided to update for entity: %s", entity_id)
else:
    # Get current state and attributes
    current_state_object = hass.states.get(entity_id)

    if current_state_object:
        current_state = current_state_object.state
        current_attributes = dict(current_state_object.attributes) # Create a mutable copy

        # Update only the specified attributes
        current_attributes.update(attributes_to_update)

        # Set the state back with updated attributes, preserving the original state
        hass.states.set(entity_id, current_state, current_attributes)
        logger.info("Attributes updated for entity %s", entity_id)
    else:
        logger.warning("Entity %s not found. Cannot update attributes.", entity_id)

Explanation:

1.Set attributes only

2.The existing state will be preserved unless explicitly provided in the state parameter.

 

Developer tools 

action: python_script.update_entity_attributes
data: 
  entity_id: sensor.my_map
  attributes:
    friendly_name: new_value

 

use this script in an automation or another script:

- service: python_script.update_entity_attributes
  data:
    entity_id: sensor.my_entity
    attributes:
      new_attribute_1: value_1
      another_attribute: new_value

Explanation:

To update only attributes of an entity in Home Assistant using a Python script, you can leverage the hass.states.set() function, passing in the entity_id and the new_attributes you want to set. The existing state will be preserved unless explicitly provided in the state parameter.

 

homeassistant.update_entity

script:
  update_travel_time:
    alias: "Manually update travel time sensor"
    sequence:
      - service: homeassistant.update_entity
        target:
          entity_id: sensor.google_travel_time_home_work # Replace with your sensor's entity ID

 

 

mkdir config/python_scripts
cat <<'EOF' > config/python_scripts/set_state.py
entity = data['entity_id']
hass.states.set(entity, data['state'])
EOF

 

Set State via Button

square: true
type: grid
cards:
  - show_name: true
    show_icon: true
    name: sleep
    type: button
    tap_action:
      action: call-service
      service: python_script.set_state
      data:
        entity_id: sensor.baby
        state: sleep
      target: {}
    icon: mdi:baby-face-outline

 

useful links

Python script for Home Assistant to handle state and attributes of existing sensors and entities.

https://github.com/pmazz/ps_hassio_entities

Comments


Comments are closed