6

I would like to send values from the HomeAssistant to an ESP8266 via MQTT. I have already successfully done it the other way around (sending temperature values from the ESP to HomeAssistant via MQTT) however now I would like to be able to send values via the HomeAssistant interface to the ESP.

Is this possible to do this (is there a component like sensor or switch that does this)?

I'd be very happy for any help you could give me and feel free to ask if anything is unclear.

2 Answers 2

2

If you have sent data from the device to the HomeAssistant over MQTT, then you can do it for sure the other way around. MQTT is completely bidirectional, there is no notion of server and clients as in HTTP, but broker and clients, and all clients are first class citizens.

You have to determine what topic the HomeAssistant uses to send the data (let's say /my-cool-device/commands), and you have to configure the device so that it subscribes to /my-cool-device/command, and handles incoming messages.

1

I use HA to trigger a topic update push from the ESP8266.

In the ESP8226 code, in setup, register a callback function for subscribing to a topic. mqttClient.setCallback(callback);

In that MQTT callback function, read topic and payload messages, and on a match, execute your ESP8266 code.

 if (strcmp(topic, inTopicPub) == 0) {// fishtank/pub
    pubAll();
  }

On code initialization, create a topic name: const char* inTopicPub = "fishtank/pub";

In the MQTT connect sequence, register a subscription request for that topic. mqttClient.subscribe(inTopicPub);

In .homeassistant/configuration.yaml create a switch entry:

switch 9:
  - platform: mqtt
    name: "Synch"
    command_topic: "fishtank/pub"
    payload_on: "91"
    payload_off: "90"
    qos: 0
    retain: false

Reload HA to use changed configuration.

Edit HA dashboard add a button card (hopefully populated from the dropdown choices).

type: button
tap_action:
  action: toggle
entity: switch.synch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.