...

I turned my smart light into a Morse Code communicator with Home Assistant… because why not?


Ah, Home Assistant, a wonderful piece of software that has enabled me to automate all kinds of things in my home thanks to the countless integrations available for it. It can simply do it all, from the weird and wonderful, like detecting an electric toothbrush to kickstart my day, to the entire automation of my lighting all around me. Oh, and now I’m using it to turn my lights into Morse Code communicators. That’s… useful too, I guess?

I say that tongue in cheek, of course. I actually did use Home Assistant to turn my smart light into a Morse Code communicator, but not for any particular reason. Borne out of, well, boredom primarily, the idea came to mind following my configuration of the Tuya Local (not to be confused with LocalTuya) integration, so that I could control my lights on my LAN. Seeing as I could entirely circumvent any kind of rate limiting that might occur, I began to think of silly ways I could abuse my newfound power.

Thus, the idea of a Morse Code communicator came to mind. I had also been looking into the Pyscript integration, which allows you to write Python scripts to control your home, so I figured it was the perfect time to try it out and see what I could achieve. It turns out it works really well, so now I can communicate with my neighbors via the medium of a flashing light in my living room, assuming they happen to be looking at precisely the right moment. Oh, and assuming they know it’s Morse Code and can understand it… Yeah, come to think of it, it might not be particularly useful.

Still, this was a fun project, and maybe you can take some of what I learned here to turn it into something that does have uses. The actual Morse Code communication part will seldom be useful, but some of the concepts certainly will be.

What is Home Assistant’s Pyscript?

It’s a custom integration with lots of features

home-assistant-pyscript-automation

Pyscript, installed via HACS, is a custom integration that can be used to write Python functions and scripts for automation and logic. You can write a service that can be called from any other integration in Home Assistant, so you can create your own custom functionality within Home Assistant without needing to know how to write a complete integration. Functions can sleep or wait for additional changes and events as well, and Pyscript can integrate with Jupyter front-ends like Notebook.

As someone proficient in Python, it seemed like a no-brainer to play around with Pyscript and see what it could do. I’ve already built my own Home Assistant integration for controlling my Govee B615 lights that I reverse engineered, but if Pyscript can make controlling my smart home even easier without the need for an entire integration, it’s well worth the time to get to grips with it. Building an integration can be time-consuming, but a Python script, if you know what you’re doing, can take mere minutes.

Of course, many of the things you might want to do with Pyscript can simply be implemented using the “command_line” integration instead, a legacy integration that allows you to define shell commands for execution based on specific criteria. Not everything can, though, and with Pyscript, you can automate the control of individual devices through code within Home Assistant.

Writing a Home Assistant Pyscript Morse Code light script

It’s really simple

Home Assistant morse code pyscript

Building and deploying this was fairly simple, though I made a misstep along the way that could have made things easier on me if I had avoided it. This was, primarily, my usage of async tasks, where I wrote an async task using await in order to prevent the main event loop of Home Assistant from sleeping. As it turns out, this isn’t necessary when using Pyscript, and usage of async is discouraged when it comes to Home Assistant’s AppDaemon unless you know what you’re doing, as every Pyscript is run asynchronously anyway. While I’m familiar with async, this could have introduced problems that would have been a pain to fix, though it thankfully just works.

In terms of actually writing and deploying the script, aside from my misguided foray into async tasks, it’s ridiculously simple. There’s a character map at the start to map every letter and number to its equivalent Morse combination of dots and dashes, and then defined behavior for how long it should wait for a dot and how long it should wait for a dash. We then define a gap between characters and words, which is multiplied by the length of time of a dot. Because the light turns off at the end of a dot, this doesn’t mean the light stays on for that long; it’s just a way to chain the logic together and make things relative, especially because the latency can be different between devices. This makes it so that the script can work on any light with some changes.

What I love about this is how simple it is to see and control entities in your Home Assistant instance. Simply defining my light as “light.office_light” and referring to this as the entity_id makes it instantly controllable, so that I can use functions like “light.turn_on” and it just… works. Throughout my testing, I utilized Home Assistant’s developer tools, specifically the “Action” tab, so that I could test my script and ensure that features worked as I went. Once a script has been picked up in your /config/pyscript folder and it’s been recognized by Pyscript after restarting Home Assistant, you only need to reload the Pyscript integration itself every time you make a change, so it saves you needing to restart constantly.

home-assistant-morse-code-call-action

I can’t emphasise enough how silly this is as a project for most people, but there are genuine uses for something similar to this for those looking to make their lights a core part of their smart home. You could have your lights flash based on specific criteria, such as when your home lab goes down. If you created an automation using Uptime Kuma that would then call a script to flash a certain number of times, representing which service was down, that would actually be useful. And it’s not just overhead lights that we’re talking about here, it could be any kind of light that you can control.

Plus, in accessibility contexts, those who may have impaired speech might prefer to use a light that can flash once for yes, or two for no. And this kind of automated control extends past lights; Morse Code was just a silly example, but the same concepts applied here to implement this project can be applied across a variety of tools and devices.

If you’ve been thinking about what to play with next, Pyscript is definitely something to consider. It’s in HACS, it’s easy to use, and you can gain a finer amount of control over your smart home through code rather than YAML, especially when doing things like this that aren’t always easily possible through Home Assistant’s automations.



Source link

Previous Article

New Register Booster Fan Fits 6”x12” (Duct Opening Cutout), Smart Vent Booster with Thermostat Control & Remote Control, 10-Speed Control Heating Cooling AC Vent Fan (White)

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Submit Comment

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨
Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.