Newbie lack of understanding dumb MQTT device

Hello everyone, first post here after a successfull begining with HomeAssistant, so please be patient. I’ve been tinkering with HomeAssistant on a Raspberry Pi 4B for a couple of weeks now.

My main objective with HomeAssistant is to reduce/remove my reliance on lots of apps and cloud services by controlling everything from one source. I do not currently have many devices, mostly Philips Hue and a variety of smart plugs. Usual media devices, TV, set-top box and surround sound amp, these are controlled with a Harmony Hub. Plus one rather dumb vertical blind motor, which can be controlled with a Sonoff RF Bridge as well as buttons on the housing and a two button keyfob.

So all of these were working independantly and I wanted to simplify. So to date I have flashed the smart plugs with Tasmota and these are now all controllable with Home assistant. I have automations running that duplicate what I had before, except the blind motor

The dumb blind motor that I’ve had for a while, it is my intention to replace this at some point, but it is providing me with some “interesting” learning about HomeAssistant! When I first got the blind motor, the configuration was straight forward: pair the fob with the motor, set up eWeLink, connect with the Sonoff RF Bridge, set up the schedule(s), all was good.

Now I want to integrate the motor into HomeAssistant, The RF Bridge is a V1 and I’ve flashed it with Tasmota and Portisch, I have sniffed the codes from the fob and successfully used the codes to add a MQTT switch in configuration.yaml. The switch works fine from the entity dialog, when I “open” or “close” but I cannot add this to an automation, I need this entity to be a device???

In my current “test” automation I use Call Service with the MQTT payload which works, but this call does not reflect in the MQTT switch status. This dumb motor is really dumb, in that it does not return ANY status information so I have no idea what state the blinds/motor is in.

I’ve read lots of documents but am really stumped how to get this working. I’m guessing I’m looking at this the wrong way round or have not made the right connections in my head as yet!

You will need a template switch, with something to keep the status. An input boolean would do, but would not work if someone manually presses a button. Maybe you could add a door/window sensor to keep track if the blind is open or closed.

I don’t plan to keep this motor for long, whilst it does open and close, it will require a bit too much effort to make it clever. I’m still working my way up the learning curve and trying to understand how the bits of HomeAssistant work for me.

I suppose I could summarise my whole first post with this question: Is it possible to “make” a switch in HomeAssistant that would send the MQTT payload when operated. Then incorporate this switch in my automations?

See the template switch

One of my mqtt-based template switches as an example:

  - platform: template
    switches:
      boven_rflink:
        value_template: "{{ is_state('binary_sensor.sensorrflinkboven', 'on') }}"
        turn_on:
          - service: mqtt.publish
            data:
              payload: '10;newkaku;03a0b800;5;on;'
              topic: rflink/cmd
          - service: mqtt.publish
            data:
              payload: '{"SWITCH":"5","CMD":"ON"}' 
              topic: 'rflink/NewKaku-03a0b800'
        turn_off:
          - service: mqtt.publish
            data:
              payload: '10;newkaku;03a0b800;5;off;'
              topic: rflink/cmd
          - service: mqtt.publish
            data:
              payload: '{"SWITCH":"5","CMD":"OFF"}' 
              topic: 'rflink/NewKaku-03a0b800'
        unique_id: 23493375-dc5a-48de-92e7-1edab3994569      

That sounds like it’s what I’m after. I think I read that earlier, but I ignored it as I thought it was a template on which to base other hardware.

Where do you get the unique_id from? Is it system generated?

A job for tomorrow I think. Thank you.

It sounds like you already have a switch.

can you show the (properly formatted) code you used to create the switch? you might be able to go from there and make it maintain the status if it is tweaked.

As far as using a switch in an automation…

you don’t have to have a device (_id) to use it in an automation. you will use a state trigger instead of a device trigger.

it is generated from here: https://www.uuidgenerator.net/

And perhaps good to add why it’s done:

unique_id string (optional)

An ID that uniquely identifies this switch. Set this to a unique value to allow customization through the UI.

Here’s the code I had already added to my configuration.yaml

# My version of the example
switch:
  - platform: mqtt
    unique_id: blinds_lounge
    name: "Lounge Blinds"
    state_topic: "stat/tasmotaRF/STATE"
    command_topic: "cmnd/tasmotaRF/Backlog"
#    availability:
#      - topic: "tele/tasmotaRF/available"
    #Open
    payload_on: "RfRaw AAB0230408019003B60122288C3818181819281819281818181928181909081819081818190855; RfRaw 0"
    #Close
    payload_off: "RfRaw AAB0230408019003B6011828823818181819281819281818181928181929081819081819081855; RfRaw 0"
    state_on:  "OPEN"
    state_off: "CLOSE"
    optimistic: true
    qos: 0
    retain: true

This gives me two buttons in the entity dialog, off and on. What I’d ideally like is a simple On/Off button or even better Open/Close.

Now I can see this is very close to @francisp posted in the example above and won’t take much to alter. As I have created this entity can I just remove it from my configuration.yaml with any issues? I have some automations but they have the MQTT payload coded in them.

This is why I flashed the RF Bridge with Tasmota, the smart plugs didn’t have unique_ids and were difficult to control, plus I could then get rid of eWeLink.

So some more reading and this is where I’ve got to with the template as suggested by @francisp

# Example MQTT template
  - platform: template
    switches:
      blinds_lounge:
        friendly_name:"Lounge Blinds"
        value_template: "{{ is_state('cover.lounge_blinds', 'on') }}"
        turn_on:
          # Open
          - service: mqtt.publish
            data:
              topic: "cmnd/tasmotaRF/Backlog"
              payload: "RfRaw AAB0230408019003B60122288C3818181819281819281818181928181909081819081818190855; RfRaw 0"
          service: cover.open_cover
          target:
            entity_id: cover.lounge_blinds
        turn_off:
          # Close
          - service: mqtt.publish
            data:
              topic: "cmnd/tasmotaRF/Backlog"
              payload: "RfRaw AAB0230408019003B6011828823818181819281819281818181928181929081819081819081855; RfRaw 0"
          service: cover.close_cover
          target:
            entity_id: cover.lounge_blinds
        unique_id: ????
        icon_template: >-
          {% if is_state('cover.lounge_blinds', 'open') %}
            mdi:blinds
          {% else %}
            mdi:blinds-open
          {% endif %}

Still not entirely sure about yaml formating and syntax, am I doing OK?