Dynamic Icon for MQTT cover

I have just changed my old MQTT cover code to the newer format but the new format does not allow for templated icons to show the current cover status. If I use a normal icon config I don’t get a dynamic icon relating to the cover status.

This old code is no longer valid with the new way of configuring MQTT covers as they only allow for a static icon: parameter.

              icon_template: >-
                {% if is_state('binary_sensor.paradox_z3_garage_door', 'off') %}
                  mdi:garage-variant
                {% else %}
                  mdi:garage-open-variant
                {% endif %}

What are people doing to get around this?

Can’t you just add a device class?

https://www.home-assistant.io/integrations/cover/#device-class

I’ve got that, it just stays on the open door variant all the time…

device_class: garage

There must be something else wrong with it I guess. I’ll go back and investigate some more.

Problem found, kinda: This template shows the correct state in the dev tool but the cover entity is showing ‘unknown’.

mqtt:
  cover:
    - name: Garage Door
      device_class: garage
      value_template: "{{is_state('binary_sensor.paradox_z3_garage_door', 'on')}}"

Which is then displaying the open door icon.

Any idea why this is showing ‘unknown’?

The value_template here is used to interpret what you receive on the topic (which you have not specified).

Do you need a template cover instead of an mqtt cover?

Sorry, I should have just posted the full config.

mqtt:
  cover:
    - name: Garage Door
      device_class: garage
      value_template: "{{is_state('binary_sensor.paradox_z3_garage_door', 'on')}}"
      state_topic: 'paradoxdCTL/out'
      command_topic: 'paradoxdCTL/in'
      payload_open:
          '{
            "password": "xxxx",
            "Command": "PGM_ON",
            "Subcommand": "2"
          }'
      payload_close:
          '{
            "password": "xxxx",
            "Command": "PGM_ON",
            "Subcommand": "2"
          }'
      payload_stop:
          '{
            "password": "xxxx",
            "Command": "PGM_ON",
            "Subcommand": "2"
          }'

What do you receive on this topic?

state_topic: 'paradoxdCTL/out'

Because it will be ignored and replaced with true or false depending on this:

value_template: "{{is_state('binary_sensor.paradox_z3_garage_door', 'on')}}"

And it’s actually looking to match these defaults (which “true” or “false” don’t match):

I actually receive all of the Paradox Alarm system status / state changes on that topic, so all sensors changing etc.

The cover entity doesn’t actually need that state_topic since it should just use it’s status based on the template sensor, however if I comment out the state_topic line the cover throws an error and won’t load in HA which is why I put it in there (docs do say it’s optional hence it passes config check when commented out)

error:

Manually configured MQTT sensor(s) found under platform key ‘sensor’, please move to the mqtt integration key, see MQTT Sensor - Home Assistant

2022-06-16 04:52:15 ERROR (MainThread) [homeassistant.config] Invalid config for [cover]: ‘value_template’ must be set together with ‘state_topic’… Got OrderedDict([(‘name’, ‘Garage Door’), (‘device_class’, ‘garage’), (‘value_template’, “{{is_state(‘binary_sensor.paradox_z3_garage_door’, ‘on’)}}”), (‘command_topic’, ‘paradoxdCTL/in’), (‘payload_open’, ‘{ “password”: “xxxx”, “Command”: “PGM_ON”, “Subcommand”: “2” }’), (‘payload_close’, ‘{ “password”: “xxxx”, “Command”: “PGM_ON”, “Subcommand”: “2” }’), (‘payload_stop’, ‘{ “password”: “xxxx”, “Command”: “PGM_ON”, “Subcommand”: “2” }’)]). (See /config/packages/garage_door.yaml, line 6).

Yeah if you want to use a value template you have to have a state topic. It’s just that your value template is resolving to true or false when the default values it is looking for are: closed and open. Either specify the state_open: false and state_closed: true options (See my screenshot above) or use this value template:

value_template: "{{'open' if is_state('binary_sensor.paradox_z3_garage_door', 'on') else 'closed'}}"
1 Like

I tried a few other options such as specifying:

      state_topic: 'paradoxdCTL/hassio/zone3'
      state_open: "OFF"
      state_closed: "ON"

Which seemed to be what I should have used from the start, but it still didn’t work.

Your template above works perfectly, thanks mate. :+1:

EDIT: just to clarify, Tom’s template works to resolve the cover status yet the config still requires a state_topic be configured which seems like a bug given that the state_topic in this case is not helping in any way but needs to have something written in it for the sake of the component loading.

Well no, it’s not a bug. If you use a value_template the integration expects to receive something on the state_topic. The only way to not include a state topic is to not include a value_template. Then the integration defaults to optimistic mode.

I think I spoke too soon… After opening and closing the garage door a few times the cover entity is showing open instead of closed.

Then why can’t we simply use the value_template which uses the status from another entity rather than getting that status via MQTT? It seems those two things (value_template vs state_topic) are trying to achieve the same thing…?

I’m thinking I’ll actually try swapping this for a template cover instead and just use service calls for sending out the MQTT commands, it might be easier for my use case.

Then use this value template:

value_template: "{{'closed' if is_state('binary_sensor.paradox_z3_garage_door', 'on') else 'open'}}"

It’s an MQTT cover. It was written to expect a state on a state topic. Interpreting that state can be done with a value_template and two other options that define the open and closed messages.

I swapped the MQTT cover for a template cover, basically using my old MQTT cover code (the deprecated method) and so far it looks to be working. I’ll run some tests.

cover:
  - platform: template
    covers:
      garage_door:
        device_class: garage
        friendly_name: "Garage Door"
        value_template: "{{is_state('binary_sensor.paradox_z3_garage_door', 'on')}}"
        open_cover:
          - service: mqtt.publish
            data:
              topic: 'paradoxdCTL/in'
              payload:
                '{
                  "password": "xxxx",
                  "Command": "PGM_ON",
                  "Subcommand": "2"
                }'
        close_cover:
          - service: mqtt.publish
            data:
              topic: 'paradoxdCTL/in'
              payload:
                '{
                  "password": "xxxx",
                  "Command": "PGM_ON",
                  "Subcommand": "2"
                }'
        stop_cover:
          - service: mqtt.publish
            data:
              topic: 'paradoxdCTL/in'
              payload:
                '{
                  "password": "xxxx",
                  "Command": "PGM_ON",
                  "Subcommand": "2"
                }'