Mysensors relay with button know the status HA is on or off

Hello guys, i have a doubt.
i see this code in mysensors.

RelayWithButtonActuator.ino

  • DESCRIPTION
  • Example sketch for a “light switch” where you can control light or something
  • else from both HA controller and a local physical button
  • (connected between digital pin 3 and GND).
  • This node also works as a repeader for other nodes
  • http://www.mysensors.org/build/relay
    */

// Enable debug prints to serial monitor
#define MY_DEBUG

// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69

// Enabled repeater feature for this node
#define MY_REPEATER_FEATURE

#include <SPI.h>
#include <MySensors.h>
#include <Bounce2.h>

#define RELAY_PIN 4 // Arduino Digital I/O pin number for relay
#define BUTTON_PIN 3 // Arduino Digital I/O pin number for button
#define CHILD_ID 1 // Id of the sensor child
#define RELAY_ON 1
#define RELAY_OFF 0

Bounce debouncer = Bounce();
int oldValue=0;
bool state;

MyMessage msg(CHILD_ID,V_LIGHT);

void setup()
{
// Setup the button
pinMode(BUTTON_PIN,INPUT);
// Activate internal pull-up
digitalWrite(BUTTON_PIN,HIGH);

// After setting up the button, setup debouncer
debouncer.attach(BUTTON_PIN);
debouncer.interval(5);

// Make sure relays are off when starting up
digitalWrite(RELAY_PIN, RELAY_OFF);
// Then set relay pins in output mode
pinMode(RELAY_PIN, OUTPUT);

// Set relay to last known state (using eeprom storage)
state = loadState(CHILD_ID);
digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
}

void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo(“Relay & Button”, “1.0”);

// Register all sensors to gw (they will be created as child devices)
present(CHILD_ID, S_LIGHT);
}

/*

  • Example on how to asynchronously check for new messages from gw
    */
    void loop()
    {
    debouncer.update();
    // Get the update value
    int value = debouncer.read();
    if (value != oldValue && value==0) {
    send(msg.set(state?false:true), true); // Send new state and request ack back
    }
    oldValue = value;
    }

void receive(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.isAck()) {
Serial.println(“This is an ack from gateway”);
}

if (message.type == V_LIGHT) {
// Change relay state
state = message.getBool();
digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
// Store state in eeprom
saveState(CHILD_ID, state);

 // Write some debug info
 Serial.print("Incoming change for sensor:");
 Serial.print(message.sensor);
 Serial.print(", New status: ");
 Serial.println(message.getBool());

}
}

my doubt is:
when i press the button and relay is ON home assistant knows the relays is on and put STATUS ON?

But if I have a button in the home assistant and click OFF … does the relay go off and the status in the home assistant also goes off?

if you connect and programm everything right, the state in HA will be the same as the relay.

You should be able to change state of the relay both using the physical button and from home assistant. To have reliable state updates in home assistant, always send feedback of a state change to home assistant and set optimistic to false in the config for mysensors in home assistant. You need to add a line in the end of receive function.

send(msg.set(state?1:0));

@martinhjelmare
you can tell me concretely where I put that line you told me.

I put this line below where?

@ReneTode thanks for your response.
so i can use my common switch for control relay.
but i have one more doubt… i have two wires.

this guy uses 3 wires:

and this guy uses 2 wire:

in my case, Should I keep an electrical installation like I have, or add one more wire?

sorry i cant follow those because there in portugues.
but i have another doubt for you.

you will never be able to get

  1. arduino
  2. switchrelay
  3. 240 to 5v adapter

all behind your switch.
if you make a box beside it it can work, but still wont be really cheap, because you need a decent 5v power adapter.
(if you find those for a decent price, please let me know, i need them for myselve :wink: )

but you only need the 2 wires from your actual switch to go into the relay.
but you need the third wire anyway to get power to your arduino. (you dont want to power your arduino witj batteries, allthough it is possible)

@ReneTode
wait… i dont put everthing behind my swich…
only put the relay
Then I take 3 thin strands up to another box you see.
I take the brown thread, the positive wire and the negative wire, to connect them to the Arduino.
In this box I can get by arduino and transformer. The wires pass well into the tube.

but if you want a mini transformer… i think is good for you

@ReneTode this is for you
HLK-PM01 https://www.youtube.com/watch?v=cee9S10PCdI

seach on youtube

your idea is false.

the only way to use the wallswitch and to let HA know what it is doing is if you connect the wallswitch to the arduino.

so the wallswitch will not have 240v connected anymore.

and then you need 8 thin wires for 2 switches and a relayboard.

  1. arduino gnd ==>> wallswitch 1 line in
  2. arduino gnd ==>> wallswitch 2 line in
  3. arduino pin 4 ==>> wallswitch 1 line out
  4. arduino pin 5 ==>> wallswitch 2 line out
  5. arduino gnd ==>> relayboard gnd
  6. arduino vcc ==>> relayboard vcc
  7. arduino pin 6 ==>> relayboard line 1
  8. arduino pin 7 ==>> relayboard line 2

live wire from wallswitch in ==>> centerpin relayboard out
wire from wallswitch out ==>> left or right pin relayboard out.

i looked at the HLK-PM01 but cant find them really affordable unless they are fake.

@ReneTode but i need 2 switches? I’m going crazy, I’ve been going there for many days to find a cheap solution to turning the lights on by the switch and the home assistant. And always know the status … as it is used in z wave. And I can not find anything … and all my ideas do not seem to work. It is very frustrating that in any forum anyone should use what I want to do.

no if just 1 of those wallswitches is used, you only need 1.
but why do you have 2 switches in the wall? normally for 2 lights.

whay you want is possible, but when you want it cheap you have to make some sacrefices.
or you have only 99% certainty that HA knows the right state,
or you will have to make a lot off extra efford,
or it wont we that pretty,
or …

you see me reakting a lot because like you i searched for the same things.
but on this time it isnt available very cheap and also 100% reliable.

it is possible with mysensors, but you need a box besides your wallswitches.
in that box you have:

  1. power transformer (near 10 euro for a reliable one)
  2. arduino (near 3 euro)
  3. nrf24 (near 1 euro)
  4. switchboard (near 3 euro)
  5. some small extra part (near 4 euro)

so thats 20 euro pro wallswitch

you can chose for 433 mhz switches they are a little cheaper (if you buy in china) and directly in the wall.
but you need to send the code a few times to up the reliability, and using the wallswitch would make it less reliable.

you could chose 433 mhz switches which use a remote at the place from the switch.
when you have a reciever which collects the codes right it can be pretty reliable.

or you need zwave for about 35 euro every switch and a zwave reciever who gives your codes to HA.

the other option is not to use wallswitches anymore. implement motiondetection, or other ways to switch the lights on.
that can be used in combination with a remotecontrol, or with a (small) tablet on the place from your lightswitches.

i will not have wallswitches anymore in the near future.
a tablet from 50 euro beside every door and i can switch on every light in the house with it.
when needed, because the lights will go on and off automaticly when needed.

@ReneTode.

Why do you think they are fake?

I ordered 5 and am waiting…

This guy made it work (supposed)

I think is should be a great solution, if not, we should consider creating another 2 wires 5V DC all over the house. Here in Brazil is common using plaster ceiling, where I could install safely cell phones charging plugs.

on the mysensors forum you can find quite a lot about these
and also that there are fakes, which are way less save on the market and how you can recognize them.

for that price and the source where you got them, i am 95% sure that those are fake.
that doesnt mean they are bad. or at least you cant be sure.

but i wouldnt risk it inside a wall.

i would love to make an entire network from 5v all around my house, but that would cost me 2 years and a great amount of money. (besides that you need expensive big transformers to start with.)

right now i use cheap transformers outside the wall at visible places.
i bought a set of 3 transformers which were bigger and more expensive to built inside my wall, the first one died within 24 hours.
making me even more carefull, allthough i am normally like: “the chances arent that big that something goes wrong”

please take pictures from them when you got them, then i will know for sure if i was right about those being fake. :wink:

by the way, when they started to talk about those transformers on mysensors, they were in the pricerange from 25 dollar.
now you can find them all around for 2 or 3 euro.
but i am quite sure that now there are more fakes then originals on the market.

i found another brand that is sold by a big company in Germany. they are also produced in china, but then at least the company is responsible to check that the specs are met. but they are also 11 euro.

1 Like

The HLK PM01 5V was never priced at 25$, to my knowledge, but was around 2.5-5 $ a year ago. There was 1-2 more expensive transformers mentioned that were in the price range ~ 25$. But those were not tested and reported in the forum, that I can recall. The HLK has been tested and that report is in the mysensors forum.

i guess the i did read something wrong. or interpreted something false.
yeah i have seen the report in the mysensors forum. thats why i started seaching.
but i also read about the fakes and some ways to see if they are fake.

then i started searching where to buy them and all i can find are some that are obvious fakes and a lot of sellers from which i am doubting if they sell what they show.

i would love to use some if i could find a reliable selling source.

In the case of HLK PM01 I don’t think you can judge a fake from the price only, since the price is so low to begin with. The fakes that has been reported had some design differences compared to samples that has been judged as non fakes. This is reported in the forum.

https://forum.mysensors.org/topic/1607/safe-in-wall-ac-to-dc-transformers/355

Your updated receive function should look something like this:

void receive(const MyMessage &message) {
  // We only expect one type of message from controller. But we better check anyway.
  if (message.isAck()) {
    Serial.println("This is an ack from gateway");
  }

  if (message.type == V_LIGHT) {
    // Change relay state
    state = message.getBool();
    digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
    // Store state in eeprom
    saveState(CHILD_ID, state);
    // Feedback state to controller
    send(msg.set(state?1:0));
    // Write some debug info
    Serial.print("Incoming change for sensor:");
    Serial.print(message.sensor);
    Serial.print(", New status: ");
    Serial.println(message.getBool());
} 

yeah i have seen the design differences.
i dont just judge on only price.
i look at the pictures and seek out or those pictures are not just copied from somewhere else.

some off them are quite easy to spot as fakes.

i know took my chances with the ones posted in the start of that topic.
all that can happen is that in over a month i see that i have thrown away 15 euro :wink:

great, thanks

Yes, i have 2 switches in the wall, but only one works, are two because when i buy it, theres no single,

but yes you have a reason.
“the other option is not to use wallswitches anymore. implement motiondetection, or other ways to switch the lights on.
that can be used in combination with a remotecontrol, or with a (small) tablet on the place from your lightswitches.”

i am going to that away, i will buy Sonoff and some slamphers and control them for RF, and motiondetection.
And put the remote control on the wall. Even if it turns ugly I do not want to know. My home is small and I can control by RF, so I’m pretty sure that the home assistant’s status will not fail often and will give the real state.

i want follow this, thanks @1technophile