Custom attributes managed by automations

Is it possible to add custom attributes to an existing sensor and set their value from an automation?

Example: I have a power consumption sensor of my washing machine and would like to add automation like this

Start

trigger:
    platform: state
    entity_id: sensor.washing_machine_power
    from: '0.0' 
action:
    # started_at = now()
    # washing = True

Stop

trigger:
    platform: state
    entity_id: sensor.washing_machine_power
    to: '0.0' 
    for:
      minutes: 1
action:
    # finished = now()
    # washing = False

Is there an example how to manage such custom attributes, the simplest possible way?

There’s no service call for adding custom attributes.

Currently, custom attributes are defined manually either via editing customize.yaml or via the UI using Configuration > Customizations.

Thanks. The simplest way to set the attribute value (from automation action) is custom python script?

A python_script (such as rodpayne’s set_state.py) can be used to set an entity’s (existing) attribute value. However, the value will not survive a restart.

According to logs the script works fine when tested from dev tools. Attributes are however not updated. Their values remain set to initials, from customize.yaml, although new values are passed to script. Is this the correct way to add custom attributes:

customize.yaml

sensor.shly_washing_machine_power:
  started_at: ""
  finished_at: ""

Yes.

However, if your intention is to set the values with a python_script, the values will (as per my explanation) disappear after a restart. They will be reset to whatever values you specified in customize.yaml. According to your example, their values will be empty strings on startup.

Any ideas why the values are not updated after calling the script and no error recorded in log?

Post the YAML code you are using.