433mhz, infrared IR to and from MQTT on ESP8266

Absolutely…some remotes just don’t work …With some remotes just two buttons work. How can i train or add the code tth bridge?

Do you have some pictures of these remotes and the model?

I included your examples into the wiki thanks!

@1technophile thanks for sharing this and all the effort you put into this. This has some advantages over the rpi_rf that I was using as it running on a ESP8266 instead of a PI. The advantage is that I can have multiple gateways through my house (each floor) as I have some issues getting through the 60 cm walls with just one PI :slight_smile: I just need to make sure that the topics per floor are different

Now with some testing I found that your code doesn’t update the state topic subject433toMQTT “home/433toMQTT” when sending a command to a switch which give some issues in some of my test cases.

I understand that this is mainly caused by the fact that I’m using the setting “optimistic: false” instead of “true”. However currently the state topic is only providing a state based on receiving a signal and not after sending a signal which makes it useless in most cases.

Anyway I added the following lines to your sketch after line 330 and it is all working perfectly for my now :

// Update default state topic with the latest status = command 
String subject = String(subject433toMQTT);
String value = String(data);
trc(F("Updating default state topic in MQTT"));
trc(subject);
trc(value);
boolean resultUpdateST1 = client.publish((char *)subject.c_str(),(char *)value.c_str());
if (resultUpdateST1)trc(F("Update default state topic published"));

Now this only solves it for the 433toMQTT state topic not for any IR but you get the idea behind it.

Now as this state topic is overwritten as soon as a new command is given or received I also added an extra flag in the command topic that allows me to define a special state topic:

// MQTT state topic - This is the name of the state topic if a subject contains the StateTopicKey followed by the name of the state topic
#define StateTopicKey “ST_” // The state topic will be the value of subject433toMQTT and combined with the string after “ST_”
#define StateTopicEnd “$” // State Topic end key

The code for this is (also after line 330):

// Check for special state topic
//We look into the subject to see if a special state topic is defined
int posstart = topic.lastIndexOf(StateTopicKey); 
int posend = topic.lastIndexOf(StateTopicEnd); 
if (posstart != -1){ // State topic defined in subject
  posstart = posstart + +strlen(StateTopicKey);
  String StateTopic = (topic.substring(posstart,posend));
  trc(F("State Topic defined:"));
  trc(StateTopic);
  String subject = String(subject433toMQTT) + "/" + String(StateTopic);
  String value = String(data);
  trc(F("Updating special state topic in MQTT"));
  trc(subject);
  trc(value);
  boolean resultUpdateST2 = client.publish((char *)subject.c_str(),(char *)value.c_str());
  if (resultUpdateST2)trc(F("Update special state topic published"));
}

in my hass config the switch config looks like:

  • platform: mqtt
    name: “REV 1”
    state_topic: “home/433toMQTT/REV_1”
    command_topic: “home/commands/MQTTto433/433_1/PLSL_282/ST_REV_1$”
    payload_on: “14013452”
    payload_off: “14013443”
    optimistic: false
    qos: 0
    retain: true

This avoids copying the status to another sensor via automation and makes it a bit cleaner… bear in mind thought that if a RF remote is used the state will not be updated in that case so you could end up out of sync. I never use remotes though.

Hope this helps to give you some ideas :slight_smile:

Hello,

Thanks for the feedback!

I will take a look to your idea of updating the state topic when sending data to the gateway.

Regarding your multiple gateway installation could you point me out how it works exactly, you said that you’ve changed the topic, are you talking about in and out topic or only in?

easy by changing state topic subject433toMQTT from “home/433toMQTT” to “basement/433toMQTT” for the gateway in the basement, to “downstairs/433toMQTT” for downstais gateway, etc for first and second floor.

In HASS the sensors and switches etc… will look at state topic of the gateway per floor and also put the commands in the resp. gateway.

sure the gateway will pickup signals from RF remotes on the various floors and create MQTT signals but as the HASS sensors etc are only looking at the floor gateway these will not impact the behavior.

Hope this explains it

Great timing, I have just been looking into how to trigger state changes from MQTT messages to indicate to HA whether a device has been turned on / off. I’ll adapt this for my IR codes. Cheers

Hmmmm with the IR Codes i’m using the same code to basically toggle on/off, so I wont have separate payload values to define an on state and an off state.

So I will need to somehow keep track of the relative state in the arduino code to pass back to HA to acknowledge the state in HA.

Just wondering if anyone has done anything similar already? @ronvl @1technophile

Interesting no haven’t come across this yet
What I would do is add something to the RF code like _on and _off and strip this off the code in sketch before calling the RF function.

When you update the state topic you use the full code with the on and off.

@1technophile this you could also add to your sketch so other user will benefit from this as well

@ronvl @craigcarps Just to understand your needs, is the goal to have a kind of acknowledgement coming from the gateway saying that the signal has been sent?

Got my sender’s successfully added to home assistant.works like a charm so far. Just a question or two: is it somehow possible to increase distance and make it reacting faster
?

And I still see ir codes without having ir attached to the arduino.wondering which codes those are. Is it possible to completely deactivate this ir?

Hi
Thanks again to all that contributed to this project, is just amazing.
Just manage to send the codes to my plugs and all is working fine.
Thanks again

Hi

Ideally I’m keen to use the acknowledgement payload to change the state in Home Assistant to indicate whether the switch has been turned on / off.

I might get a chance over the weekend to trial some stuff with the Arduino code

What do you think if we republish the payload to a subsujects like home/commands/mqttto433/ack ? In this case i have just to replace the “ok” acknwledgemeny by the sent payload .

Thanks for your feedback !

1 Like

Increase the distance it depends on what is your current setup in terms of antenna, emitter voltage and emitter type. Could you describe what are your components? I will guide you to what could be improved

Did you modify the irremote library finaly as pointed in th wiki?

I think I did but as I’m totally new to thus stuff i dunno if i made it right . Could you show me how it should look like ? Don’t get me wrong 433 works perfectly i am just wondering how i get ir codes without ir mounted on the arduino

@thundergreen I made a fork with the modification

and updated the wiki, you were right it is now into IRremote/boarddefs.h

This will not work for HASS.
The State topic needs to have the same value as the values configured in HASS for on or off after a command has been executed.

For example my hass config looks like:

  • platform: mqtt
    name: “REV 1”
    state_topic: “home/433toMQTT”
    command_topic: “home/commands/MQTTto433/433_1/PLSL_282”
    payload_on: “14013452”
    payload_off: “14013443”
    optimistic: false
    qos: 0
    retain: true

so after i send the command 14013452 in “home/commands/MQTTto433/433_1/PLSL_282” the state topic has to change to 14013452 as ack and HASS will update the interface and shows the switch is turned on.

Currently you don’t update the state topic when a command is send. You only update the state topic when you receive an RF command i.e. if you use an RF remote.