Change mqtt topic value to other text

I am reading an MQTT topic (Tank float low, float high and valve) which give me an ‘on’ or ‘off’ value. And it gets like that into the dashboard. However I would like to change these to ‘closed’ if on and ‘open’ if off. These topics are defined as binary sensors within HA.
Tried already several things but none worked.
Can someone point out to me how to do that?
Thx!

Screenshot 2024-05-21 150810

Screenshot 2024-05-21 150304

Easiest way would be to create a template binary sensor (UI, under Helpers) with a state template as follows (put the correct entity ID in):

{{ is_state('binary_sensor.tank_float_low','off') }}

and a device class of perhaps door or opening. I’ve set it to be the opposite way around because the convention for these sensors is that on is open.

Then show that sensor in the UI.

I think they are already binary sensors - if that is what they mean by “binary switch”. And if defined in YAML the state can be inverted with the value_template and the device class can be applied directly to them without the need for extra template binary sensors.

Sorry, indeed binary sensors. Corrected. Thx!

Can you elaborate a bit "can be inverted with the value_template and the device class can be applied directly to them " as I do not understand (not an HA expert nor experienced user :expressionless: but learning :wink: )

Share your mqtt binary sensor configuration.

    - name: "Greenhouse Tank Float Low"
      unique_id: greenhouse_tank_fltl
      state_topic: "engrie111/greenhouse/tank/float_low"
      payload_on: "on"
      payload_off: "off"
      
    - name: "Greenhouse Tank Float High"
      unique_id: greenhouse_tank_flth
      state_topic: "engrie111/greenhouse/tank/float_high"
      payload_on: "on"
      payload_off: "off"

    - name: "Greenhouse Tank Valve"
      unique_id: greenhouse_tank_valve
      state_topic: "engrie111/greenhouse/tank/valve"
      payload_on: "on"
      payload_off: "off"
    - name: "Greenhouse Tank Float Low"
      unique_id: greenhouse_tank_fltl
      state_topic: "engrie111/greenhouse/tank/float_low"
      value_template: "{{ value = 'off' }}"
      device_class: opening
      
    - name: "Greenhouse Tank Float High"
      unique_id: greenhouse_tank_flth
      state_topic: "engrie111/greenhouse/tank/float_high"
      value_template: "{{ value = 'off' }}"
      device_class: opening

    - name: "Greenhouse Tank Valve"
      unique_id: greenhouse_tank_valve
      state_topic: "engrie111/greenhouse/tank/valve"
      value_template: "{{ value = 'off' }}"
      device_class: opening

OK! That looks quite simple. I’ll give it a try.
Thx a lot!

Actually I think the value_template works a little differently from other binary sensors. So do this instead:

    - name: "Greenhouse Tank Float Low"
      unique_id: greenhouse_tank_fltl
      state_topic: "engrie111/greenhouse/tank/float_low"
      device_class: opening
      payload_on: "off"
      payload_off: "on"
      
    - name: "Greenhouse Tank Float High"
      unique_id: greenhouse_tank_flth
      state_topic: "engrie111/greenhouse/tank/float_high"
      device_class: opening
      payload_on: "off"
      payload_off: "on"

    - name: "Greenhouse Tank Valve"
      unique_id: greenhouse_tank_valve
      state_topic: "engrie111/greenhouse/tank/valve"
      device_class: opening
      payload_on: "off"
      payload_off: "on"

Does the job! Thx! Learned something today :slight_smile:

Yeah, that’s a better way if you have the sensors in YAML already. I was allowing for them being auto-discovered and thus not easily editable.

Oeps!. I just saw you switched also the values for payload_on and payload_off. OK. That should work then the right way. I’ll try it!

Indeed, this did not work

value_template: "{{ value = 'off' }}"

(it gave an error so changed it to value_template: "{{ value == 'off' }}"

Just adding the device_class: opening works fine for the valve but not for the float low and high as the logic there is inversed. However, the use of a template the way Troon explained does the job for these. A bit more “complicated” but it does the job :slight_smile:
Thx!

Yeah my mistake = assigns a value, == compares them. But as I said you don’t need the value template just reverse the payloads.