Snille / MMM-homeassistant-sensors

Shows home assistant sensors on the Magic Mirror.
MIT License
63 stars 15 forks source link

[Feature Request] Use 'controlsensorvalue' with above-value or below-value #10

Closed Schnabulation closed 1 year ago

Schnabulation commented 1 year ago

Hi all

Just a quick feature request: It would be neat if one could use the controlsensorvalue with a decimal value and a logarithmic comparison.

Ex.

if (controlsensorvalue > 100) {
 display sensor
}

Thank you and best regards

Snille commented 1 year ago

This is a bit tricky because the control sensor value can basically be anything: a number, a text-string, a boolean or whatever you decide. So, for the moment I'm using "negative" checking. The "default" value is always set to "sensor control disabled", so if it's NOT that display the module... It will be a real pain to check everything "else" in another way. :)

Schnabulation commented 1 year ago

Thanks for your feedback. I have checked your code and can understand what you mean.

May I suggest the following approach? (Attention: this is just pseudo code and needs clean up)

    defaults: {
        controlsensorcompare: 'equal' // The comparison algorithm that the control sensor will have.
    },

if (this.config.controlsensor !== "sensor control disabled") {
                var stateval = this.getState(data, this.config.controlsensor);
                var compareval = this.getState(data, this.config.controlsensorcompare);
                switch (compareval) {
                    case "equal":
                        if ((stateval !== this.config.controlsensorvalue && this.config.controlsensorvalue !== "sensor control disabled")) {
                        // this.hide() does not work well with MMM-Pages, so use wrapper.style.display instead
                           if (wrapper.style.display != "none") {
                              this.visibleStyle = wrapper.style.display;
                              wrapper.style.display = "none";
                           }
                        } else {
                           if (wrapper.style.display == "none") {
                              wrapper.style.display = this.visibleStyle;
                           }
                        }
                    case "above":
                        if ((stateval < this.config.controlsensorvalue)) {
                            // this.hide() does not work well with MMM-Pages, so use wrapper.style.display instead
                            if (wrapper.style.display != "none") {
                              this.visibleStyle = wrapper.style.display;
                              wrapper.style.display = "none";
                           }
                        } else {
                           if (wrapper.style.display == "none") {
                              wrapper.style.display = this.visibleStyle;
                           }
                        }
                    case "below":
                        if ((stateval > this.config.controlsensorvalue)) {
                            // this.hide() does not work well with MMM-Pages, so use wrapper.style.display instead
                           if (wrapper.style.display != "none") {
                              this.visibleStyle = wrapper.style.display;
                              wrapper.style.display = "none";
                           }
                        } else {
                           if (wrapper.style.display == "none") {
                              wrapper.style.display = this.visibleStyle;
                           }
                        }
                    case "notequal":
                        if ((stateval == this.config.controlsensorvalue)) {
                        // this.hide() does not work well with MMM-Pages, so use wrapper.style.display instead
                           if (wrapper.style.display != "none") {
                              this.visibleStyle = wrapper.style.display;
                              wrapper.style.display = "none";
                           }
                        } else {
                           if (wrapper.style.display == "none") {
                              wrapper.style.display = this.visibleStyle;
                           }
                        }
                }
                // If the control sensor value is anything not the default or not the defined value, hide the module.

             }
Snille commented 1 year ago

Hmm... That may work... Not sure of you can compare a string with "above" ( > greater then) or "below" ( < Less then) in this way? If you want to try it, just fork and test, if it works, send a PR. :) But I can see that it would be a good idea, maybe we could do it / sensor bases instead... I'll have to think about it. :)

Snille commented 1 year ago

Ok, I have added a "highDisplayThreshold" and "lowDisplayThreshold". These can be used to only display sensors in the "specified" range... Let me know if this works... :)

Schnabulation commented 1 year ago

Amazing! Thank you very much!