After switching from Home Assistant's built in automation system, I moved onto Node-Red. Well, I've made the switch again but this time I get to write automations in C# with NetDaemon.

While I love the versatility of Home Assistant, one area it fails to meet my needs is automation. It has built in editors for building automations, but in my opinion it's a bit too limiting. Originally I looked to Node-Red. Since it's based in JavaScript I was able to expand past the basic logic blocks that make up Home Assistant automations. Being able to do things like convert colors from hex to RGB or parse external APIs was made much easier.

As awesome as Node-Red is though, I am a C#/.NET developer. Chaining logic blocks together in a flowchart-like interface is neat, but it can be frustrating to debug and architect more complex automations. Managing things like state or scheduled events is really what I felt I was lacking in Node-Red. I'm sure there's a way to do it, but automations are meant to remove frictions from our smart home. Why should writing them be difficult?

Okay, I'm a bit biased. Building automations with Node-Red or Home Assistant has an objectively smaller learning curve than learning a programming language. That being said, I'm a power user. Wouldn't it be great if I could just use C#/.NET to write automations?

Enter NetDaemon

NetDaemon is a wonderful Home Assistant framework built by the community "For the C# developer". By using the Home Assistant websocket API, NetDaemon interfaces directly with all of your entities and services and can even generate C# classes from them. Need to get the state of the entry lights? Easy, it's available at Entities.Lights.Entry.State.

With this framework I was able to easily convert all of my previous Node-Red automations over to C# code. Even more I can set a breakpoint at anywhere in my automation and see every detail about my entity. Additionally, with generated entity and service classes I get to utilize Intellisense which removes a ton (but not all, which I'll highlight) of guesswork about what an entity or service supports.

If you want to read more about NetDaemon, I highly recommend checking out their docs.

Automation Example

I'll cover more automations in follow up posts, but I did want to highlight one that would have been difficult for me to accomplish without NetDaemon.

Sunset Lamp

My partner prefers to unwind before bed with soft lights and warm colors. In the past she's used a Hatch Rest, but I made it a mission of mine to handle this with an RGB bulb and Home Assistant. By using an IKEA TRÅDFRI button and a Philips Hue Go portable light I was able to replicate the base features of the Hatch.

Pressing the button will start the automation:

  1. The bedroom lights are turned off by dimming over a 20 second period
  2. The Hue Go turns on to a color temperature of 2700k with a 30 second transition
  3. A Spotify playlist is played over the bedroom speakers using Spotcast
  4. After 5 minutes the Go will transition to a full red (#FF0000) in a 10 minute transition
  5. The Go will dim to fully off with a 5 minute transition period

During this automation a "Status" entity is updated to the different states of "Daylight", "Sunset", "Twilight", and "Night". If the button is pressed while the automation is running, the next phase will begin immediately.

This automation probably could have been solved through other platforms, but managing the state of the automation and cancelling transitions of the light really got to be a pain. Overall I was able to write this in about 250 lines of C#.

In reality this automation admittedly doesn't provide much more than the Hatch Rest already did. Generally, though, I don't like to rely on products that are closed source, cloud enabled, and have to compromise in order to make an all-in-one unit. Everything in this C# automation runs locally and let's me utilize an objectively better light and sound system. (The little speaker on the Hatch probably kept me awake as I was bothered by its poor quality)

All of this is ignoring probably the best part: now that this automation is written I can use it with almost any combination of RGB light and media player. In fact I pretty much gutted the light logic and put it into a reading lamp we have in the living room. If you take the sunset and flip it around, you can also have yourself a sunrise alarm!

Final Thoughts

Overall NetDaemon has empowered me to be productive with my automations. I tend to drop projects pretty easily if the learning curve is too steep and I love how it just allows me to write code in a language I know.

The framework itself can be a little finicky to work with, especially with some of the generated code. However, I've been able to fix a lot of shortcomings by establishing my own extensions of the framework. Notifications are a great example. All the generated code does is expose the notify service and the Home Assistant Mobile entities. Actually having to send notifications and handle the clearing of notifications or when someone presses a notification's buttons can be frustrating.

To curb this a bit [I wrote a class] to clean up some of the dirtier sections of my code. Clearing then sending a notification is now as simple as:

Notifications.ClearPushNotification("DryerComplete");

Notifications.SendPushNotification(new PushNotification()
{
    Title = "Dryer Done!",
    Message = "The dryer has finished! Time for some warm undies!",
    Tag = "DryerComplete",
    Data = new PushNotificationData()
    {
        TTL = 0,
        Priority = PushNotificationPriority.High
    }
});

Despite the areas where NetDaemon is a bit more... verbose... I would definitely recommend it to anyone who's looking to go just a bit further with Home Assistant. All of my automations are available on my GitHub if you need some inspiration. Now if you'll excuse me I have to find more parts of my home to unnecessarily add to my network!

Previous Post Next Post