Zigbee > MQTT > HA, but how to HA > MQTT > Zigbee?

Hi guys!

In this moment, I use a tasmotized sonoff zigbee with HA through MQTT.

If I want to see the status of a zigbee sensor, I can use a template like

if trigger.payload_json['ZBReceived']['Data'] == '889UUAI'

and I know that the sensor have been triggered

My question is: how can I activate a zigbee swtich with template, from home assistant?

Thank you!

you need to create an MQTT switch in HA using the topics from Tasmota.

Then you can control it just like any other switch.

1 Like

Ok, I understand. But how can I send something to tasmota? Tasmota can also listen for topics, not only publish? If yes, how can I use a template for publishing to tasmota?

Thank you in advance!

it depends on the device and how it’s set up.

A sensor likely won’t subscribe (“listen too”) any topics except for the standard birth/will topics from other clients in the mqtt network.

A switch will both publish status topics to the MQTT broker but also subscribe to command topics from the broker to control it’s functions.

you will need to figure out what the command topics are for your particular tasmotized device in order to know what topic to publish the command to that the device uses and what the payload that will be to execute the command.

Tasmota has changed the standard status and command topic structure over time and it also has the ability to customize those topics so I can’t for sure say what the topic & payload will be.

But my couple of Tasomtized devices use the following example for configuration:

switch:
  - platform: mqtt
    name: "Garage Door South Op MQTT"
    state_topic: "stat/s_gd_op/POWER"
    command_topic: "cmnd/s_gd_op/POWER"
    availability_topic: "tele/s_gd_op/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false

in there you can see the topics and payloads expected for my tasmotized switch.

in that example if I click on the switch in the front end to turn it on or call the switch.turn_on service for that switch then the system will publish “ON” to the command topic “cmnd/s_gd_op/POWER” to the broker (which the switch also subscribes to) and the switch is turned on. It will then send it’s updated status to the broker via the status topic “stat/s_gd_op/POWER” which HA subscribes to and HA knows what the new status of the switch is.

there is no “template” required.

In the case of the sensor you could do the same thing but sensors typically will send more information out in the status topic that you then need to extract the desired value from by using a template as you have in your example above.

But the template above is doing nothing but listening for info on the status topic then you are using the trigger state object to extract the desired value out of the delivered json string.

1 Like

Oh, it is more complicated than I thought, but I will try to see if I can manage this to work. Thank you for all these informations.

the other thing that might make it easier is that you can enable MQTT discovery so that it automatically configures the switch for you.

I’ve never needed to use it but you can google it.

it’s going to be something like “setOption19” but I can’t remember specifically.

You are right SetOption19 1
Thanks!

I manage to work it :smiley:

Just publish this payload “ZbSend {“Device”:“Computer_Heater”,“Write”:{“TuyaTempTarget”:1}}” to the topic and my device (which is a thermostat with switch), will set the temperature to the exact value. As far for the sensors, it is easy to read. I didn’t know how to send command. Thanks for your advices man, cya!

1 Like