Shelly Gen 2 as a cover via MQTT problem

Hi,
I realised that Shelly plus 2 PM uses different MQTT messages/topics compared to older Shelly 2.5 or Shelly 1.
I’m using it as a cover control. I tried to figure out what these new topics are but I cannot understand how to open/close the cover.
Up to now I just got that

mqtt:
  cover:
    - name: "tapparella_cucina1"
      unique_id: "shellyplus2pm-xxx"
      availability:
        topic: "shellies/shellyplus2pm-xxx/online"
        payload_available: "true"
      state_topic: "shellies/shellyplus2pm-xxx/status/cover:0"
      value_template: "{{ value_json.state }}"
      state_open: "open"
      state_closed: "close"
      position_topic: "shellies/shellyplus2pm-xxx/status/cover:0"
      value_template: "{{ value_json.current_pos }}"
      position_open: 100
      position_closed: 0
      command_topic: "shellies/shellyplus2pm-xxx/events/rpc"

How can I send open/close command? How can I set a specific position?

Thanks

2 Likes

I looked into this issue.
I found this link where I found some useful mqtt topic.
To send commands to the device it is needed to send them to <shelly-id>/rpc
Commands are messages described here for the cover.

So I changed my config:

mqtt:
  cover:
    - name: "tapparella_cucina1"
      unique_id: "shellyplus2pm-xxx"
      availability:
        topic: "shellies/shellyplus2pm-xxx/online"
        payload_available: "true"
      state_topic: "shellies/shellyplus2pm-xxx/status/cover:0"
      value_template: "{{ value_json.state }}"
      state_open: "open"
      state_closed: "close"
      position_topic: "shellies/shellyplus2pm-xxx/status/cover:0"
      value_template: "{{ value_json.current_pos }}"
      position_open: 100
      position_closed: 0
      command_topic: "shellies/shellyplus2pm-xxx/rpc"
      payload_open: '{"id":0,"method":"Cover.Open","params":{"id":0}}'
      payload_close: '{"id":0,"method":"Cover.Close","params":{"id":0}}'

But nothing happens when I push on the arrows in HA user Interface to open/close the cover.
Comman is correctly sento to mqtt server I can see it with an mqtt explorer but the shelly looks not to read it.
Any idea?

Thanks

The Shelly documentation is a little bit confused.
I managed to command the cover now.
The json payload must contain also a “src” field:

payload_open: '{"id":0,"src":"admin","method":"Cover.Open","params":{"id":0}}'
payload_close: '{"id":0,"src":"admin","method":"Cover.Close","params":{"id":0}}'

This fixed the problem

Moreover to have the position working it is needed to have an integer in position_template value:

  position_topic: "shellies/shellyplus2pm-xxx/status/cover:0"
  position_template: "{{ value_json.current_pos | int }}"
1 Like

Hi, thanks for sharing your config and solution but also your “solution” doesn’t work for me. I used your config an the HA and Shelly documentations as well as some “subscripe” and “publish” tests with the Mqtt.fx tool to put in some extensions. Now it ist possibile to control the cover with the following functions: open, close, stop, and the go to position.

Config-File: /config/mqtt.yaml

cover:
  - name: "Cover XXXXXXXXXX"
    unique_id: "shellyplus2pm-XXXXXXXXXX"
    availability_topic: "shellyplus2pm-XXXXXXXXXX/online"
    payload_available: "true"
    payload_not_available: "false"
    state_topic: "shellyplus2pm-XXXXXXXXXX/status/cover:0"
    value_template: "{{ value_json.state }}"
    state_opening: "opening"
    state_closing: "closing"
    state_stopped: "stopped"
    state_open: "open"
    state_closed: "closed"
    position_topic: "shellyplus2pm-XXXXXXXXXX/status/cover:0"
    position_template: "{{ value_json.current_pos | int }}"
    position_open: 100
    position_closed: 0
    command_topic: "shellyplus2pm-XXXXXXXXXX/rpc"
    payload_open: '{"id":0,"src":"shellyplus2pm-XXXXXXXXXX","method":"Cover.Open","params":{"id":0}}'
    payload_close: '{"id":0,"src":"shellyplus2pm-XXXXXXXXXX","method":"Cover.Close","params":{"id":0}}'
    payload_stop: '{"id":0,"src":"shellyplus2pm-XXXXXXXXXX","method":"Cover.Stop","params":{"id":0}}'
    set_position_topic: "shellyplus2pm-XXXXXXXXXX/rpc"
    set_position_template: '{"id":0,"src":"shellyplus2pm-XXXXXXXXXX","method":"Cover.GoToPosition","params":{"id":0,"pos":{{ position }}}}'

For me this works fine to control my covers over mqtt with the shelly 2 pm plus :slight_smile:

2 Likes

Thank you! works very well!
works 1 to 1 like shelly integrations control.

Another question is how can you get the sensor data via mqtt power, energy, device temperature… etc.?

Its not working to me, I can open and close the cover but onlt with position service. It´s not working close and open command

Any advice ?

You can also control Shelly 2pm without rpc over mqtt. In this case you only need to enable the following options in your shelly under MQTT settings:

  • Enable MQTT control
  • Generic status update over MQTT

In your MQTT yaml file you can configure like:

mqtt:
    cover:
      - name: "Shutter voor"
        unique_id: "Shutter_voor"
        qos: 1
        command_topic: "shellyplus2pm-shuttervoor/command/cover:0"
        payload_open: "open"
        payload_close: "close"
        payload_stop: "stop"
        position_topic: "shellyplus2pm-shuttervoor/status/cover:0"
        position_template: "{{ value_json.current_pos }}"
        position_closed: 0
        position_open: 100
        set_position_topic: "shellyplus2pm-shuttervoor/command/cover:0"
        set_position_template: "pos,{{ value }}"
        state_topic: "shellyplus2pm-shuttervoor/status/cover:0"
        state_opening: "opening"
        state_closing: "closing"
        state_open: "open"
        state_closed: "closed"
        state_stopped: "stopped"
        optimistic: false
        value_template: "{{ value_json.state }}"
1 Like

Thanks a million for this! I have a rather peculiar shutter setup that uses both an IR remote control (which I don’t use, by the way) and a regular two-button switch connected to a Shelly 2PM. To get it calibrated, I had to code all the logic for a “virtual cover” using Home Assistant and MQTT.

It was working fine with a Shelly 2.5, but I had to replace it with a 2PM. This is where your post was a lifesaver.

Now, I’m wondering if it’s possible to implement a sensor that listens to the physical buttons on the switch. I had it set up like this with the Shelly 2.5:

  sensor:
   - name: 'customcover_salonterraza_mqtt_open'
     state_topic: 'shellies/persiana-salonterraza/input/1'
   - name: 'customcover_salonterraza_mqtt_close'
     state_topic: 'shellies/persiana-salonterraza/input/0'