Using mockmysensors as a guide I tried to combine a pir motion sensor with one led. The motion sensor is being picked up by home assistant fine but its seeing the led as a switch and not a light and won't turn the light on. I don't need the led to be dimmable, just on and off. I'm going to be using both in automations once I get the code right.
I've tested all the parts using basic arduino coding to make sure they work. I already have a gateway set up, and a pir motion sensor elsewhere in my home that is working and has been used in its own automations.
I've watched videos and searched google trying to figure out how to do this, at this point I think I need a bit of help sorting this out.
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_RF24
//#define MY_RADIO_NRF5_ESB
//#define MY_RADIO_RFM69
//#define MY_RADIO_RFM95
#define MY_NODE_ID 5
#include <MySensors.h>
// Wait times
#define LONG_WAIT 500
#define SHORT_WAIT 50
#define SKETCH_NAME "My Kitchen"
#define SKETCH_VERSION "5"
// Define Sensors ids
#define ID_S_MOTION 1
#define ID_S_LIGHT 2
// Define input output sensors
#define MOTION_DIGITAL_INPUT_SENSOR 3
#define LIGHT_PIN 8
// Global Vars
uint32_t SLEEP_TIME = 9000; // Sleep time between reads (in milliseconds)
#define LIGHT_OFF 0
#define LIGHT_ON 1
int16_t last_state = LIGHT_OFF;
//Instantiate Messages objects
#ifdef ID_S_MOTION // V_TRIPPED, V_ARMED
MyMessage msg_S_MOTION_T(ID_S_MOTION, V_TRIPPED);
#endif
#ifdef ID_S_LIGHT
MyMessage msg_S_LIGHT(ID_S_LIGHT, V_LIGHT);
bool isLightOn = 1;
#endif
void setup()
{
pinMode(MOTION_DIGITAL_INPUT_SENSOR, INPUT);
pinMode(LIGHT_PIN, OUTPUT);
wait(LONG_WAIT);
Serial.println("GW Started");
}
void presentation()
{
// Send the Sketch Version Information to the Gateway
Serial.print("Send Sketch Info: ");
sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
Serial.print(SKETCH_NAME);
Serial.println(SKETCH_VERSION);
wait(LONG_WAIT);
// Register all sensors to gw (they will be created as child devices)
Serial.println("Presenting Nodes");
Serial.println("________________");
#ifdef ID_S_MOTION
Serial.println(" S_MOTION");
present(ID_S_MOTION, S_MOTION, "Kitchen Motion");
wait(SHORT_WAIT);
#endif
#ifdef ID_S_LIGHT
Serial.println(" S_LIGHT");
present(ID_S_LIGHT, S_LIGHT, "Kitchen Night Light");
wait(SHORT_WAIT);
#endif
Serial.println("________________");
}
void loop()
{
Serial.println("Request Time");
requestTime();
wait(LONG_WAIT);
#ifdef ID_S_MOTION
void motion();
{
// Read digital motion value
bool tripped = digitalRead(MOTION_DIGITAL_INPUT_SENSOR) == HIGH;
Serial.println(tripped);
send(msg_S_MOTION_T.set(tripped ? "1" : "0")); // Send tripped value to gw
// Sleep until interrupt comes in on motion sensor. Send update every two minute.
sleep(digitalPinToInterrupt(MOTION_DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
}
#endif
{
#ifdef ID_S_LIGHT
void light();
Serial.print("Light is: " );
Serial.println(isLightOn ? "On" : "Off");
send(msg_S_LIGHT.set(isLightOn));
}
#endif
}
void receive(const MyMessage & message)
// case V_STATUS: // V_LIGHT:
#ifdef ID_S_LIGHT
{
//For this example, just print the light status to console.
if ( last_state == LIGHT_OFF ) {
Serial.println( "Light state: OFF" );
} else {
Serial.print( "Light state: ON, Level: " );
}
}
#endif
// default:
// Serial.print("Unknown/Unimplemented message type: ");
// Serial.println(message.getType());
// }