Jump to content
🌟 NOTIFICATION/Benachrichtigung: Welcome to our New Store! - shelly.com 🌟 ×

Shelly Plus Plug S - power controlled LED light ring (1st ChatGPT experience)


Recommended Posts

At first I have to admit that I can't write Java Script Code. I learned several programming languages on university (about 200 years ago), but didn't use one of them later on the job or private. But I can read and in broad terms understand any programming Code.

Until now I had no use of any smarthome toys, but this has changed on installing solar panels on my house some weeks ago. I found it necessary to merge data from the inverter with data from the electricity meter in one system to have a good overview of all the parameters of the system. So I was searching around and found a smarthome solution called everhome that not only meets my needs. They offer much more and work a lot with Shelly-Components.

During playing around I found a really useful application for me that I wanted to implement into the system besides all the solar stuff:

I am using an automatic water pump that prevents my house from getting a wet basement when it's raining to hard. Problem: one can't hear the pump working and there's no way of controlling if it works. So during last Christmas I had some water in the house, because the old pump refused to work and we didn't notice this.

But how can I make the (new) pump (a little bit) smart?

So I ordered a Shelly Plus Plug S and a Shelly Flood. Both were installed in minutes and connected to my system. Whenever the pump starts working I now get a message on the smartphone and when Shelly Flood detects water too. I will even receive a message as soon as the battery power get's below 10%.

But then I had another idea: why not showing the status of the pump on the LED ring. I decided the following color schema: blue=>Plug OFF, green=>Plug ON, red=>Pump ON.

I decided to try out ChatGPT to work as a "coding slave" – and I have to say: it really worked.

Whoever tries to go this way let me tell you: If you don't have any experience or at least basic knowledge of programming and programming languages ... FORGET ABOUT  IT!

ChatGPT ist writing code very fast and is able to vary code super quick if it doesn't work or produces errors. But it was necessary to provide links with code sniplets or API information to him. I needed to feed him with ideas of alternative solutions (he also offered his own, but those were to complex for the little brain of the Shelly Plug). He also recognizes irony, especially if you use the 😉 - and he is able to understand metaphors and change his working attitude accordingly.

But he also failed several times: forgot closing }, couldn't remember that RGB details vary from 0-100 not 0-255, after hours he was quite confused and mixed different ways we had tried into one Script ... So you must be able to cooperate with ChatGPT at eye level. You must tell him what to do and you must be able to check his results and guide him.

Okay, enough talking. Here is finally our Script who works fantastic.

// Script for the power controlled lightning of the LED-ring of the Shelly Plus Plug S
// Version 1: OFF=blue / ON=green / power consumption=red
// You can enter a power threshold if your attached device has a little StandBy power
// consumption. The LED-ring changes to red if the power consumption exceeds this value (device is on).
// Author: MR / Co-Author: ChatGPT - 19.8.2024

let power_threshold = 5; // Watt

function setLedColor(state, power) {
  let r = 0, g = 0, b = 0, brightness = 0;

  if (state) {
    if (power <= power_threshold) {
      r = 0;
      g = 100;
      b = 0;
      brightness = 50;
    } else {
      r = 100;
      g = 0;
      b = 0;
      brightness = 70;
    }
  }
  
  Shelly.call(
    "PLUGS_UI.SetConfig", {
      id: 0,
      config: {
        "leds": {
          "mode": "switch",
          "colors": {
            "switch:0": {
              "on": {
                "rgb": [r, g, b],
                "brightness": brightness
              },
              "off": {
                "rgb": [0, 0, 100],
                "brightness": 50
              }
            }
          }
        }
      }
    },
    null,
    null
  );
}

function checkPower() {
  Shelly.call("Switch.GetStatus", { id: 0 }, function (result) {
    if (result && result.hasOwnProperty("apower")) {
      let power = result.apower;
      let state = result.output;
      setLedColor(state, power);
    }
  });
}

Timer.set(1000, true, function () {
  checkPower();
});

checkPower();

I am actually working on a variation of this Script for another application. It actually works, but needs some cosmetics. Will publish it in this thread soon. Believe me: what seemed to be just a little variation developed into a hell ride ...

Edited by Shellist
Link to comment
Share on other sites

Posted (edited)

Here is a second Version of my Script. It works wonderful, but unfortunately it is not as smooth as expected - due to hardware limitations. The Plus Plug S is not a Mac Pro. Please feel free to use it for optimisation and let me know here if you find a better solution.

You can now set not only a single color for every status of the plug - you can create real light-animations! That looks cool man!!!

If you find a good animation let us all know and post it here.

Have fun!

 

// Script for the power controlled lightning of the LED-ring of the Shelly Plus Plug S
// Version 2: set animations for every status (ON/OFF/POWER)
// You can enter a power threshold if your attached device has a little StandBy power consumption.
// The LED-ring changes to POWER-animation if the power consumption exceeds this value and device is ON.
// You can also set the timer speed. If you go higher animation will play slower. If you go under 100 the animation becomes unrhythmic
// due to hardware limitations.
// ATTENTION: all settings for the light elements are 0-100! The string is: [r, g, b, brightness, time_units]
// If you want a fixed light just set one single light element.
// Author: MR / Co-Author: ChatGPT - 27.8.2024

const offanimation = [
    [0, 0, 100, 10, 1],   // blue - 10% brightness - 1 time unit
    [0, 0, 100, 100, 1]   // blue - 100% brightness - 1 time unit
]; // This is just an even blinking

const onanimation = [
    [0, 100, 0, 30, 1],   // green - 30% brightness - 1 time unit
    [0, 100, 0, 40, 1],   // green - 40% brightness - 1 time unit
    [0, 100, 0, 50, 1],   // green - 50% brightness - 1 time unit
    [0, 100, 0, 60, 1],   // green - 60% brightness - 1 time unit
    [0, 100, 0, 70, 1],   // green - 70% brightness - 1 time unit
    [0, 100, 0, 80, 1],   // green - 80% brightness - 1 time unit
    [0, 100, 0, 90, 1],   // green - 90% brightness - 1 time unit
    [0, 100, 0, 100, 1],  // green - 100% brightness - 1 time unit
    [0, 100, 0, 90, 1],   // green - 90% brightness - 1 time unit
    [0, 100, 0, 80, 1],   // green - 80% brightness - 1 time unit
    [0, 100, 0, 70, 1],   // green - 70% brightness - 1 time unit
    [0, 100, 0, 60, 1],   // green - 60% brightness - 1 time unit
    [0, 100, 0, 50, 1],   // green - 50% brightness - 1 time unit
    [0, 100, 0, 40, 1]    // green - 40% brightness - 1 time unit
];  // This one is a pulsing green light

const poweranimation = [
    [100, 0, 0, 10, 50],   // red - 10% brightness - 50 time units
    [100, 0, 0, 50, 1],    // red - 50% brightness - 1 time unit
    [100, 0, 0, 100, 20]   // red - 100% brightness - 20 time units
];  // And this one imitates a heart-beat

const threshold = 5;           // Threshold for POWER-status
const timerInterval = 100;     // Timer-speed in ms

let currentStatus = "";
let i = 0;

function setLedColor(color) {
    let r = color[0];
    let g = color[1];
    let b = color[2];
    let h = color[3];
    
    // Setze die LED-Farbe
    Shelly.call("PLUGS_UI.SetConfig", {
        id: 0,
        config: {
            leds: {
                mode: "switch",
                colors: {
                    "switch:0": {
                        on: {
                            rgb: [r, g, b],
                            brightness: h
                        },
                        off: {
                            rgb: [r, g, b],
                            brightness: h
                        }
                    }
                }
            }
        }
    });
}

function updateLedStatus(animation) {
    setLedColor(animation[i]);
    i = (i + 1) % animation.length;
}

function mainLoop() {
    Shelly.call("Switch.GetStatus", { id: 0 }, function(result) {
        let power = result && result.hasOwnProperty('apower') ? result.apower : 0;
        let newStatus = result && result.output ? (power > threshold ? "POWER" : "ON") : "OFF";

        if (newStatus !== currentStatus) {
            currentStatus = newStatus;
            i = 0;
        }

        if (currentStatus === "OFF") {
            updateLedStatus(offanimation);
        } else if (currentStatus === "ON") {
            updateLedStatus(onanimation);
        } else if (currentStatus === "POWER") {
            updateLedStatus(poweranimation);
        }

        Timer.set(timerInterval, false, mainLoop);
    });
}

mainLoop();

 

Edited by Shellist
Link to comment
Share on other sites

The unicorn is riding the rainbow ...

 

const onanimation = [
    [50, 0, 0, 100, 1], 
    [51, 16, 16, 100, 1], 
    [55, 33, 23, 100, 1], 
    [60, 40, 46, 100, 1], 
    [60, 40, 66, 100, 1], 
    [50, 0, 50, 100, 1], 
    [40, 0, 61, 100, 1], 
    [28, 0, 88, 100, 1], 
    [2, 0, 82, 100, 1], 
    [0, 27, 86, 100, 1], 
    [0, 45, 89, 100, 1], 
    [0, 62, 91, 100, 1], 
    [4, 69, 64, 100, 1], 
    [9, 70, 30, 100, 1], 
    [0, 83, 11, 100, 1], 
    [0, 100, 0, 100, 1], 
    [50, 100, 0, 100, 1], 
    [78, 100, 0, 100, 1], 
    [100, 100, 0, 100, 1], 
    [100, 86, 0, 100, 1], 
    [100, 71, 0, 100, 1], 
    [100, 57, 0, 100, 1], 
    [100, 43, 0, 100, 1], 
    [100, 29, 0, 100, 1], 
    [100, 0, 0, 100, 1], 
    [100, 0, 50, 100, 1], 
    [100, 41, 71, 100, 1], 
    [100, 0, 100, 100, 1], 
    [66, 0, 73, 100, 1], 
    [0, 0, 0, 0, 100]
];

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Erstelle neue...