Automation with Slider and Binary switch

Hi All,

I am new to the automation world, so please be patient with me.
I am trying to display values at the front end using sensor template and with if, elif conditions.
I am facing an issue, as first if statement works and i can see the value display on the front end, but conditions after the first if condition seems to be not working.

  lb4:
    friendly_name: "Load Bank 1"
    unit_of_measurement: 'W'
    value_template: >-
      {% if is_state('input_boolean.loadbank_1_100', 'on') %}
         100      
      {% elif is_state('input_boolean.loadbank_1_200', 'on') %}
         200   
      {% else %}
         0   
      {% endif %}

Thanks

Please read the blue banner at the top of the page and edit your post to correctly format your code. Indentation is important in YAML.

However I think I see what you are trying to do and why it is not working.

You are using elif (else if) statements, So the first if statement that evaluates as true will return a value then skip to the endif.The way it is written you will only ever get 100 or 0 as the output because your first input boolean will always evaluate one of these statements true.

What do you actually want?

I think you want the two separate inputs combined to output 0, 100 or 200 for a total load. If so, try this:

value_template: >-
  {% if is_state('input_boolean.loadbank_1_100', 'on') and is_state('input_boolean.loadbank_1_200', 'off') %}
    100
  {% elif is_state('input_boolean.loadbank_1_200', 'on') %}
    200
  {% else %}
    0
  {% endif %}

Hi Tom,

Thanks for the reply.

I am trying to combine slider and switch(input_boolean) to show the value at front end.

if the slider is moved then it should display value corresponding to the slider position, but at the same time if one of the button is turned on it should reflect the value of that button. if more than one buttons are turned on it should sum up values of both switches and display on the sensor output.

1 Like

Ok, that’s a lot more complicated. The summed switch output is easy enough:

value_template: >-
  {% if is_state('input_boolean.loadbank_1_100', 'on') and is_state('input_boolean.loadbank_1_200', 'on') %}
    300
  {% elif is_state('input_boolean.loadbank_1_200', 'on') %}
    200
  {% elif is_state('input_boolean.loadbank_1_100', 'on') %}
    100
  {% else %}
    0
  {% endif %}

The number if statements required will increase as 2 to the power of the number of input booleans. So while this is easy for two inputs, for 8 inputs (as shown in your screen-shot for Yellow House 1) this would require 256 elif statements. Messy.

Also the slider change is an event. This will require an automation and I’m pretty sure there isn’t a service to assign a value to a template sensor for when the event happens. So I’m not sure how this part can be done.

An easier approach would be to forget the template sensor and use an automation to detect the change in any switch and to to set the slider value based on the switch values. Unfortunately this comes a little unstuck as your slider can be changed manually and would then not reflect the switch values.

First off, your slider will never work because you cannot reach 400, 900, 1400, 1900, 2400, 2900, 3400, 3900, 4400, 4900 etc. Sliders will require all numbers from 100 to 15800 in increments of 100. Also, this can get very complicated because the on/off switch combinations can result with the same answer for different combinations. Example:

5000 on, 3000 on, and 1000 on result in 9000

and

5000 on, 4000 on, result in 9000.

All in all, your slider will never work when sliding without making a very complicated script. Jinja (the templates) won’t be able to support it easily.

If you used a different number combination, you could pontially get this to work with a slider, but your combinations would need to be:

100, 200, 400, 800, 1600, 3200, 6400, 12800.

Here is a simple template that will work with your configuration (but not a slider):

sensor:
  - platform: template
    sensors:
      load_bank_1:
        friendly_name: "Load Bank 1"
        unit_of_measurement: 'W'
        value_template: >
          {% set a = 100 if is_state('input_boolean.loadbank_1_100', 'on') else 0 %}
          {% set b = 200 if is_state('input_boolean.loadbank_1_200', 'on') else 0 %}
          {% set c = 500 if is_state('input_boolean.loadbank_1_500', 'on') else 0 %}
          {% set d = 1000 if is_state('input_boolean.loadbank_1_1000', 'on') else 0 %}
          {% set e = 2000 if is_state('input_boolean.loadbank_1_2000', 'on') else 0 %}
          {% set f = 3000 if is_state('input_boolean.loadbank_1_3000', 'on') else 0 %}
          {% set g = 4000 if is_state('input_boolean.loadbank_1_4000', 'on') else 0 %}
          {% set h = 5000 if is_state('input_boolean.loadbank_1_5000', 'on') else 0 %}
          {{ a+b+c+d+e+f+g+h }}

Hi petro,

it seems that yes combining switch and slider will be difficult, I am not sure if slider could be moved based on switch state?

for example:
if slider(limited to 5000 watts) is moved the value will be shown at the front-end, and if the 100_watts switch is turned on then the slider will automatically move to the 100 watts position (on the sliding bar) and again if the slider is moved then all switches will change their state to off position.

by the way I have got value to be shown at the front-end sensor based on the slider movement.

Unfortunately this will trigger your switch watching automation that sets the slider value. This will set your slider back to 0.

The problem isn’t making the slider move. The problem is using the slider to turn on the switches properly. There are slider values that do not match combinations for your switches, which makes using the slider to turn on the switches impossible. Well, not impossible, it just would be a bad slider. YOu’d move the slider to specific positions and nothing would happen because your numbers don’t account for those slider positions. Basically your numerical switches cannot account for 400 or 900 in every 1000 range. Sliders can only have a static increment. You’d want to use 100, but what happens when you put the slider on 400? Nothing will happen, and that’s a bad control.

If you don’t want the slider to control the toggles, then making the slider move is easy with a small automation:

Using the following sensor:

sensor:
  - platform: template
    sensors:
      load_bank_1:
        friendly_name: "Load Bank 1"
        unit_of_measurement: 'W'
        value_template: >
          {% set a = 100 if is_state('input_boolean.loadbank_1_100', 'on') else 0 %}
          {% set b = 200 if is_state('input_boolean.loadbank_1_200', 'on') else 0 %}
          {% set c = 500 if is_state('input_boolean.loadbank_1_500', 'on') else 0 %}
          {% set d = 1000 if is_state('input_boolean.loadbank_1_1000', 'on') else 0 %}
          {% set e = 2000 if is_state('input_boolean.loadbank_1_2000', 'on') else 0 %}
          {% set f = 3000 if is_state('input_boolean.loadbank_1_3000', 'on') else 0 %}
          {% set g = 4000 if is_state('input_boolean.loadbank_1_4000', 'on') else 0 %}
          {% set h = 5000 if is_state('input_boolean.loadbank_1_5000', 'on') else 0 %}
          {{ a+b+c+d+e+f+g+h }}

This automation will move the slider:

- alias: Load Bank 1 Slider automation.
  trigger:
    - platform: state
      entity_id: sensor.load_bank_1
  action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.load_bank_1
        value: "{{ trigger.to_state.state | int }}"

Hi petro,

I want to publish value over the mqtt to turn on the relevant relay on the 8 channel relay shield( mqtt), but not sure as How it will work with the switch (input_binary).
for example: let say if the 100 W, input_binary switch is turned on then it should publish value of 100 (2^0 in binary value) over mqtt.

here is my automation code for the slider:

- alias: Load bank 1 slider moved
  hide_entity: True
  initial_state: on
  trigger:
    platform: state
    entity_id: input_number.slider1
  action:
    service: mqtt.publish
    data_template: 
      topic: 'loadBank/1/power'
      retain: true
      payload: "{{ states('input_number.slider1') | int }}"

wait a sec, does each switch correlate with a bit in binary?

yes, it does. here is the schematic.
I am using 8 pin relay shield on the Arduino.

template should be near identical to the last one then:

          {% set a = '1' if is_state('input_boolean.loadbank_1_100', 'on') else '0' %}
          {% set b = '1' if is_state('input_boolean.loadbank_1_200', 'on') else '0' %}
          {% set c = '1' if is_state('input_boolean.loadbank_1_500', 'on') else '0' %}
          {% set d = '1' if is_state('input_boolean.loadbank_1_1000', 'on') else '0' %}
          {% set e = '1' if is_state('input_boolean.loadbank_1_2000', 'on') else '0' %}
          {% set f = '1' if is_state('input_boolean.loadbank_1_3000', 'on') else '0' %}
          {% set g = '1' if is_state('input_boolean.loadbank_1_4000', 'on') else '0' %}
          {% set h = '1' if is_state('input_boolean.loadbank_1_5000', 'on') else '0' %}
          {{ h+g+f+e+d+c+b+a }}

Assuming the 100 is in the first bit, 200 is in the second bit, etc…

Hi petro,

I tired to implement the automation code for the slider, but it seems that only works with one switch at a time. if I try to turn on more than one switch it stuffs up.

Elaborate please

The only way you can make a slider work is if you don’t provide feedback to the slider. Also, you’ll have to adjust your slider range. You have to use a range of 0 to 255. You cannot use the slider you currently have. You have no choice in this matter because you need to be able to convert the slider integer to a binary representation of your switches.

Input_number configuration

input_number:
  slider1:
    name: Load bank 1 Slider
    initial: 0
    min: 0
    max: 255
    step: 1

Automation

- alias: Load bank 1 slider moved
  hide_entity: True
  initial_state: on
  trigger:
    platform: state
    entity_id: input_number.slider1
  action:
    - service_template: >
        {% set flip = '{:08b}'.format(trigger.to_state.state | int)[-1] | int %}
        input_boolean.turn_{{ 'on' if flip else 'off' }}
      entity_id: input_boolean.loadbank_1_100
    - service_template: >
        {% set flip = '{:08b}'.format(trigger.to_state.state | int)[-2] | int %}
        input_boolean.turn_{{ 'on' if flip else 'off' }}
      entity_id: input_boolean.loadbank_1_200
    - service_template: >
        {% set flip = '{:08b}'.format(trigger.to_state.state | int)[-3] | int %}
        input_boolean.turn_{{ 'on' if flip else 'off' }}
      entity_id: input_boolean.loadbank_1_500
    - service_template: >
        {% set flip = '{:08b}'.format(trigger.to_state.state | int)[-4] | int %}
        input_boolean.turn_{{ 'on' if flip else 'off' }}
      entity_id: input_boolean.loadbank_1_1000
    - service_template: >
        {% set flip = '{:08b}'.format(trigger.to_state.state | int)[-5] | int %}
        input_boolean.turn_{{ 'on' if flip else 'off' }}
      entity_id: input_boolean.loadbank_1_2000
    - service_template: >
        {% set flip = '{:08b}'.format(trigger.to_state.state | int)[-6] | int %}
        input_boolean.turn_{{ 'on' if flip else 'off' }}
      entity_id: input_boolean.loadbank_1_3000
    - service_template: >
        {% set flip = '{:08b}'.format(trigger.to_state.state | int)[-7] | int %}
        input_boolean.turn_{{ 'on' if flip else 'off' }}
      entity_id: input_boolean.loadbank_1_4000
    - service_template: >
        {% set flip = '{:08b}'.format(trigger.to_state.state | int)[-8] | int %}
        input_boolean.turn_{{ 'on' if flip else 'off' }}
      entity_id: input_boolean.loadbank_1_5000

Sensor Template

sensor:
  - platform: template
    sensors:
      load_bank_1:
        friendly_name: "Load Bank 1"
        unit_of_measurement: 'W'
        value_template: >
          {% set a,b,c,d,e,f,g,h = '{:08b}'.format(trigger.to_state.state | int) %}
          {{ a|int*5000+b|int*4000+c|int*3000+d|int*2000+e|int*1000+f|int*500+g|int*200+h|int*100 }}

below is my code :

**configuration.yaml**

homeassistant:
  name: Home
  latitude: xx.xxxx
  longitude: xxx.xxxx
  elevation: 33
  unit_system: metric
  time_zone: Australia/xxxx
  customize: !include customize.yaml
frontend:
config:
http:
updater:
discovery:
history:
logbook:
map:
sun:
tts:
  - platform: google
cloud:

mqtt: 
  broker: 172.17.0.4
  port: 1883
  protocol: 3.1.1


group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
sensor: !include sensors.yaml

input_number:
  slider1:
    name: Load Bank 1
    initial: 0
    min: 0
    max: 5000
    step: 100

  slider2:
    name: Load Bank 2
    initial: 0
    min: 0
    max: 5000
    step: 100
  

  slider3a:
    name: Load Bank 3a
    initial: 0
    min: 0
    max: 5000
    step: 100
  

  slider3b:
    name: Load Bank 3b
    initial: 0
    min: 0
    max: 5000
    step: 100
  

  slider3c:
    name: Load Bank 3c
    initial: 0
    min: 0
    max: 5000
    step: 100


input_boolean:
   loadbank_1_100:
     icon: mdi:toggle-switch
     name: 100_watts
     initial: off

   loadbank_1_1000:
     icon: mdi:toggle-switch
     name: 1000_watts 
     initial: off

   loadbank_1_2000:
     icon: mdi:toggle-switch
     name: 2000_watts 
     initial: off    

   loadbank_1_3000:
     icon: mdi:toggle-switch
     name: 3000_watts 
     initial: off

Automation.yaml

- alias: Load bank 1 switched on 
  hide_entity: True
  initial_state: on
  trigger:
    platform: state
    entity_id: 
      - input_number.slider1
      - input_boolean.loadbank_1_100
      - input_boolean.loadbank_1_1000
      - input_boolean.loadbank_1_2000
      - input_boolean.loadbank_1_3000
  action:
    service: mqtt.publish
    data_template: 
      topic: 'loadBank/1/power'
      retain: true
      payload: >
        {% set a = 100 if (is_state('input_boolean.loadbank_1_100', 'on') or states('input_number.slider1') | int ==100) else 0 %}
        {% set b = 200 if states('input_number.slider1')| int ==200 else 0 %}
        {% set c = 300 if states('input_number.slider1')| int ==300 else 0 %}
        {% set d = 400 if states('input_number.slider1')| int ==400 else 0 %}
        {% set e = 500 if states('input_number.slider1')| int ==500 else 0 %}
        {% set f = 600 if states('input_number.slider1')| int ==600 else 0%}
        {% set g = 700 if states('input_number.slider1')| int ==700 else 0%}
        {% set h = 800 if states('input_number.slider1')| int ==800 else 0%}
        {% set i = 900 if states('input_number.slider1')| int ==900 else 0%}
        {% set j = 1000 if (is_state('input_boolean.loadbank_1_1000', 'on') or states('input_number.slider1') | int ==1000) else 0 %}
        {% set k = 1100 if states('input_number.slider1')| int ==1100  else 0%}
        {% set l = 1200 if states('input_number.slider1')| int ==1200  else 0%}
        {% set m = 1300 if states('input_number.slider1')| int ==1300  else 0%}
        {% set n = 1400 if states('input_number.slider1')| int ==1400  else 0%}
        {% set o = 1500 if states('input_number.slider1')| int ==1500  else 0%}
        {% set p = 1600 if states('input_number.slider1')| int ==1600  else 0%}
        {% set q = 1700 if states('input_number.slider1')| int ==1700  else 0%}
        {% set r = 1800 if states('input_number.slider1')| int ==1800  else 0%}
        {% set s = 1900 if states('input_number.slider1')| int ==1900  else 0%}
        {% set t = 2000 if (is_state('input_boolean.loadbank_1_2000', 'on') or states('input_number.slider1') | int ==2000) else 0 %}
        {% set u = 2100 if states('input_number.slider1')| int ==2100  else 0%}
        {% set v = 2200 if states('input_number.slider1')| int ==2200  else 0%}
        {% set w = 2300 if states('input_number.slider1')| int ==2300  else 0%}
        {% set x = 2400 if states('input_number.slider1')| int ==2400  else 0%}
        {% set y = 2500 if states('input_number.slider1')| int ==2500  else 0%}
        {% set z = 2600 if states('input_number.slider1')| int ==2600  else 0%}
        {% set aa = 2700 if states('input_number.slider1')| int ==2700  else 0%}
        {% set ab = 2800 if states('input_number.slider1')| int ==2800  else 0%}
        {% set ac = 2900 if states('input_number.slider1')| int ==2900  else 0%}
        {% set ad = 3000 if (is_state('input_boolean.loadbank_1_3000', 'on') or states('input_number.slider1') | int ==3000) else 0 %}
        {% set ae = 3100 if states('input_number.slider1')| int ==3100  else 0%}
        {% set af = 3200 if states('input_number.slider1')| int ==3200  else 0%}
        {% set ag = 3300 if states('input_number.slider1')| int ==3300  else 0%}
        {% set ah = 3400 if states('input_number.slider1')| int ==3400  else 0%}
        {% set ai = 3500 if states('input_number.slider1')| int ==3500  else 0%} 
        {% set aj = 3600 if states('input_number.slider1')| int ==3600  else 0%}
        {% set ak = 3700 if states('input_number.slider1')| int ==3700  else 0%}
        {% set al = 3800 if states('input_number.slider1')| int ==3800  else 0%}
        {% set am = 3900 if states('input_number.slider1')| int ==3900  else 0%}
        {% set an = 4000 if states('input_number.slider1')| int ==4000  else 0%}
        {% set ao = 4100 if states('input_number.slider1')| int ==4100  else 0%}
        {% set ap = 4200 if states('input_number.slider1')| int ==4200  else 0%}
        {% set aq = 4300 if states('input_number.slider1')| int ==4300  else 0%}
        {% set ar = 4400 if states('input_number.slider1')| int ==4400  else 0%}
        {% set as = 4500 if states('input_number.slider1')| int ==4500  else 0%}
        {% set at = 4600 if states('input_number.slider1')| int ==4600  else 0%}
        {% set au = 4700 if states('input_number.slider1')| int ==4700  else 0%}
        {% set av = 4800 if states('input_number.slider1')| int ==4800  else 0%}
        {% set aw = 4900 if states('input_number.slider1')| int ==4900  else 0%}
        {% set ax = 5000 if states('input_number.slider1')| int ==5000  else 0%}
        {{ a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p+q+r+s+t+u+v+w+x+y+z+aa+ab+ac+ad+ae+af+ag+ah+ai+aj+ak+al+am+an+ao+ap+aq+ar+as+at+au+av+aw+ax }}
        
       
- alias: Load Bank 1 Switch automation.
  hide_entity: True
  initial_state: on
  trigger:
    - platform: state
      entity_id: 
        - sensor.lb1
        - input_boolean.loadbank_1_100
        - input_boolean.loadbank_1_1000
        - input_boolean.loadbank_1_2000
        - input_boolean.loadbank_1_3000
  action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.slider1
        value: "{{ trigger.to_state.state | int }}"

groups.yaml

YellowHouse1: 
  name: Yellow House 1 
  entities:
    - input_number.slider1
    - input_number.load_bank_1
    - input_boolean.loadbank_1_100
#    - input_boolean.loadbank_1_200
#    - input_boolean.loadbank_1_300
#    - input_boolean.loadbank_1_400
#    - input_boolean.loadbank_1_500
    - input_boolean.loadbank_1_1000
    - input_boolean.loadbank_1_2000
    - input_boolean.loadbank_1_3000
#    - input_boolean.loadbank_1_4000
#    - input_boolean.loadbank_1_5000

sensors.yaml

- platform: template
  sensors:
    lb1:
      friendly_name: "Load Bank 1"
      unit_of_measurement: 'W'
      value_template: >
        {% set a = 100 if (is_state('input_boolean.loadbank_1_100', 'on') or states('input_number.slider1') | int ==100) else 0 %}
        {% set b = 200 if states('input_number.slider1') | int ==200 else 0 %}
        {% set c = 300 if states('input_number.slider1') | int ==300 else 0 %}
        {% set d = 400 if states('input_number.slider1') | int ==400 else 0 %}
        {% set e = 500 if states('input_number.slider1') | int ==500 else 0 %}
        {% set f = 600 if states('input_number.slider1')| int == 600 else 0%}
        {% set g = 700 if states('input_number.slider1')| int == 700 else 0%}
        {% set h = 800 if states('input_number.slider1')| int == 800 else 0%}
        {% set i = 900 if states('input_number.slider1')| int == 900 else 0%}
        {% set j = 1000 if (is_state('input_boolean.loadbank_1_1000', 'on') or states('input_number.slider1') | int ==1000) else 0 %}
        {% set k = 1100 if states('input_number.slider1')| int ==1100  else 0%}
        {% set l = 1200 if states('input_number.slider1')| int ==1200  else 0%}
        {% set m = 1300 if states('input_number.slider1')| int ==1300  else 0%}
        {% set n = 1400 if states('input_number.slider1')| int ==1400  else 0%}
        {% set o = 1500 if states('input_number.slider1')| int ==1500  else 0%}
        {% set p = 1600 if states('input_number.slider1')| int ==1600  else 0%}
        {% set q = 1700 if states('input_number.slider1')| int ==1700  else 0%}
        {% set r = 1800 if states('input_number.slider1')| int ==1800  else 0%}
        {% set s = 1900 if states('input_number.slider1')| int ==1900  else 0%}
        {% set t = 2000 if (is_state('input_boolean.loadbank_1_2000', 'on') or states('input_number.slider1') | int ==2000) else 0 %}
        {% set u = 2100 if states('input_number.slider1')| int ==2100  else 0%}
        {% set v = 2200 if states('input_number.slider1')| int ==2200  else 0%}
        {% set w = 2300 if states('input_number.slider1')| int ==2300  else 0%}
        {% set x = 2400 if states('input_number.slider1')| int ==2400  else 0%}
        {% set y = 2500 if states('input_number.slider1')| int ==2500  else 0%}
        {% set z = 2600 if states('input_number.slider1')| int ==2600  else 0%}
        {% set aa = 2700 if states('input_number.slider1')| int ==2700  else 0%}
        {% set ab = 2800 if states('input_number.slider1')| int ==2800  else 0%}
        {% set ac = 2900 if states('input_number.slider1')| int ==2900  else 0%}
        {% set ad = 3000 if (is_state('input_boolean.loadbank_1_3000', 'on') or states('input_number.slider1') | int ==3000) else 0 %}
        {% set ae = 3100 if states('input_number.slider1')| int ==3100  else 0%}
        {% set af = 3200 if states('input_number.slider1')| int ==3200  else 0%}
        {% set ag = 3300 if states('input_number.slider1')| int ==3300  else 0%}
        {% set ah = 3400 if states('input_number.slider1')| int ==3400  else 0%}
        {% set ai = 3500 if states('input_number.slider1')| int ==3500  else 0%} 
        {% set aj = 3600 if states('input_number.slider1')| int ==3600  else 0%}
        {% set ak = 3700 if states('input_number.slider1')| int ==3700  else 0%}
        {% set al = 3800 if states('input_number.slider1')| int ==3800  else 0%}
        {% set am = 3900 if states('input_number.slider1')| int ==3900  else 0%}
        {% set an = 4000 if states('input_number.slider1')| int ==4000  else 0%}
        {% set ao = 4100 if states('input_number.slider1')| int ==4100  else 0%}
        {% set ap = 4200 if states('input_number.slider1')| int ==4200  else 0%}
        {% set aq = 4300 if states('input_number.slider1')| int ==4300  else 0%}
        {% set ar = 4400 if states('input_number.slider1')| int ==4400  else 0%}
        {% set as = 4500 if states('input_number.slider1')| int ==4500  else 0%}
        {% set at = 4600 if states('input_number.slider1')| int ==4600  else 0%}
        {% set au = 4700 if states('input_number.slider1')| int ==4700  else 0%}
        {% set av = 4800 if states('input_number.slider1')| int ==4800  else 0%}
        {% set aw = 4900 if states('input_number.slider1')| int ==4900  else 0%}
        {% set ax = 5000 if states('input_number.slider1')| int ==5000  else 0%}
        {{ a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p+q+r+s+t+u+v+w+x+y+z+aa+ab+ac+ad+ae+af+ag+ah+ai+aj+ak+al+am+an+ao+ap+aq+ar+as+at+au+av+aw+ax }}

when two or more switches are turned on, the value (addition) of switches are not reflected on the sensor(Load Bank 1).

Right, Like I said, you won’t be able to have a feedback loop here because of how slow the reaction will be. It will continuously fight your on/off. When I say feedback loop, I’m saying you won’t be able to use a slider and have the toggles set the slider.