Deprecated: Function jetpack_form_register_pattern is deprecated since version jetpack-13.4! Use Automattic\Jetpack\Forms\ContactForm\Util::register_pattern instead. in /var/www/html/wp-includes/functions.php on line 6078 Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-includes/functions.php:6078) in /var/www/html/wp-includes/feed-rss2.php on line 8 ESPHome – D Khaz https://khaz.me Things that I spend my time on... Thu, 28 Sep 2023 23:47:07 +0000 en-US hourly 1 https://khaz.me/wp-content/uploads/2018/05/DK_logo_square___100x100.png ESPHome – D Khaz https://khaz.me 32 32 147286311 Flashing an ESP32-S2 Mini on a Mac https://khaz.me/flashing-an-esp32-s2-mini-on-a-mac/ Fri, 29 Sep 2023 00:46:39 +0000 https://khaz.me/?p=618

Unfortunately, flashing the newer ESP32 devices is a bit more of a pain than the good ol days of the ESP8266 (RIP ). While trying to flash this device my typical go-to method was to use the recently deprecated esphome-flasher. However, it doesn’t work.

Per the directions in the chart below, you should be in Device Firmware Upgrade (DFU) Mode, but this did not help me.

Success

Ultimately, I was able to flash it via CLI using the trusty esptool.py. I used a modern FW build from ESPHome. The command that worked for me was the following:

esptool.py --port /dev/tty.usbmodem01 write_flash --flash_mode dio --flash_size 4MB 0x0 /path_to_file/esphome-s2-mini-test-factory.bin
]]>
618
Low Voltage DC lighting for your home https://khaz.me/low-voltage-dc-lighting-for-your-home/ Mon, 18 Jul 2022 20:29:50 +0000 https://khaz.me/?p=571

Typical lighting circuits for homes in the United States are 120V AC. While this standard makes sense from a historical perspective, it’s maybe not the best idea going forward. Here I’ll outline my thoughts on why you might want to consider using Low Voltage Direct Current LV DC lighting.

Back story

Prior to the proliferation of LEDs, lighting circuits used a lot of power. It was common to have high wattage circuits like 6-10x 100W can lights or a chandelier with 20x 60w candelabra bulbs. These incandescent bulbs would easily be supported by a 120V AC power over the 14 gauge wiring in the home as required by typical 15A lighting circuits.

So?

With the introduction and standardization of LEDs, the lighting loads have dropped significantly. A decrease of 90% is relatively common for a similar light output. My outdoor lights used to be 240W, and with LEDs that dropped to 12W, a reduction of 95%.

So…?

Given these much lower loads, here are some reasons LV DC lighting for your home

  • DIY
    • LV DC can be done with voltages below 50V which has a lot fewer requirements from a National Electrical Code perspective Consult your local AHJ
  • Reduced Cost
    • By doing this yourself, you can eliminate most of the cost
  • Reduced Wiring
    • Romex is expensive and also vastly oversized for the current needs of LEDs. With reduced power requirements, you can power your lights with CAT5/6 cables.
  • Increased Efficiency
    • Since LEDs operate on direct current, you will eliminate AC/DC converters at every bulb
  • Improved Control
    • DC lights can be controlled with basic PWM dimming circuits
      • These are easy to design, build, and integrate with home automation (ESPHome and Home Assistant)
  • A good option for retrofit
    • These reduced requirements make LVDC a great option for retrofitting/adding lights to an existing home with more ease
  • Customizability
    • If you are designing your PWM circuits, it’s very easy to add additional functionality like sensors or other output devices

What now?

In future posts, I will go through my process for designing, building and installing LV DC lighting and controlling them through Home Assistant

]]>
571
Updating ESPHome NEC Remote Transmitter hex for v2021.12 https://khaz.me/updating-esphome-nec-remote-transmitter-hex-for-v2021-12/ Wed, 09 Feb 2022 17:05:38 +0000 https://khaz.me/?p=539

Get your IR devices working again

After I updated to a more recent version of ESPHome and flashed my fan controller, it stopped being able to control the fan. After a bit of troubleshooting and searching, I found the culprit.

Note

In version 2021.12, the order of transferring bits was corrected from MSB to LSB in accordance with the NEC standard. Therefore, if the the configuration file has come from an earlier version of ESPhome, it is necessary to reverse the order of the address and command bits when moving to 2021.12 or above. For example, address: 0x84ED, command: 0x13EC becomes 0xB721 and 0x37C8 respectively.

Remote transmitter docs

Clearly, I don’t read all of the release notes

So after reading this, I was confused. And if you’re here, you are probably confused. So here’s what I did to solve it. In my case, I used the python3 command line inside of my ESPHome docker container, but this should work anywhere you have python3 installed.

Steps for updaing the address and command values

  1. CLI into container
  2. Type python3
  3. Type bin(<value>) and replace value with the hex or decimal value.
  4. Reverse this value right to left: abc becomes cba, the 0b stays at the front.
  5. Count the number of digits, if it’s less than 16 digits, add zero’s at the end to make it 16 digits
  6. Type hex(0b<value>) and replace value with the reversed binary.
  7. Resulting hex should work as your command.

Example

Below are the steps I used to convert my Fan Oscillation values:

- platform: template
  name: Fan Oscillation
  turn_on_action:
    remote_transmitter.transmit_nec:
      address: 255
      command: 25245
root@cbd65cf7857e:/config# python3
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> bin(255)
'0b11111111'
>>> # 11111111 reversed is 11111111
>>> # 11111111 is 8 digits, so add eight zeros to the end = 1111111100000000
>>> hex(0b1111111100000000)
'0xff00'
>>> 
>>> 
>>> 
>>> 
>>> bin(25245)
'0b110001010011101'
>>> # 110001010011101 reversed is 101110010100011
>>> # 101110010100011 is 15 digits, so add one zero to the end = 1011100101000110
>>> hex(0b1011100101000110)
'0xb946'

So the new command is:

- platform: template
  name: Fan Oscillation
  turn_on_action:
    remote_transmitter.transmit_nec:
      address: 0xFF00
      command: 0xB946
]]>
539
Low Voltage DC Lighting with ESPHome https://khaz.me/low-voltage-dc-lighting-with-esphome/ Sat, 27 Mar 2021 17:07:36 +0000 https://khaz.me/?p=433 About a year ago I started a project to figure out if I can add lighting to my hundred-year-old house without too much work. Famous last words. Looking back at it, hiring an electrician would have been faster and maybe cheaper, but I wouldn’t have learned anything. This is a summary of the journey I took through buying parts, programming ESPs, and ultimately, making my first PCB.

Humble benginnings

Initially, I wanted to see how I could scale the ability of controlling LED lights. Analog LED strip lights were about 7 bucks for 15 ft from China and recessed lights were about the same. More importantly, being that these lights are LEDs, they very efficient especially when driven and 60%-80% of their max rating. So I went online and ordered a dozen LED recessed lights, LED strips, and some COB LEDs which are inside of the recessed lights.

My plan was to hook the LEDs up to 24vDC and see how they worked. For the LED strips, it was easy as you can order them in 24v flavor. But the recessed lights are all powered by little LED drivers that are 110/220 to 24v. I first started by burning up a few of the COB LEDs to see what they could take. It turns out the larger COBs did fine with 24v but the smaller ones burned up. Driving two of the smaller ones in series worked fine.

Now I needed to control them

At this point, I had used and flashed a few H801‘s to control analog RGBW strips. While the H801 is nice little package, it had a habit of ringing at certain PWM frequencies. Combine that with the fact that it was using an ESP8266 software PWM to drive the FETs left room for improvement.

Since this was my first foray into controlling analog LEDs, I wanted to see what I could achieve from off-the-shelf components. The key was to drive the LED with a good PWM driver at a high enough frequency that made them work well. I already had a few dozen Wemos D1 Mini, but decided to try a servo controller for the PWM generator and tie it to a MOSFET. Here is the hardware I used:

To my surprise, this setup worked flawlessly on the workbench. For a few months, I used this in my garage to see if it was going to be short-lived, but it just kept working. Initially, I powered these via a bench power supply and later moved the setup over to the DIN Rail PS above. Keep in mind, your power supply should be sized for the load you plan on powering with some headroom.

Circuit Operation

Pulse Width Modulation allows you to control the brightness of the LEDs. The way that you control an LED’s brightness is by turning it ON and OFF, many times per second (this is called PWM frequency). The longer that the LED stays ON, compared to OFF, the brighter the LED will light (this is called PWM duty cycle). In this circuit, the PCA9685 is the PWM generator. For example, the Wemos D1 controller will tell the PCA9685 to turn on channel 1 at 50% duty cycle. The PCA will then pulse that channel at half ON and half OFF based on the frequency you’ve configured for the module. This pulse is an output from the PCA and is wired up to the gate pin of an N channel MOSFET. This device will then act as the switch that turns the LED on and off.

Wiring

The key thing to understand about the wiring is that the Wemos D1 mini and PCA9685 are not 24v tolerant. If you put 24v on them, you will release the magic blue smoke. So, just make sure before you turn it on, you double-check your wiring that you’re only run 5v to these devices.

Components and their wiring

One PCA9685 has 16 channels and each MOSFET module has 4 channels. This means you can go up to 4x MOSFET modules for one PCA9685. I have some channels that are running 4 and 6 recessed LEDs, and many that are running a single recessed LED. This gives you a lot of flexibility in how you wire your lights giving you much more control of your lights.

LED Wiring

The wire types that you use are up to you. It’s important to select the correct wire for the LEDs based on the current draw and the distance you’re running. My longest wire are about 40 feet and there I used bell wire. For shorter 20ft runs with only one light I used cheap CAT6. The wires between the Wemos, PCA, and MOSFET were either dupont or servo extension.

Firmware

As you probably would have guessed, I used the trusty ESPHome project to create the firmware for the Wemos D1. This is pretty straightforward to configure. as you configure the PCA9685 as outputs per the documentation, and then just tie your lights to those outputs to lights.

substitutions:
  platform: ESP8266
  board: d1_mini
  device_name: esphome_lvrc
  friendly_name: "LVRC"
  reboot_timeout_wifi: 900s
  reboot_timeout_api: 1800s
  output_power: 17dB
  gamma_correction: '1.6'
  led_max_power: '0.85'

esphome:
  platform: ${platform}
  board: ${board}
  name: ${device_name}
  build_path: ./build/${device_name}

wifi:
  ssid: !secret wifi_iot_ssid
  password: !secret wifi_iot_pwd
  fast_connect: on
  reboot_timeout: ${reboot_timeout_wifi}
  output_power: ${output_power}
  ap:
    ssid: "AP_${friendly_name}"
    password: !secret wifi_backup_pwd

api:
  reboot_timeout: ${reboot_timeout_api}

ota:

web_server:
  port: 80

logger:

captive_portal:

switch:
- platform: restart
  name: "${friendly_name} REBOOT"

text_sensor:
- platform: version
  name: "${friendly_name} ESPHome Version"  

i2c:
- id: bus_a
  scl: D1
  sda: D2
  frequency: 100kHz
  scan: True

pca9685:
  i2c_id: bus_a
  address: 0x40
  frequency: 1000
output:
  - platform: pca9685
    id: 'pca9685_output0'
    channel: 0
    inverted: False
    max_power: ${led_max_power}
  - platform: pca9685
    id: 'pca9685_output1'
    channel: 1
    inverted: False
    max_power: ${led_max_power}
  - platform: pca9685
    id: 'pca9685_output2'
    channel: 2
    inverted: False
    max_power: ${led_max_power}
  - platform: pca9685
    id: 'pca9685_output3'
    channel: 3
    inverted: False
    max_power: 0.70
  - platform: pca9685
    id: 'pca9685_output4'
    channel: 4
    inverted: False
    max_power: 0.70
light:
  - platform: monochromatic
    output: pca9685_output0
    name: "Entry Outdoor"
    default_transition_length: 1s
    restore_mode: ALWAYS_OFF
    gamma_correct: ${gamma_correction}
  - platform: monochromatic
    output: pca9685_output1
    name: "Entry Indoor"
    default_transition_length: 1s
    restore_mode: ALWAYS_OFF
    gamma_correct: ${gamma_correction}
  - platform: monochromatic
    output: pca9685_output2
    name: "Foyer Hall"
    default_transition_length: 1s
    restore_mode: ALWAYS_OFF
    gamma_correct: ${gamma_correction}
  - platform: monochromatic
    output: pca9685_output3
    name: "Stairs"
    default_transition_length: 1s
    restore_mode: ALWAYS_OFF
    gamma_correct: ${gamma_correction}
  - platform: monochromatic
    output: pca9685_output4
    name: "Dining Outer"
    default_transition_length: 1s
    restore_mode: RESTORE_DEFAULT_OFF
    gamma_correct: ${gamma_correction}

The components above include

  • An I2C component to enable I2C communication
  • A PCA9685 component to define the PWM module
  • Outputs for each of the channels used on the PCA9685
  • Lights for each of the Outputs
  • A switch restart component to reboot the Wemos
]]>
433
Pulsing an LED with ESPHome https://khaz.me/pulsing-an-led-with-esphome/ Tue, 22 Dec 2020 20:24:51 +0000 https://khaz.me/?p=406 I might be dating myself, but I loved the way old Mac’s in my high school’s computer lab would pulse the LED when the unit was in standby mode. While I love how easy ESPHome makes it possible to make a wild variety of custom applications, smoothly pulsing an LED isn’t one of them.

I did some searching and found a good write up by SparkFun that walked you through a way of achieving it. Basically, you can leverage the sin function’s smooth output and appropriately scale/offset that output to drive your LED output.

Making it happen

The code below is running on my dining room switch as a lighting effect on a single pixel of WS2812. I’ve included some comments to help understand some of the pieces and how you can tailor it for your application.

light:
- platform: fastled_clockless
  chipset: WS2812B
  pin: GPIO2
  num_leds: 1
  rgb_order: GRB
  name: "${friendly_name} LED"
  id: status_led
  effects:
    - lambda:
        name: Pulse RED
        // SPEED
        update_interval: .05s
        lambda: |-
          static float in = 0;
          static float out = 0;
          
          // Scale sin output from -1/1 to 0/1
          out = sin(in) * 0.5 + 0.5;  

          auto call = id(status_led).turn_on();
          call.set_transition_length(100);
          // COLOR + BRIGHTNESS
          call.set_rgb(1.0, 0.0, 0.0);
          call.set_brightness(out);
          // Do not publish state to eliminate flooding of logs
          call.set_publish(false);
          call.set_save(false);
          call.perform();
          
          // RESOLUTION
          in += 0.314;
          if (in > 6.283)
            in = 0;
  • Speed
    • Change the update_interval to .15s to slow down the pulsing loop
  • Color & Brightness
    • Change the 1.0, 0.0, 0.0 to R, G, B color values (0-1.0) to set color and brightness
  • Resolution
    • Change the step size of the pulse loop

The pulsing glory

Here’s what it looks like in action:

]]>
406
How to Easily Configure New Unknown Devices with ESPHome https://khaz.me/how-to-easily-configure-new-unknown-devices-with-esphome/ Sun, 27 Sep 2020 01:00:51 +0000 https://khaz.me/?p=381 Over the years, I’ve configured a few new devices with ESPHome. Lately, I loved being able to use Tuya Convert to flash new devices without ever having to unscrew and screws. While Tuya Convert makes the process of putting the initial firmware very easy, it also means I’m now not taking the devices apart. This means I can’t trace the pins to know which pin is doing what.

For me, most of these devices are ESP8266s in some type of smart home gadget. To identify the pins, I’ve created two firmware configs. The first has all of the pins set as binary inputs and the second has all of the pins set as switches. I’ve provided the code for the firmware and precompiled bin files.

Steps for identifying the pins

  1. Use Tuya Convert to flash device
  2. Flash the device with the binary inputs firmware
  3. Connect to the device on it’s broadcasted SSID, then open the devices webpage
  4. Use the device to activate pins, they’ll be listed in the log output
  5. Flash the device with the switches firmware
  6. Connect to the device webpage and activate one switch at a time
  7. Create your ESPHome config and finally flash your device through the web GUI

The code below is used for both versions of the firmware, I just comment out the conflicting parts. For those that want it the easy way, here are the precompiled versions:

substitutions:
  platform: ESP8266
  board: esp01_1m
  device_name: esphome_new_device_assistant
  friendly_name: "ESPHome New Device Assistant"
  reboot_timeout_wifi: 90s
  reboot_timeout_api: 1800s
  output_power: 17dB

esphome:
  platform: ${platform}
  board: ${board}
  name: ${device_name}
  esp8266_restore_from_flash: true

wifi:
  ssid: esphomeesphome
  password: esphomeesphome
  fast_connect: on
  reboot_timeout: ${reboot_timeout_wifi}
  output_power: ${output_power}
  ap:
    ssid: "ESPHome New Device Assistant"
    password: "esphomeesphome"

api:
  reboot_timeout: ${reboot_timeout_api}

ota:

web_server:
  port: 80

logger:

captive_portal:


#################################

switch:
# - platform: gpio
#   name: "${friendly_name} GPIO0"
#   pin:
#     number: GPIO0
#     mode: OUTPUT
#     inverted: True
# - platform: gpio
#   name: "${friendly_name} GPIO1"
#   pin:
#     number: GPIO1
#     mode: OUTPUT
#     inverted: True
# - platform: gpio
#   name: "${friendly_name} GPIO2"
#   pin:
#     number: GPIO2
#     mode: OUTPUT
#     inverted: True
# - platform: gpio
#   name: "${friendly_name} GPIO3"
#   pin:
#     number: GPIO3
#     mode: OUTPUT
#     inverted: True
# - platform: gpio
#   name: "${friendly_name} GPIO4"
#   pin:
#     number: GPIO4
#     mode: OUTPUT
#     inverted: True
# - platform: gpio
#   name: "${friendly_name} GPIO5"
#   pin:
#     number: GPIO5
#     mode: OUTPUT
#     inverted: True
# - platform: gpio
#   name: "${friendly_name} GPIO9 - DO NOT USE!!"
#   pin:
#     number: GPIO9
#     mode: OUTPUT
#     inverted: True
# - platform: gpio
#   name: "${friendly_name} GPIO10 - DO NOT USE!!"
#   pin:
#     number: GPIO10
#     mode: OUTPUT
#     inverted: True
# - platform: gpio
#   name: "${friendly_name} GPIO12"
#   pin:
#     number: GPIO12
#     mode: OUTPUT
#     inverted: True
# - platform: gpio
#   name: "${friendly_name} GPIO13"
#   pin:
#     number: GPIO13
#     mode: OUTPUT
#     inverted: True
# - platform: gpio
#   name: "${friendly_name} GPIO14"
#   pin:
#     number: GPIO14
#     mode: OUTPUT
#     inverted: True
# - platform: gpio
#   name: "${friendly_name} GPIO15"
#   pin:
#     number: GPIO15
#     mode: OUTPUT
#     inverted: True
# - platform: gpio
#   name: "${friendly_name} GPIO16"
#   pin:
#     number: GPIO16
#     mode: OUTPUT
#     inverted: True

## REBOOT SWITCH
- platform: restart
  name: "${friendly_name} REBOOT"


binary_sensor:
- platform: gpio
  name: "${friendly_name} GPIO0"
  pin:
    number: GPIO0
    mode: INPUT_PULLUP
    inverted: True
- platform: gpio
  name: "${friendly_name} GPIO1"
  pin:
    number: GPIO1
    mode: INPUT_PULLUP
    inverted: True
- platform: gpio
  name: "${friendly_name} GPIO2"
  pin:
    number: GPIO2
    mode: INPUT_PULLUP
    inverted: True
- platform: gpio
  name: "${friendly_name} GPIO3"
  pin:
    number: GPIO3
    mode: INPUT_PULLUP
    inverted: True
- platform: gpio
  name: "${friendly_name} GPIO4"
  pin:
    number: GPIO4
    mode: INPUT_PULLUP
    inverted: True
- platform: gpio
  name: "${friendly_name} GPIO5"
  pin:
    number: GPIO5
    mode: INPUT_PULLUP
    inverted: True
- platform: gpio
  name: "${friendly_name} GPIO9 - DO NOT USE!!"
  pin:
    number: GPIO9
    mode: INPUT_PULLUP
    inverted: True
- platform: gpio
  name: "${friendly_name} GPIO10 - DO NOT USE!!"
  pin:
    number: GPIO10
    mode: INPUT_PULLUP
    inverted: True
- platform: gpio
  name: "${friendly_name} GPIO12"
  pin:
    number: GPIO12
    mode: INPUT_PULLUP
    inverted: True
- platform: gpio
  name: "${friendly_name} GPIO13"
  pin:
    number: GPIO13
    mode: INPUT_PULLUP
    inverted: True
- platform: gpio
  name: "${friendly_name} GPIO14"
  pin:
    number: GPIO14
    mode: INPUT_PULLUP
    inverted: True
- platform: gpio
  name: "${friendly_name} GPIO15"
  pin:
    number: GPIO15
    mode: INPUT_PULLUP
    inverted: True
- platform: gpio
  name: "${friendly_name} GPIO16"
  pin:
    number: GPIO16
    mode: INPUT_PULLUP
    inverted: True

]]>
381
Home Theater: Automating my subwoofer with Home Assistant and Node-RED https://khaz.me/home-theater-automating-my-subwoofer-with-home-assistant-and-node-red/ Sat, 11 Jan 2020 20:14:13 +0000 https://khaz.me/?p=283 A while back, we decided to rearrange our living room, get new furniture, etc.  I took this opportunity to expand my home automation addiction and pull more items into Home Assistant.  My plan included running wiring through the ceiling for power and audio, automating my projector, screen, and subwoofer.   This how-to will outline how I’m controlling the power of my remotely mounted subwoofer all from a one-word activation with my Google Home, but more on that in another post. This is the first of multiple posts for automating my home theater experience.

Hardware

While my subwoofer’s power switch has an auto setting that is only supposed to turn on the amp when it’s getting signal from the receiver, this never really worked.  And as we rearranged the room, it’s new location did not allow it to be wired into the receiver’s builtin switched receptacle.  Thus, I needed a way to remotely control the sub being on or off.

Original Subwoofer

The Sonoff Basic is perfect for this job as you are switching 120v for the subwoofer’s power.  It does not matter which version of the Sonoff Basic you use (Original, R2, or R3) as they will perform the same here.  I have a half dozen R2s that came as part of a bulk order, so that’s what I used. Splicing the Sonoff into the subwoofer’s power cable was very straight forward.  All you have to do is cut the cable and insert the Sonoff in the middle, with the plug side of the cable wired to the input side of the Sonoff and the subwoofer side to the output side.

My installation of subwoofer with Sonoff spliced into the power cable

I chose to take a step further by disassembling my subwoofer and installing the Sonoff inside of the subwoofer.  This means it’s spliced on the power cable after the cable enters the subwoofer.  I used a couple of strong zip ties to mount the Sonoff, in its enclosure, to one of the sub’s metal components.

Sonoff mounted to the subwoofer amplifier

Firmware

The overall process is to flash this unit with custom firmware that tightly integrated to Home Assistant.  To get the unit converted from the original firmware, I used a USB FTDI converter combined with ESPHome to get this accomplished.  If you are not familiar with this process, JuanMTech and many others have great detailed steps for this part. Head over to your ESPHome dashboard and create a new device.  Paste the YAML below into the device configuration:

substitutions:
  platform: ESP8266
  board: esp01_1m
  device_name: esphome_subwoofer
  friendly_name: "Subwoofer"
  reboot_timeout_wifi: 900s
  reboot_timeout_api: 1800s
  output_power: 17dB

# <<: !include z_common.yaml

esphome:
  platform: ${platform}
  board: ${board}
  name: ${device_name}
  esp8266_restore_from_flash: true

wifi:
  ssid: !secret wifi_iot_ssid
  password: !secret wifi_iot_pwd
  fast_connect: on
  reboot_timeout: ${reboot_timeout_wifi}
  output_power: ${output_power}
  ap:
    ssid: "AP_${friendly_name}"
    password: !secret wifi_backup_pwd

api:
  reboot_timeout: ${reboot_timeout_api}

ota:

web_server:
  port: 80

logger:

captive_portal:

binary_sensor:
- platform: gpio
  pin:
    number: GPIO0
    mode: INPUT_PULLUP
    inverted: True
  id: sonoff_button
  internal: true
  on_press:
    - switch.toggle: sonoff_relay

switch:
- platform: restart
  name: '${friendly_name} REBOOT'

- platform: gpio
  name: "${friendly_name} Power"
  icon: "mdi:speaker"
  id: sonoff_relay
  pin:
    number: GPIO12
  restore_mode: ALWAYS_ON

status_led:
  pin:
    number: GPIO13
    inverted: True

text_sensor:  
  - platform: version
    name: ${friendly_name} ESPhome Version

Add the device to Home Assistant

Now, go to the Home Assistant Integrations page (Configuration>Integrations). You should see the ESPHome device show up in the discovered section after a minute or two.  Click configure, and submit, and that’s it.  Alternatively, you can manually add the device by clicking “CONFIGURE” on the ESPHome integration and entering “<NODE_NAME>.local” as the host.

Once the device is added, it will be in the lower part of this page (under Configured).  Clicking through to the device will show you the entity names that are auto-magically created by the ESPHome integration.

Node-RED Automation

Now, you cannot be expected to have to switch on the subwoofer through Home Assistant like a barbarian!  So my fix for this is to automate it with the state of my receiver’s power.  This is a very easy automation to create in Node-RED, my automation engine for Home Assistant.

This automation is looking for any messages about the on/off state of my receiver (media_player.rx_v675) and then outputs that state through an on/off switch (splitter).  If the receiver state becomes on, the flow calls to turn on the subwoofer switch to on. You can copy and paste the JSON flow below to import it into your Node-RED instance.

[{"id":"cf3c6f80.3beae","type":"server-state-changed","z":"57657673.d86dd8","name":"Receiver state","server":"c75798f0.9eb148","version":1,"entityidfilter":"media_player.rx_v675","entityidfiltertype":"exact","outputinitially":false,"state_type":"habool","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":120,"y":890,"wires":[["67b8934a.8ffaec"]]},{"id":"67b8934a.8ffaec","type":"switch","z":"57657673.d86dd8","name":"on / off","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":350,"y":890,"wires":[["2e23fe27.8fa362"],["6b0f2645.bdd0a8"]]},{"id":"2e23fe27.8fa362","type":"api-call-service","z":"57657673.d86dd8","name":"Turn on sub","server":"c75798f0.9eb148","version":1,"service_domain":"homeassistant","service":"turn_on","entityId":"switch.subwoofer_power","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":580,"y":860,"wires":[[]]},{"id":"6b0f2645.bdd0a8","type":"api-call-service","z":"57657673.d86dd8","name":"Turn off sub","server":"c75798f0.9eb148","version":"1","service_domain":"homeassistant","service":"turn_off","entityId":"switch.subwoofer_power","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":580,"y":920,"wires":[[]]},{"id":"c75798f0.9eb148","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

Concerns

I had some initial concerns that the Sonoff would be subjected to stress/interference that could make it work incorrectly or breakdown faster.  However, after making this modification last May, I’ve had zero issues with this setup.  It just works. If you’ve got any questions, feel free to ping me below.

]]>
283
Home Assistant integrated WiFi Power Strip for your HomeLAB https://khaz.me/home-assistant-integrated-wifi-power-strip-for-your-homelab/ Fri, 25 Oct 2019 06:43:23 +0000 https://khaz.me/?p=252 One of the things the lazy side of me hates, is physically having to go to my HomeLAB to hard reset any device.  While this has been happening less and less, it’s still something I dread.  My solution to the problem is to make a poor man’s rack PDU that’s integrated into Home Assistant. This how-to will outline how you can do the same for less than $7 per controlled socket using ESPHome firmware.

Hardware

I have used a Teckin and Tunbox (white or black), but there are many manufacturers that seem to brand label the same physical product.  The key is that the description needs to mention the “Smart Life” app as that means it’s using the Tuya WiFi module.

Teckin WiFi Power Strip

Firmware

The overall process is to flash this unit with custom firmware that tightly integrated to Home Assistant.  To get the unit converted from the original firmware, I used Tuya Convert combined with ESPHome to get this accomplished.  If you are not familiar with these, digiblurDIY and DrZzs both have extensive videos on how to get started with these.  I will cover the detailed configuration for these power strips.

The configuration I’ve set up for this device is focused on keeping the power on, as typically you do not want to shut it off accidentally. So, the physical button on the unit only toggles the USB power and all power sockets default to being ON when it’s plugged in or power is restored.  The only way to control the power sockets is through the device’s webpage or Home Assistant.

Follow the guides linked above to flash your power strip via the tuya convert method.  Then create a new device in ESPHome and load the YAML below to this device.

substitutions:
  board: esp01_1m
  device_name: esphome_homelab_powerstrip
  friendly_name: Homelab Powerstrip 1

esphome:
  name: ${device_name}
  platform: ESP8266
  board: ${board}

wifi:
  ssid: !secret wifi_iot_ssid
  password: !secret wifi_iot_pwd
  fast_connect: on
  ap:
    ssid: !secret wifi_backup_ssid
    password: !secret wifi_backup_pwd
  reboot_timeout: 0s

api:

ota:

web_server:
  port: 80

logger:

switch:
- platform: gpio
  name: "${friendly_name} Socket 1"
  icon: mdi:power-socket-us
  id: relay1
  pin:
    number: GPIO4
    inverted: True
  restore_mode: RESTORE_DEFAULT_ON
  on_turn_on:
  - switch.turn_on: red_led
  on_turn_off:
    - if:
        condition:
          and:
            - switch.is_off: relay2
            - switch.is_off: relay3
            - switch.is_off: relay4
        then:
          - switch.turn_off: red_led
- platform: gpio
  name: "${friendly_name} Socket 2"
  icon: mdi:power-socket-us
  id: relay2
  pin:
    number: GPIO13
    inverted: True
  restore_mode: RESTORE_DEFAULT_ON
  on_turn_on:
  - switch.turn_on: red_led
  on_turn_off:
    - if:
        condition:
          and:
            - switch.is_off: relay1
            - switch.is_off: relay3
            - switch.is_off: relay4
        then:
          - switch.turn_off: red_led
- platform: gpio
  name: "${friendly_name} Socket 3"
  icon: mdi:power-socket-us
  id: relay3
  pin:
    number: GPIO12
    inverted: True
  restore_mode: RESTORE_DEFAULT_ON
  on_turn_on:
  - switch.turn_on: red_led
  on_turn_off:
    - if:
        condition:
          and:
            - switch.is_off: relay1
            - switch.is_off: relay2
            - switch.is_off: relay4
        then:
          - switch.turn_off: red_led
- platform: gpio
  name: "${friendly_name} Socket 4"
  icon: mdi:power-socket-us
  id: relay4
  pin:
    number: GPIO14
    inverted: True
  restore_mode: RESTORE_DEFAULT_ON
  on_turn_on:
  - switch.turn_on: red_led
  on_turn_off:
    - if:
        condition:
          and:
            - switch.is_off: relay1
            - switch.is_off: relay2
            - switch.is_off: relay3
        then:
          - switch.turn_off: red_led
- platform: gpio
  name: "${friendly_name} USB Ports"
  icon: mdi:usb
  id: relay5
  pin:
    number: GPIO16
    inverted: False
  restore_mode: RESTORE_DEFAULT_ON

- platform: gpio
  internal: true
  id: red_led
  pin:
    number: GPIO3
    inverted: True

- platform: restart
  name: '${friendly_name} REBOOT'

binary_sensor:
- platform: gpio
  pin:
    number: GPIO5
    mode: INPUT_PULLUP
    inverted: False
  internal: true
  on_press:
    - switch.toggle: relay5

status_led:
  id: blue_led
  pin:
    number: GPIO0
    inverted: True

text_sensor:  
  - platform: version
    name: ${friendly_name} ESPhome Version

Home Assistant UI (Lovelace)

At first, I did not want to display these in my lovelace UI because I was worried about fat fingering something by accident.  Then, I discovered the Toggle Lock Entity Row plugin developed by Thomas Loven.  The easiest method for installing this is to utilize HACS (Home Assistant Community Store) – if you don’t have it, get it…now.  This plugin prevents accidental switching by requiring a secondary click within 5 seconds in order to turn off the device.

In my instance, I have chosen to name the entities generically via the firmware and add the label in lovelace.

- type: entities
        show_header_toggle: false
        title: HomeLAB Powerstrip 1
        entities:
          - entity: switch.homelab_powerstrip_1_socket_1
            name: Modem
            type: custom:toggle-lock-entity-row
          - entity: switch.homelab_powerstrip_1_socket_2
            name: UniFi USG
            type: custom:toggle-lock-entity-row
          - entity: switch.homelab_powerstrip_1_socket_3
            name: UniFi Switch
            type: custom:toggle-lock-entity-row
          - entity: switch.homelab_powerstrip_1_socket_4
            name: Dell R210
            type: custom:toggle-lock-entity-row
          - entity: switch.homelab_powerstrip_1_usb_ports
            name: UniFi Console Pi
            type: custom:toggle-lock-entity-row

 

Concerns

One of the concerns I initially had with this setup was the relays.  Since these are going to be on 99.9% of their life, the coil will be energized all of that time.  I couldn’t find a coil life listed anywhere from the manufacturer of the Y3F-SS-105DM.  After some googling, it appears for DC coils, this is a somewhat negligible concern as they will run very cool.  My power strip has been on since April 2018 without issues.

 

If you’ve got any questions, feel free to ping me below.

]]>
252
Cheap and Easy Control of 8 Relays through Home Assistant https://khaz.me/cheap-and-easy-control-of-8-relays-through-home-assistant/ https://khaz.me/cheap-and-easy-control-of-8-relays-through-home-assistant/#comments Sat, 06 Jul 2019 16:27:06 +0000 https://khaz.me/?p=238 Home Assistant and ESPHome

Following up on my previous post of the Cheap and Easy series, I’m going to show you how you can control 8 relay channels for about 25 bucks and in about 30 minutes. If you’re not familiar with Home Assistant or ESPHome, check out my previous post.

In this project, I use a Wemos D1 Mini as the wifi enable the controller to interface with the relay module using the IO expansion shield.  The magic of this setup is using the IO module and soldering it directly onto the relay board.  This eliminates the narly mess you see you when would typically have 8 channels of relays.  As a bonus, since you are only using 8 of the available 16 channels on the IO module, you could use the remaining 8 IO to do all sorts of other things.

Shopping list:

  1. Wemos D1 Mini – The Wemos D1 Mini is an ESP8266 based microcontroller that will handle the heavy lifting of WiFi and communicating to your Home Assistant instance [ Amazon, 3 Pack, Amazon, 5 Pack ]
  2. MCP23017 IO Expansion Module – This is what you will use to allow you to drive all 8 relay channels with minimal effort [ Amazon ]
  3. 8 Channel Relay Board – I added the DHT to get temperature and humidity data [ Amazon ]
  4. Assorted Dupont Jumpers – Always have a bunch of these handy, they have a tendency to disappear and get used up in projects. [ Amazon ]OPTIONAL: If you want to control 12v or 24v loads
  5. Appropriate DC power supply for your loads – This will vary on what you are trying to control, links to some options. [ Amazon 12v 5A | Amazon 24v 3A ]
  6. LM2596S DC-DC power regulator – It converts garage door’s 12V DC to 5V DC for the Wemos or use a microUSB cable. [ Amazon ]
  7. Soldering Iron Kit – It’s likely that you already have this if you’ve been DIYing for a while [ Amazon ]

Installation is cake

You will need to provide 5v DC power to power the system.  To get started, you will want to place the IO module over the pins of the relay module.  Make sure to line these up correctly as I’ve indicated below.  If you don’t, it’s a big pain to remove it and put it back on correctly, especially if you’ve soldered it in place.  Ask me how I know.
The IO module will have a total of 4 wires connected to it:  two for +5v and GND from the power supply, and two for the I2C (SDA and SCL) from the Wemos.
The IO module will have a total of 4 wires connected to it:  two for +5v and GND from the power supply, and two for the I2C (SDA and SCL) from the Wemos.
That’s it!

Wiring for all of the devices

Pay careful attention to the alignment of the pins

Soldered IO module on to the relay module

Since it’s likely that you will want to switch/power things that are of higher voltage than the 5v rail you need to power the Wemos, I’ve included the 12v and 24v optional power supplies.  Be sure that you do not connect any of these higher voltages to any of the logic pins on the Wemos or the IO, only connect them to the relay output terminals.

ESPHome config

I created the 8 channels as output switches and for the time being omitted the remaining 8 channels which can be configured as inputs or outputs.  I created a new device, named it esphome_relay_board using a D1 mini board with the following config:

esphome:
  name: esphome_relay_board
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: 'insert'
  password: 'your_own'
  fast_connect: on
  
api:

ota:

web_server:
  port: 80

logger:

i2c:
  sda: D1
  scl: D2
  scan: True

mcp23017:
  - id: 'mcp23017_hub'
    address: 0x20

switch:
- platform: restart
  name: "Relay Channel Board REBOOT"

- platform: gpio
  name: relay_channel_1
  icon: mdi:electric-switch
  pin:
    mcp23xxx: mcp23017_hub
    number: 15
    mode: OUTPUT
    inverted: True
- platform: gpio
  name: relay_channel_2
  icon: mdi:electric-switch
  pin:
    mcp23xxx: mcp23017_hub
    number: 14
    mode: OUTPUT
    inverted: True
- platform: gpio
  name: relay_channel_3
  icon: mdi:electric-switch
  pin:
    mcp23xxx: mcp23017_hub
    number: 13
    mode: OUTPUT
    inverted: True
- platform: gpio
  name: relay_channel_4
  icon: mdi:electric-switch
  pin:
    mcp23xxx: mcp23017_hub
    number: 12
    mode: OUTPUT
    inverted: True
- platform: gpio
  name: relay_channel_5
  icon: mdi:electric-switch
  pin:
    mcp23xxx: mcp23017_hub
    number: 11
    mode: OUTPUT
    inverted: True
- platform: gpio
  name: relay_channel_6
  icon: mdi:electric-switch
  pin:
    mcp23xxx: mcp23017_hub
    number: 10
    mode: OUTPUT
    inverted: True
- platform: gpio
  name: relay_channel_7
  icon: mdi:electric-switch
  pin:
    mcp23xxx: mcp23017_hub
    number: 9
    mode: OUTPUT
    inverted: True
- platform: gpio
  name: relay_channel_8
  icon: mdi:electric-switch
  pin:
    mcp23xxx: mcp23017_hub
    number: 8
    mode: OUTPUT
    inverted: True

status_led:
  id: status_led
  pin:
    number: GPIO2

The components above include

  • An I2C component to enable I2C communication
  • A MCP23017 component for the IO module
  • A switch restart component to reboot the Wemos
  • A switch GPIO component for the relay
  • A status LED component for indicating networking/error status.

Now can compile the firmware and flash it to your Wemos D1.  Again, if you’re not familiar with how to do this, reference the videos linked above.  Once it’s flashed, power cycle the device.  Now you should be able to open the web GUI for the device by either going to it’s IP or going to http://esphome_relay_board.local in your browser, or whatever you named it.  When the page loads up, you should test out the relay switches by hitting the button next to them.

Integration into Home Assistant

Now to integrate it into Home Assistant, you will need to go to configuration > integrations.  Assuming you have discovery enabled, you will have a new ESPHome device, click configure and add it to your system.  If you don’t have any existing conflicting entities, your entity names should match the ones I have.  I’m using the following config for lovelace:

- type: entities
        title: Garage Relay Module + IO
        show_header_toggle: false
        entities:
          - switch.relay_channel_1
          - switch.relay_channel_2
          - switch.relay_channel_3
          - switch.relay_channel_4
          - switch.relay_channel_5
          - switch.relay_channel_6
          - switch.relay_channel_7
          - switch.relay_channel_8
          - switch.garage_relay_module_reboot

This provides you with a simple switch list.  Each icon shows you the state of the relay.  And lastly, you get the reset switch to reset the ESP.

]]>
https://khaz.me/cheap-and-easy-control-of-8-relays-through-home-assistant/feed/ 41 238