junkfix / numberbox-card

Replace input_number sliders with plus and minus buttons
109 stars 9 forks source link

Feature Request: step size from a helper #50

Closed marcelvandorp closed 2 years ago

marcelvandorp commented 2 years ago

Hi, I want to be able to change a number quickly from a tablet, and this card works great. But I would like the step size to be flexible. I created an input_select helper (with values like '100','10','0.1','-1') I am not a good JS programmer, and I find the documentation of the hass object too complicated for me, but I did look at the code. Around line 32 is the relevant part (I think):

    // WIP: If config.step has the name of an entity, then use it's value as the step size
    if(this.config.step in hass.states){ this.config.step = hass.states[this.config.step].state };
    // end WIP - this is just before original line 32
    if(this.config.step === undefined){ this.config.step=this.stateObj.attributes.step;}

The second line is the only line of code, but I am not sure if I call the hass methods correctly. The last line is unchanged. What I try to do, is: config.step = => use step value on thisObj (current functionality) config.step = => use the as the step size (current functionality) config.step = => use the value of this hass entity as the step value (NEW)

I want to use this to adjust the offsets for a S0PCM5 module, which counts pulses from 5 power meters. Sometimes a pulse or two is missed, and I want an easy way to correct this (on a phone or tablet). So the plan is to use one input_select to determine the step size, and use this as the step size for five numberbox-card entities. I use this same helper value as secondary info for visual feedback to the user (existing functionality).

For a foolproof solution, there should be some sanity checking on the content of the helper (it should be numerical), or perhaps defaults to 0 or undefined if the value is unusable.

Is such an addition easy to implement?

Regards, Marcel

marcelvandorp commented 2 years ago

Okay, I figured it out. In the render() function the step size is determined (line 32): if(this.config.step === undefined){ this.config.step=this.stateObj.attributes.step;} I added below this line the following oneliner (two lines if you also count the comment):

// New Feature: Get step size from another entity if this contains a valid number (float) if(this.config.step_entity in this._hass.states && !isNaN(parseFloat(this._hass.states[this.config.step_entity].state))) {this.config.step=this._hass.states[this.config.step_entity].state;}

This will retrieve the step-size from another entity. My card config is:

type: custom:numberbox-card border: true entity: input_number.test secondary_info: increment value with %input_select.factor:state speed: '500' step_entity: input_select.factor step: 3

The value of 'step:' is only used if the step_entity doesn't exist or doesn't contain a number As you can see, I added the step-size entity to the secondary_info, to inform the user of the current step size

Can this be added to the 'official' code of this extension?

htmltiger commented 2 years ago

Thanks, I have added this in the new version