@adamp237
It sounds as you currently only have one arduino that acts both as a gateway and motion sensor and you don't have any other devices talking to the gateway via nrf radio. If so it's OK to have the gateway sleeping as long as you don't plan to send messages to it. But if you plan to send messages to the arduino via mqtt from home assistant and/or connect other arduino via radio to the gateway, the gateway is not allowed to sleep. You can use wait
instead to suspend execution.
This is the most basic switch example controlling a relay:
#include <MySensor.h>
#include <SPI.h>
#define SN "Relay"
#define SV "1.0"
#define CHILD_ID 1
#define RELAY_PIN 3
bool initialValueSent = false;
MyMessage msgRelay(CHILD_ID, V_STATUS);
void setup()
{
digitalWrite(RELAY_PIN, 0);
// Initialize the digital pin as an output.
pinMode(RELAY_PIN, OUTPUT);
}
void presentation() {
sendSketchInfo(SN, SV);
present(CHILD_ID, S_BINARY);
}
void loop()
{
if (!initialValueSent) {
Serial.println("Sending initial value");
send(msgRelay.set(0));
Serial.println("Requesting initial value from controller");
request(CHILD_ID, V_STATUS);
wait(2000, C_SET, V_STATUS);
}
}
void receive(const MyMessage &message)
{
if (message.type == V_STATUS) {
if (!initialValueSent) {
Serial.println("Receiving initial value from controller");
initialValueSent = true;
}
// Change relay state.
digitalWrite(RELAY_PIN, message.getInt() ? 1 : 0);
send(msgRelay.set(message.getInt() ? 1 : 0));
}
}
Are you using the mysensors component in home assistant or are you defining your sensor yourself directly using the mqtt component and mqtt sensor platform? If you're using the mysensors component you don't need to change anything in the home assistant configuration, you just need to present the mysensors switch properly to home assistant and home assistant will add it as a switch entity automatically.