How to remove quotes from MQTT messages

Home Assistant publishes the attributes of sensors with quotes. How would I be able to get rid of the quotes around text that is imported from MQTT.

This is the states as published in Home Assistant:

date: 5
month: October
icon: U000F0590
temperature: 18.2
lowtemp: 11
hightemp: 19.9
percipitation: 57
friendly_name: weathertoday

They are posted in MQTT as:

state = Many clouds
last_updated = 2022-10-05T18:40:22.568707+00:00
last_changed = 2022-10-05T18:40:22.568707+00:00
date = 5
month = "October"
icon = "U000F0590"
temperature = 18.2
lowtemp = 11.0
hightemp = 19.9
percipitation = 57
friendly_name = "weathertoday"

If I want to use the attribute month to be printed on e-paper I use this:

text_sensor:
  - platform: mqtt_subscribe
    id: monttoday
    topic: homeassistant/sensor/weathertoday/month

And in the lambda I use this:

it.printf(75, 200, id(tm24), TextAlign::TOP_CENTER, "%s", id(monttoday).state.c_str());

This causes "October" is printed instead of October. So how would I be able to get rid of the quotation marks?
Preferably not having to create seperate sensors for all values that I want to use - Home Assistant doesn’t sent the state with quotes. I hope there is a nice solution.

I assume you aren’t using the API to connect this ESPhome to HA? Otherwise I would just use a Home Assistant text sensor rather than MQTT.

1 Like

No, the API is not quick enough and therefor not suitable for deep sleep. HA only connects to ESPhome every 60 seconds. To have the display come out of deep sleep and having to wait 60+ seconds for the numbers to show up is too long for my project.
This display comes on every 1 hour only to update. With MQTT this takes less than 6 seconds.

I had the code first with API. It would come on for 90s, refresh 3x to make sure the numbers updated and then went to deep sleep for 1 hour.

I changed the code over the MQTT for speed.

Here is a whole discussion about it https://github.com/esphome/feature-requests/issues/46

Yeah - I get that. I use it on all my deep sleep devices.

I suggest you have a look at text sensor filters - the substitute one probably won’t work for your purpose so maybe a lambda one calling std::substr or std::replace, both of which work in ESPHome.

Could you give me an example how I could use std::substr or std::replace?
I tried the substitute method with

filters:
    - substitute:
      - '" -> '

It did work but doing this with more than 2 sensor values would make the whole thing crash.

Not sure why. Apply the filter at the sensor level, not globally. If this works it is much better than mucking about with lambda.

@zoogara thanks. I did apply it at sensor level. But I will try again, maybe I did something else wrong.