Trigger various scripts with input slider

What I am trying to do is to link an input slider to 10 different scripts. So for example when the slider’s value is 1 I want to trigger a script, if the sliders value is 2 then activate another script and so on.

An example of the script i would like to trigger is:
script:
amp_mute:
alias: Mute
sequence:
- service: broadlink.send_packet_192_168_1_79
data:
packet:
-“JgBgADEAAZQLaAhkCycQYgwkFRsbHRtbEBscHB5VGRofJRFRHxkeUBxSG1QdGBwABKFubh5RH08dGxxTHlAeGhxSHRsdGh0bHk4cGx8ZHVEdGx4aHVIcGh1SHFMcUxwYHQANBQAAAAAAAAAA”

is this possible?

I think you would have to do it with 10 different automations…or, it might be a good application for one of the new python scripts.
You could trap a change to the input slider, and as the action, have the python script get the state of the slider, and then go through an if elif condition to decide what to do off of each state… That might work…

Haven’t tested that, but maybe you can use templating to select the script to launch.
If you have your input slider from 1 to 10, and your scripts named my_script_number_1, my_script_number_2, etc…
Something like this:

- trigger:
    - platform: state
      entity_id: input_slider.your_input_slider
  action:
    - service: script.turn_on
      data_template:
        entity_id: script.my_script_number_{{ trigger.to_state.state }} 

Hi Everyone,

I have an LED Dimmer module that I control via script commands. If I could link this to a input slider that would be great.

I found this post - Send message to IP:PORT But unfortunately have not be able to link my scripts to an input slider.

So far my script looks like:

kitchen:
alias: Kitchen Brightness
sequence:
- service_template: >
{% if brightness == 100 %}
shell_command.quinled_kitchen_100
{% elif brightness == 75 %}
shell_command.quinled_kitchen_75
{% elif brightness == 50 %}
shell_command.quinled_kitchen_50
{% elif brightness == 35 %}
shell_command.quinled_kitchen_35
{% elif brightness == 15 %}
shell_command.quinled_kitchen_15
{% elif brightness == 00 %}
shell_command.quinled_kitchen_00
{% else %}
//NOTIFY YOURSELF because this should not happen
{% endif %}

Any advise / help would be appreciated

First off please use preformatted text for configs. Also we would need a bit more info here. Could you post the config of all components involved (Not the scripts obviously)? Right now I can’t understand where 'brightness ’ comes from.

~Cheers

1 Like

Thanks for the reply - I have tweaked the script as per below based on another post I found.

configuration.yaml

input_slider:
  kitchen_brightness:
    name: Kitchen Brightness
    initial: 5
    min: 0
    max: 5
    step: 1

Scripts.yaml

  kitchen:
    alias: Kitchen Brightness
    sequence:
      - service_template: >
          {% if states.input_slider.kitchen_brightness.state == 5 %}
            shell_command.quinled_kitchen_100
          {% elif states.input_slider.kitchen_brightness.state== 4 %}
            shell_command.quinled_kitchen_75
          {% elif states.input_slider.kitchen_brightness.state== 3 %}
            shell_command.quinled_kitchen_50
          {% elif states.input_slider.kitchen_brightness.state== 2 %}
            shell_command.quinled_kitchen_35
          {% elif states.input_slider.kitchen_brightness.state== 1 %}
            shell_command.quinled_kitchen_15
          {% elif states.input_slider.kitchen_brightness.state== 0 %}
            shell_command.quinled_kitchen_00
          {% else %}
            //NOTIFY YOURSELF because this should not happen
          {% endif %}

shell_commands.yaml

quinled_kitchen_100: sh /home/homeassistant/.homeassistant/shell_commands/quinled_kitchen_100.sh
quinled_kitchen_75: sh /home/homeassistant/.homeassistant/shell_commands/quinled_kitchen_75.sh
quinled_kitchen_50: sh /home/homeassistant/.homeassistant/shell_commands/quinled_kitchen_50.sh
quinled_kitchen_35: sh /home/homeassistant/.homeassistant/shell_commands/quinled_kitchen_35.sh
quinled_kitchen_15: sh /home/homeassistant/.homeassistant/shell_commands/quinled_kitchen_15.sh
quinled_kitchen_00: sh /home/homeassistant/.homeassistant/shell_commands/quinled_kitchen_00.sh

Can you test out your if statements in the dev tools to make sure that they work?

~Cheers

shell commands work fine via dev tools, the slider input does not do anything. not sure how to test the script?

ok, so if i run the script via dev tools as per attached, I get the below - but sure I am not using the correct formant for service data option.

2017-09-13 16:19:32 ERROR (MainThread) [homeassistant.components.websocket_api] WS 1853298480: Message incorrectly formatted: expected dict for dictionary value @ data[‘service_data’]. Got 5
2017-09-13 16:19:39 ERROR (MainThread) [homeassistant.components.websocket_api] WS 1852870352: Message incorrectly formatted: expected dict for dictionary value @ data[‘service_data’]. Got 2
2017-09-13 16:19:58 ERROR (MainThread) [homeassistant.components.websocket_api] WS 1850307792: Message incorrectly formatted: expected dict for dictionary value @ data[‘service_data’]. Got 5
2017-09-13 16:20:37 ERROR (MainThread) [homeassistant.helpers.service] Template rendered invalid service: //NOTIFY YOURSELF because this should not happen
2017-09-13 16:20:58 ERROR (MainThread) [homeassistant.components.websocket_api] WS 1922534480: Message incorrectly formatted: expected dict for dictionary value @ data[‘service_data’]. Got 5
2017-09-13 16:21:17 ERROR (MainThread) [homeassistant.helpers.service] Template rendered invalid service: //NOTIFY YOURSELF because this should not happen
2017-09-13 16:14:54 ERROR (MainThread) [homeassistant.helpers.service] Template rendered invalid service: //NOTIFY YOURSELF because this should not happen

I meant to check the template for rendering. You can just paste this

{% if states.input_slider.kitchen_brightness.state == 5 %}
            shell_command.quinled_kitchen_100
          {% elif states.input_slider.kitchen_brightness.state== 4 %}
            shell_command.quinled_kitchen_75
          {% elif states.input_slider.kitchen_brightness.state== 3 %}
            shell_command.quinled_kitchen_50
          {% elif states.input_slider.kitchen_brightness.state== 2 %}
            shell_command.quinled_kitchen_35
          {% elif states.input_slider.kitchen_brightness.state== 1 %}
            shell_command.quinled_kitchen_15
          {% elif states.input_slider.kitchen_brightness.state== 0 %}
            shell_command.quinled_kitchen_00
          {% else %}
            //NOTIFY YOURSELF because this should not happen
          {% endif %}

in there. The template dev tool is the fourth one from the left. You can then play around with your if clauses to check whats wrong. e.g. does this

{{ if states.input_slider.kitchen_brightness.state == 5 }}

return true if your slider is on 5? If no what does

{{ states.input_slider.kitchen_brightness.state }}

return etc.

~Cheers

OK - so if I run below as suggested I have figured out that the slider value is 1.0 / 2.0 / etc and not just 0 / 1 / etc. Getting an error on the first command that I am not sure how to correct.

{{ if states.input_slider.kitchen_brightness.state == 5.0 }}

I get

When I run

{{ states.input_slider.kitchen_brightness.state }}

I get

Sorry that was an error on my end…

{{ if states.input_slider.kitchen_brightness.state == 5.0 }}

should be

{{ states.input_slider.kitchen_brightness.state == 5.0 }}

If you try it with

{{ states.input_slider.kitchen_brightness.state | int }}

You should get what you need. Either use the | int or check for 5.0 etc.

~Cheers

1 Like

Thanks - getting there

I get a value of “5” with below command

06 PM

and “5.0” with

19 PM

When moving my slider it is not invoking the scripts, and I suspect its because I am not getting a “true” with below commands… but not sure how to troubleshoot further…

You have two options here either use:

          {% if states.input_slider.kitchen_brightness.state == "5.0" %}
            shell_command.quinled_kitchen_100
          {% elif states.input_slider.kitchen_brightness.state== "4.0" %}
            shell_command.quinled_kitchen_75
          {% elif states.input_slider.kitchen_brightness.state== "3.0" %}
            shell_command.quinled_kitchen_50
          {% elif states.input_slider.kitchen_brightness.state== "2.0" %}
            shell_command.quinled_kitchen_35
          {% elif states.input_slider.kitchen_brightness.state== "1.0" %}
            shell_command.quinled_kitchen_15
          {% elif states.input_slider.kitchen_brightness.state== "0.0" %}
            shell_command.quinled_kitchen_00
          {% else %}
            //NOTIFY YOURSELF because this should not happen
          {% endif %}

or use

          {% if states.input_slider.kitchen_brightness.state | int == 5 %}
            shell_command.quinled_kitchen_100
          {% elif states.input_slider.kitchen_brightness.state | int == 4 %}
            shell_command.quinled_kitchen_75
          {% elif states.input_slider.kitchen_brightness.state | int == 3 %}
            shell_command.quinled_kitchen_50
          {% elif states.input_slider.kitchen_brightness.state | int == 2 %}
            shell_command.quinled_kitchen_35
          {% elif states.input_slider.kitchen_brightness.state | int == 1 %}
            shell_command.quinled_kitchen_15
          {% elif states.input_slider.kitchen_brightness.state | int == 0 %}
            shell_command.quinled_kitchen_00
          {% else %}
            //NOTIFY YOURSELF because this should not happen
          {% endif %}

if you are sure there are only integers coming from that slider I think the second would be the best. Also please replace //NOTIFY YOURSELF because this should not happen with an actual service call :slight_smile:

~Cheers

PS: The reason for this is that the state seems to return a string…and you compare it with numbers the double quotes tell the jinja2 engine that it is a string and the | int tells the engine that it should convert what comes before it into an integer.

1 Like

Awesome !! this script now works and I get the required responses I believe from the dev tools.

I just need to link the slider to interact correctly - will play around over the weekend. Thanks very much

1 Like

Did you manage to get your input_slider / input_number to function correctly? If so could you share the workings as I have a need for the same functionality to control 2 relays.

Thanks.

If anyone is wondering, here is the solution that I found to the slider calling a script file.

So running a slightly later version of HA than that which uses input_slider (0.61.1) which changes input_slider to input_number.

input_number:
  heat_temperature:
    name: Heating Power 1-5
    initial: 1
    min: 1
    max: 5
    step: 1

shell_command:
  set_heat_to_slider: "bash /path.to/homeassistant/.homeassistant/fire.{{ states.input_number.heat_temperature.state }}"
  
  - alias: "Pellet Heat Setting"   
    trigger:
      platform: state
      entity_id: input_number.heat_temperature
    action:
      service: shell_command.set_heat_to_slider

The script that this calls are fire.X.0 where X is the corresponding input_number.

Hope this helps

Bananaskin

1 Like