Pass value to command line

Okay. Due to the implementation of shell_command, I can’t think of any possibility of passing parameters to a command_shell service.

So, I’ve came up with a solution.

Let’s start with ~/shell_scripts/setlightgroup:

#!/bin/bash HA_PHUE_HOST="philipshue1" HA_PHUECONF=~/.homeassistant/phue.conf HA_PHUEAPIKEY=$(cat $HA_PHUECONF | awk '{print $3}' | sed -e 's/\"//g' -e 's/}//g'); num='^[0-9]+$' if ! [[ $1 =~ $num ]]; then echo "Not a number." &>2 exit 1 fi if [ -z "$2" ]; then echo "Please enter Entity ID for brightness." &>2 exit 1 fi if [ -z "$3" ]; then echo "Please enter Entity ID for color temperature." &>2 exit 1 fi if ! [[ $4 =~ $num ]]; then echo "Not a number." &>2 exit 1 fi LIGHT_BRI=$(curl -X GET http://localhost:8123/api/states/$2 2> /dev/null | \ tail -n2 | head -n1 | awk '{print $2}' | sed -e 's/"//g' ) LIGHT_CT=$(curl -X GET http://localhost:8123/api/states/$3 2> /dev/null | \ tail -n2 | head -n1 | awk '{print $2}' | sed -e 's/"//g' ) if [ $LIGHT_BRI -gt 0 ]; then CT_Mireds=154 if [ $LIGHT_CT == "Cool" ]; then CT_Mireds=154 elif [ $LIGHT_CT == "Soft" ]; then CT_Mireds=369 elif [ $LIGHT_CT == "Warm" ]; then CT_Mireds=500 fi curl -X PUT -H "Content-Type: application/json" \ -d '{"on":true,"bri":'$LIGHT_BRI',"ct":'$CT_Mireds',"transitiontime":'$4'}' \ http://$HA_PHUE_HOST/api/$HA_PHUEAPIKEY/groups/$1/action 2> /dev/null else curl -X PUT -H "Content-Type: application/json" \ -d '{"on":false,"transitiontime":'$4'}' \ http://$HA_PHUE_HOST/api/$HA_PHUEAPIKEY/groups/$1/action 2> /dev/null fi exit 0

First, the script checks for parameters to make sure they are in check. Second, it gathers the state of the input elements that were set from the frontend. Third, it checks to see if the brightness is greater than 0 and checks what color temperature is set to, be it cool, soft white, or warm white. If the brightness is set to 0, the light turns off. That’s the end of the shell script.

Now to configuration.yaml:

I like to hide my two living room floor lamps. I’ll show you the code:

customize: light.living_room_left: hidden: true light.living_room_right: hidden: true

Next is shell_command:

shell_command: group_lr_fl: ~/shell_scripts/setlightgroup 1 input_slider.input_lr_fl_bri input_select.input_lr_fl_ct 10

This script gets executed by the two input elements, so it’s time to show the two inputs:

[code]input_slider:
input_lr_fl_bri:
name: “Brightness”
initial: 0
min: 0
max: 255
step: 1

input_select:
input_lr_fl_ct:
name: “Color Temperature”
options:
- Cool
- Soft
- Warm[/code]

The inputs are explanatory, so now here’s the last part:

[code]automation:

  • alias: “LR FL Dimmer”
    trigger:
    platform: state
    entity_id: input_slider.input_lr_fl_bri
    action:
    service: shell_command.group_lr_fl
  • alias: “LR FL Color Temperature”
    trigger:
    platform: state
    entity_id: input_select.input_lr_fl_ct
    action:
    service: shell_command.group_lr_fl[/code]

Notice how I left out the parameters for an internal state? It’s simply a “catch-all” state.

Put them all together and now you have the ability to change the brightness and pre-set color temperature.

However, that part is not over yet, so I do have one last question: Is there any way to set initial values that are dependent on group’s brightness and color temperature? A way to execute a shell_command during startup of Home Assistant?