IKEA Trådfri Hack: Zigbee bistate for Water Level

NOTE
This post is a Zigbee variant of post Shelly Uni: Wi-Fi bistate for Water Level

ESPECIAL REQUIREMENTS
This hack requires programming an ATtiny85. This hack assumes you already know how to program it.

BILL OF MATERIALS

  • 2pcs MOSFET 2N7002 SOT-23
  • 1pc 10 KOhm resistor
  • 1pc ATtiny85
  • 1pc 78L33 Linear voltage regulator
  • 1pc IKEA Trådfri switch E1743
  • 1pc Water level sensor XKC-Y25-NPN 5V-12V

ATTINY85 CODE

/*
  WATER LEVEL SENSOR TO ZIGBEE - ATtiny85
  Author: Tamadite
  Date: September 18th 2022

  ATTINY85:
    PB3: input from water sensor
    PB0: to OFF on IKEA Trådfri E1743
    PB1: to ON  on IKEA Trådfri E1743

  FUNCTIONALITY
    The ATtiny85 reads on pin A0 the Water Level Sensor (WLS) analog output. If ON (1024) then it sends a pulse (ms) on pin PB1.
    The code checks for WLS signal every ZIGBEE_PULSE milliseconds.
    waterStatusOn is used to keep the previous status avoiding repeatedly sending pulses when status has not changed.
*/

#define FROM_WLS          3
#define TO_ZIGBEE_OFF     0 
#define TO_ZIGBEE_ON      1
#define VOLTAGE_THRESHOLD 2.5  // below no water, above water
#define ZIGBEE_PULSE      333  // milliseconds

boolean waterStatusOn;

// the setup routine runs once when you press reset:
void setup() {
  // initialize digital pins output.
  pinMode(TO_ZIGBEE_OFF, OUTPUT);
  pinMode(TO_ZIGBEE_ON, OUTPUT);

  int wlsValue = analogRead(FROM_WLS);
  float wlsVoltage = wlsValue * (5.0 / 1023.0);
  // Trick to trigger the initialization of the status on the Zigbee device in the loop:
  if (wlsVoltage < VOLTAGE_THRESHOLD)
    waterStatusOn = true;
  else
    waterStatusOn = false;
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int wlsValue = analogRead(FROM_WLS);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float wlsVoltage = wlsValue * (5.0 / 1023.0);
  if ((wlsVoltage < VOLTAGE_THRESHOLD) && (waterStatusOn)) {    // Has the sensor changed to OFF (no water)?
    digitalWrite(TO_ZIGBEE_OFF, HIGH);
    waterStatusOn = false;
  }  
  if ((wlsVoltage >= VOLTAGE_THRESHOLD) && (!waterStatusOn)) {  // Has the sensor changed to ON (water)?
    digitalWrite(TO_ZIGBEE_ON, HIGH);
    waterStatusOn = true;
  }    
  delay(ZIGBEE_PULSE);
  digitalWrite(TO_ZIGBEE_OFF, LOW);
  digitalWrite(TO_ZIGBEE_ON, LOW);
}

THE BREADBOARD

THE IKEA TRÅDFRI E1743
Solder each mosfet transistor to each button on the IKEA Trådfri E1743 according to the picture. The gate pin on the transistor goes directly to the breadboard.

THE PROTOTYPE

HOME ASSISTANT

  1. Just pair the device with the Zigbee controller as usual and take note of the device Id e.g. 0x123456abdce7ef890
  2. Create an input boolean helper integration in the configuration.yaml as follows:
input_boolean:
  thereis_water:
    name: Keeps value of the water level sensor
    icon: mdi:hydraulic-oil-level
  1. To update the input boolean with the changes given by the sensor, add the following to automation:
# Water sensor TRUE
  - alias: There is water
    trigger:
      platform: state
      entity_id: sensor.0x123456abdce7ef890_action
      to: 'on'
    action:
      service: input_boolean.turn_on
      entity_id: input_boolean.thereis_water


# Water sensor FALSE
  - alias: There is no water
    trigger:
      platform: state
      entity_id: sensor.0x123456abdce7ef890_action
      to: 'off'
    action:
      service: input_boolean.turn_off
      entity_id: input_boolean.thereis_water

Please replace value 0x123456abdce7ef890 above by the Id of your IKEAE1743 Zigbee device.

FINAL NOTES

  1. You should use a 3v regulator if possible, e.g. 78L03, it should work as fine. Unfortunately, I didn’t have it at hand.
  2. As seen above, I use an ATtiny85 to capture the change of status of the WLS and send a pulse to the corresponding button on the IKEA Trådfri E1743. The ATtiny85 could be replaced by an “enhanced” RC circuit. I personally found the ATtiny85 alternative much simpler.
  3. This setup consumes 10 mAh when water is detected.
2 Likes