Looking for a variable component

Hi,

I’m wanting to create a variable that increments up +1 each time a sensor in the room is activated, and then decreases -1 each time it is deactivated. Then I want to use the value of this variable to control the lights. e.g.

Lights turn on if variable is above 0
Lights turn off if variable = 0

I’m just not sure how to create this variable. It would only need to hold the values 0 to 5. Seems like such a simple thing to implement I just can’t think how to do it! Any help much appreciated.

Input Slider. Also being a “visual” component, the example section demonstrates how to set a value programmatically.

Yes. Input slider. There’s not a string variable container that you can use, unfortunately.

I use input slider in my automation that remembers the dim setting for a light to return it after responding to motion.

1 Like

We need this…we need a way to SET a sensor with a service…desperatly imho.

~Cheers

Agreed. The only way to do that now is via the API.
(@PhyberApex - was that you that I had this same discussion with on a separate thread?)

1 Like

Very much likely. I hate having to call a python script just to set a sensor state :confused:

~Cheers

Thanks for your responses. I’ve made a bit of progress but can’t get it working as I’d like. I set up an input_slider and I’m trying to set the value +1 / -1 depending on the value of my motion sensor. This is what I have so far. The increment up worked once but then it stopped working. No idea why it worked. Any pointers welcome!

  - alias: "Increment office counter when motion sensor activates"
    trigger:
      platform: numeric_state
      entity_id: sensor.office_motion
      above: 0
    action:
      service: input_slider.select_value
      data_template:
        entity_id: input_slider.office_counter
        value: '{{ + 1 }}'
        
  - alias: "Decrease office counter when motion sensor activates"
    trigger:
      platform: numeric_state
      entity_id: sensor.office_motion
      below: 1
    action:
      service: input_slider.select_value
      data_template:
        entity_id: input_slider.office_counter
        value: '{{ - 1 }}'

why don’t you try?

value: '{{ (states.input_slider.office_counter.state | int) - 1 }}'

~Cheers

1 Like

That did the trick. Thanks a lot! And thanks for the suggestion of the input slider m0wlheld / ih8gates. The HA community is great!

1 Like

For real, why is this not a thing? It would be nice to set a sensor value like every day at midnight etc…

On Vera, there was a handy plugin called “variable container”. It basically just gave you 4 empty locations to store and access strings. It’d be handy to have a similar object in HA that had actions to set, concatenate, retrieve and clear. I’ll create a feature request.

In case anyone is still looking for a variables component.

I wrote a custom component which does just that:

3 Likes