Skip to content

Instantly share code, notes, and snippets.

@DavidDeSloovere
Last active March 8, 2024 18:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidDeSloovere/44e5e56cb13f4662f18021b32587f374 to your computer and use it in GitHub Desktop.
Save DavidDeSloovere/44e5e56cb13f4662f18021b32587f374 to your computer and use it in GitHub Desktop.
Self executing Snowflakes as a JS module

Home Assistant

  • Create snowflakes.js under config\www folder.
  • Add content from the gist
    • Choose to run always (remove all below line 4),
    • Or by date (remove line 4, optionally editing the dates you want the snowflakes to trigger)
    • and save.
  • In HA, go to Configuration > Lovelace Dashboards > Resources.
  • Click 'Add Resources' and enter local/snowflakes.js (it's a JavaScript module)
  • Save it
  • Enjoy the holidays

Using https://github.com/hcodes/snowflakes

import Snowflakes from 'https://cdn.skypack.dev/magic-snowflakes';
// always run
new Snowflakes();
// OR use date
// month in JS dates are zero-based
const currentMonth = new Date().getMonth() + 1;
const currentDay = new Date().getDate();
// run from december 6th to january 15th
const isOkForDecember = currentMonth === 12 && currentDay >= 6;
const isOkForJanuary = currentMonth === 1 && currentDay <= 15;
if (isOkForDecember || isOkForJanuary ) {
new Snowflakes();
}
@mckochan
Copy link

This worked great Christmas 2020, when trying to load it this year I am getting the following error:

http://192.168.1.36:8123/local/snowflakes.js:3:1 Uncaught TypeError: Class constructor Snowflakes cannot be invoked without 'new'
http://192.168.1.36:8123/local/snowflakes.js:3:1 TypeError: class constructors must be invoked with 'new'

@DavidDeSloovere
Copy link
Author

@mckochan Thanks for reporting this. Hadn't gotten around to set this up for the holidays.
The magic-snowflakes library got an update, and needs to be initialized differently by adding a new keyword. Works on my machine. I have updated the gist.
Happy holidays.

@mckochan
Copy link

Thank you! That's what I thought, friendly reminder to everyone that you need to clear your Cache for the snowflakes to show up!
Happy Holidays

@warmbadger
Copy link

This still works in 2022! Thanks so much, my wife jokingly said my dashboard needed snowflakes at Christmas - jokes on her!

@dnestico
Copy link

I can't believe how easy this was!! 1 question I have though is does this go away after Christmas is over? Or do I just have to manually remove the resource until next Christmas? Thanks!

@mckochan
Copy link

You need to unload it. I usually do so by renaming the file to snowflakes.bak

@dnestico
Copy link

Thanks for the reply and alright will do soon after the snow leaves haha. Would be cool if it was possible to automate this with the weather or seasons integration.

@heisenberg2980
Copy link

heisenberg2980 commented Feb 25, 2024

Also interested in automating this script before next winter, there must be a way, we just need to find it!
Some random ideas: Is there a way to rename files from an automation? what about adding/removing resources from an automation?

EDIT: answering my own question, YES it is possible to automatically rename a file using shell commands: https://www.home-assistant.io/integrations/shell_command/

These two shell commands added to the configuration.yaml do the trick (they can be invoked by an automation triggered by date/weather/season/etc):

shell_command:
  disable_snowflakes: mv /config/www/snowflakes.js /config/www/snowflakes_RemoveToEnable.js
  enable_snowflakes: mv /config/www/snowflakes_RemoveToEnable.js /config/www/snowflakes.js

@dnestico
Copy link

dnestico commented Feb 26, 2024

So here is my disable snow automation and weird, it seems to have worked and changed the file name but for some reason the snow still shows on my dashboard....

**EDIT: I think clearing the cache fixed it lol, but only on the HA app, still won't go away on the web but I assume its a different caching issue there.

alias: Turn off dashboard snow when not snowing
description: ""
trigger:
  - platform: template
    value_template: "{% if not is_state('weather.forecast_home', 'snowy') %}true{% endif %}"
condition: []
action:
  - service: shell_command.disable_snowflakes
    data: {}
mode: single

@heisenberg2980
Copy link

Yes that is because of the cache, search how disable the cache in your browser (e.g. in google chrome you can press F12 and then go to Network tab and select "Disable cache")

@DavidDeSloovere
Copy link
Author

Hello all, glad to see that this is still used. I updated the gist to add date checks so the snowflakes can be scheduled one in code for those that want to set this and forget.

@dnestico
Copy link

dnestico commented Mar 8, 2024

Perfect! Will update the code now forsure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment