Configure my Sonoff Relay to open my door as a secure device (Google Assistant asking for PIN)

Hi guys,

I have a sonoff 5v (tasmota) relay that I use to open my front door. For security reasons, I decided to leave it out of my google home interface.

I heard there is a way to set it as a secure device and then Google Assistant will automatically ask for a password/PIN. I tried looking at documentation but I did not find any examples on how to do it.

I heard it is related to setting the device as a generic cover on configuration.yaml. Does anyone know how to do it?

Hi Leo,

I used a SonoffSV for my garage door opener & in my case it’s added to HA as an MQTT cover entity. Other than that i didn’t do anything special for the device config. However, i am using the Home Assistant Cloud component with the Google Assistant feature enabled. In the Google Assistant feature you can set a pin code for ‘security devices’ so i guess that a cover entity type is automatically defined as a security device. It just worked out of the box for me.

This is my MQTT cover config.

- platform: mqtt
  name: "sonoffsv_02"
  unique_id: sonoffsv_02
  state_topic: "cmnd/sonoffsv_02/POWER2"
  command_topic: "cmnd/sonoffsv_02/POWER"
  qos: 1
  payload_open: "ON"
  payload_close: "ON"
  payload_stop: "ON"
  state_open: "OPEN"
  state_closed: "CLOSED"
  retain: false
  device_class: garage

I highly recommend the Nabu Casa subscription if you don’t already.

Cheers
Nick

Thank you! I will try to adapt your code to my situation. I will let you know the results.

It did not work. HA shows the status of the device as unavailable.

Isn’t there an easier way to do this? I have everything working for more that 1 year. I mean, I have a tasmota relay configured as a “switch” to receive a MQTT command to activate the relay. Then, I programmed the firmware to keep the “switch” on for about 200 ms. It will then emulate a quick button press that is necessary to open my door.

I just wanted google assistant to ask for a PIN before activating the mechanism.

cover:
  - platform: mqtt
    name: "Porta de Entrada"
    state_topic: "stat/RELE_Fechadura/POWER"
    command_topic: "cmnd/RELE_Fechadura/POWER"
    availability_topic: "tele/RELE_Fechadura/LWT"
    qos: 1
    payload_open: "ON"
    payload_close: "ON"
    payload_stop: "ON"
    state_open: "OPEN"
    state_closed: "CLOSED"
    retain: false
    device_class: door

If it is configured as a cover then it should require a pin from google assistant. You have not told us how your device is configured.

1 Like

Sure! Which settings do you nee to see? You means Tasmota’s, right? Which command should I run in the console?

12:11:05 MQT: tele/RELE_Fechadura/STATE = {"Time":"2021-06-26T12:11:05","Uptime":"1T19:50:12","UptimeSec":157812,"Heap":26,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":3,"POWER":"OFF","Wifi":{"AP":1,"SSId":"######","BSSId":"#########","Channel":4,"RSSI":100,"Signal":-50,"LinkCount":1,"Downtime":"0T00:01:37"}}
12:14:25 CMD: Status0
12:14:25 MQT: stat/RELE_Fechadura/STATUS = {"Status":{"Module":1,"DeviceName":"Tasmota","FriendlyName":["Tasmota"],"Topic":"RELE_Fechadura","ButtonTopic":"0","Power":0,"PowerOnState":0,"LedState":1,"LedMask":"FFFF","SaveData":1,"SaveState":1,"SwitchTopic":"0","SwitchMode":[0,0,0,0,0,0,0,0],"ButtonRetain":0,"SwitchRetain":0,"SensorRetain":0,"PowerRetain":0}}

Doed it appear in home assistant as a lock, or a cover or as a switch?

My original configuration.yaml was:

switch:
  - platform: mqtt
    name: "Porta de Entrada"
    state_topic: "stat/RELE_Fechadura/POWER"
    command_topic: "cmnd/RELE_Fechadura/POWER"
    availability_topic: "tele/RELE_Fechadura/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"

So, it was shown as a switch and it worked perfectly (without asking for PINs)

Now I just tried to change my configuration.yaml file according to what nickh66 said. It now reads as:

cover:
  - platform: mqtt
    name: "Porta de Entrada"
    state_topic: "stat/RELE_Fechadura/POWER"
    command_topic: "cmnd/RELE_Fechadura/POWER"
    availability_topic: "tele/RELE_Fechadura/LWT"
    qos: 1
    payload_open: "ON"
    payload_close: "ON"
    payload_stop: "ON"
    state_open: "OPEN"
    state_closed: "CLOSED"
    retain: false
    device_class: door

If I go to Home Assistant Developer Tools > State > Entities, I can see a cover entity with a door icon. The entity’s state is shown as ‘unavailable’.

I haven’t tried an mqtt cover, but have you been here? MQTT Cover - Home Assistant

Also I am confused if this should be a lock or a cover? Does the tasmota device actually open and close the door, or does it just latch and unlatch it? Because there is also this MQTT Lock - Home Assistant

Hi Leo,

I’m curious about a couple of things in your configuration.

  1. Have you confirmed with a tool like MQTT Explorer that the device is communicating successfully to the topics you defined.
  2. Did you add a door open/close sensor (reed switch perhaps) to your sonoff device to know when the door is actually in the opened or closed state.
    I did have troubles getting my config to a point where it was working correctly for my needs. I did take a lot of trial & error with both the mqtt cover config & to tweak some tasmota settings.

But in the end none of that made any difference to how the google home interacts with HA. For me, when it became cover then it automatically worked. Not sure what other advice i can give in this instance.

Nick

How about just keeping the switch configuration as originally configured, then use a template lock to template that as a lock. Then don’t export the switch and export the lock to Google Assistant for the lock and pin function.

Below assumes you turn on the switch to unlock the door and turn off the switch to lock the door (you can invert it if that is opposite of what you use).
If it is just a pulsed on (inching switch, etc.) then turns off, then the lock action doesn’t really matter so you can leave it in or blank out the action (leave lock: there though)

lock:
  - platform: template
    name: "Porta de Entrada"
    value_template: "{{ is_state('switch.porta_de_entrada', 'on') }}"
    unlock:
      service: switch.turn_on
      target:
        entity_id: switch.porta_de_entrada
    lock:
      service: switch.turn_off
      target:
        entity_id: switch.porta_de_entrada

Check the documentation for template lock for details

2 Likes

It works perfectly! Thank you!