Help with MQTT Sensor...again

Hi all,

I received support for a similar request in the past, now I changed Valetudo version (from RE to regular Valetudo) and of course logic is different.

What I want achieve is the same result as before: create a sensor that highlight which room my S50 is cleaning.

This is the topic that according with the docs store the info:

valetudo/R2-D2/MapSegmentationCapability/clean

For example when it in my kitchen (“Cucina” I see this:

{"segment_ids": ["21"], "iterations": 3, "customOrder": true}

I know that 21 means kitchen based on a sensor that is exposed already in HA, in my case called “sensor.valetudo_r2_d2_map_segments” that has the following attributes:

'16': Studio
'17': Camera da Letto
'18': Bagno Secondario
'19': Soggiorno
'20': Bagno Principale
'21': Cucina
'23': Cameretta
'24': Corridoio
icon: mdi:vector-selection
friendly_name: Map segments

I tried to use my (poor) knowledge on template and MQTT with the following tentative:

    - state_topic: "valetudo/robot/MapSegmentationCapability/clean"
      name: "Pulizia Zona"
      value_template: "{{ value_json.segment_ids[] }}"

but of course it does not works and status is “unknown”.

After this first step my final goal is to translate segment ids in room names.
I was thinking something like this:

    - state_topic: "valetudo/robot/MapSegmentationCapability/clean"
      name: "Pulizia Zona"
      value_template: >-
        {% set val =  value_json.segment_ids  %}
        {% if val == '21' %}
          Cucina
        {% elif val == '19' %}
          Soggiorno
        {% elif val == '16' %}
          Studio
        {% elif val == '18' %}
          Bagno Secondario
        {% elif val == '17' %}
          Camera da Letto
        {% else %}
          Sconosciuta
        {% endif %}

Any idea?

Thank you in advance!

@123 can you help me once again? :slight_smile:

    - state_topic: "valetudo/robot/MapSegmentationCapability/clean"
      name: "Pulizia Zona"
      value_template: >-
        {% set rooms = {
         '16': 'Studio', '17': 'Camera da Letto', '18': 'Bagno Secondario',
         '19': 'Soggiorno', '20': 'Bagno Principale', '21': 'Cucina',
         '23': 'Cameretta', '24': 'Corridoio' } %}
        {{ rooms.get(value_json.segment_ids | first, 'Unknown') }}
1 Like

WOW! That was fast!

I checked and I still see unknown. I noticed that I posted the wrong topic so I repost below all the data (in case I miss something else in the topic or some other detail):

Full topic via MQTT-Explorer

valetudo/R2-D2/MapSegmentationCapability/clean

Actual status via MQTT-Explorer

{"segment_ids": ["17","23"], "iterations": 3, "customOrder": true}

What I see in MQTT- Explorer

The last sensor in my config.yaml

mqtt:
  sensor:
    - state_topic: "valetudo/R2-D2/MapSegmentationCapability/clean"
      name: "Pulizia Zona"
      value_template: >-
        {% set rooms = {
          '16': 'Studio', '17': 'Camera da Letto', '18': 'Bagno Secondario',
          '19': 'Soggiorno', '20': 'Bagno Principale', '21': 'Cucina',
          '23': 'Cameretta', '24': 'Corridoio' } %}
        {{ rooms.get(value_json.segment_ids | first, 'Unknown') }}

Thank you (once again)!

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

Sure I’ll do @123 . At the moment I still see unknown as status.

Sorry, I misunderstood your previous reply. I thought it was working because you thanked me for it.

The value in segment_ids is a list and the first filter gets the first item in the list. So if segment_ids contains ["17", "23"] then the first item is "17".

You can try this version but I have my doubts it will fix the problem:

      value_template: >-
        {% set rooms = {
          16: 'Studio', 17: 'Camera da Letto', 18: 'Bagno Secondario',
          19: 'Soggiorno', 20: 'Bagno Principale', 21: 'Cucina',
          23: 'Cameretta', 24: 'Corridoio' } %}
        {{ rooms.get(value_json.segment_ids | first | int(0), 'Unknown') }}

Tried and no changes…

Why you have doubts? Do you think is not possible?

The doubts I had were about changing the received value, and dictionary keys, from string to int. As expected, it made no difference.

Based on the information you have provided, the template should work. The topic appears to contain a payload, the payload contains data in JSON format, the key named segment_ids contains a list, etc.

As an experiment, try this just to see what it finds in segment_ids.

      value_template: >-
        {{ value_json.segment_ids | first }}

If it reports nothing then check the Log for any related errors.

Nothing changes. It is unknown. Like it does not exist at all. The weird thing is that I cannot neither delete from entities list.

If it reports unknown then it means it has not received a value from valetudo/R2-D2/MapSegmentationCapability/clean.

maybe because is not retained? I ask because I closed MQTT-Explorer and when I reopened it the topic MapSegmentationCapability is missing.

ok so quick update. The issue (the first one) is that the message was not retained.
So i added retain = true and now it survives in my mqtt broker.

With that I can finally see latest room cleaned (I changed also topic because the command message ends with /set).

Here it is for the others out there using Valetudo:

      - service: mqtt.publish
        data:
          topic: valetudo/R2-D2/MapSegmentationCapability/clean/set
          payload: '{"segment_ids": ["21"], "iterations": 3, "customOrder": true}'
          retain: true

Below MQTT sensor:

    - state_topic: "valetudo/R2-D2/MapSegmentationCapability/clean/set"
      name: "Ultima Zona Pulita"
      value_template: >-
        {% set rooms = {
          '16': 'Studio', '17': 'Camera da Letto', '18': 'Bagno Secondario',
          '19': 'Soggiorno', '20': 'Bagno Principale', '21': 'Cucina',
          '23': 'Cameretta', '24': 'Corridoio' } %}
        {{ rooms.get(value_json.segment_ids | first, 'Unknown') }}

I can also send vacuum cleaning multiple rooms, in this case @123 can you help me modify the value template with the join magic so I can see as state for example “Studio, Camera da Letto”?

The TOPPPPPPP would be “Studio AND Camera da Letto” or “Studio, Camera da Letto AND Soggiorno”.

    - state_topic: "valetudo/R2-D2/MapSegmentationCapability/clean/set"
      name: "Ultima Zona Pulita"
      value_template: >-
        {% set rooms = {
          '16': 'Studio', '17': 'Camera da Letto', '18': 'Bagno Secondario',
          '19': 'Soggiorno', '20': 'Bagno Principale', '21': 'Cucina',
          '23': 'Cameretta', '24': 'Corridoio' } %}
        {% set ns = namespace(rooms=[]) %}
        {% for r in value_json.segment_ids %}
          {% set ns.rooms = ns.rooms + [rooms.get(r, 'Unknown')] %}
        {% endfor %}
        {{ (ns.rooms | join(', ')).rsplit(', ', 1) | join(' and ') }}
1 Like

Great! It works! Thank you very much!

1 Like