Modify the state of a sensor

Hello,
I have a text sensor created by an ESPHome device that stores strings of characters into it.
I have an automation that processes these strings when they arrive.
I would like the automation to then be able to delete the string from the sensor.
The STATE development tool allows this… I believe it requires sending an event… but I’m stuck on that.

Thanks in advance.

You should not be using a text sensor if you want to alter it in Home Assistant.

You don’t edit sensors.

You should be using an input_text entity.

If you want to modify the sensor value which is on the ESPHome, you can look into these two links.

ESPHome has some new ways to do two way communication as of a few releases ago. I haven’t used them but the docs seem ok.

I think if you elaborate a bit more on what you are doing and provide what config you have you’ll get better help / steer. There might be a better way to set it up.

I figured it out; you need to use a Python script:

  • Configure Python: Python Scripts - Home Assistant
  • Restart Home Assistant (I had to completely reboot the Pi 5 for it to work)
  • Create the script and name it, for example: set_texte.py
  • Paste the following code into it: hass.states.set('sensor.appareil_sensor-name', 'New text')
  • Write the following in the script: - action: python_script.set_texte

That said, since I’m sending information back to the ESP, I can also ask it to modify the text sensor at that point (actually, I want to erase it).

You are using a hammer to drive in a screw.

If you want to edit an entity then use an entity that you can edit. Like an input_text. Not a sensor.

3 Likes