Automation value_templates

Hi,

I’m trying to automate my magicblue bulbs but I’m having some trouble with the following value_templates, both templates work exactly as I wish them to work if done separately but when put together as below I get an error of duplicate key: “value_template”, so my question is, is there a workaround or is this not going to work at all? Thanks in advance :slight_smile:

 - alias: 'Select and connect to MagicBlue bulb'
   trigger:
     - platform: state
       entity_id: input_select.magicbluebulbs
   action:
     value_template: >
       {% if   is_state('input_select.magicbluebulbs',  'magicblue_redbottombulb') %} -m C1:40:5D:6E:A3:E8
       {% elif is_state('input_select.magicbluebulbs',  'magicblue_pclampbulb') %}    -m F2:F1:09:E6:DE:78 {% endif %}
     value_template: >
       {% if   is_state('input_select.magicbluecolours', 'cool white')  %} "'set_color #FFFFFF'" 
       {% elif is_state('input_select.magicbluecolours', 'red')         %} "'set_color #FF0000'"
       {% elif is_state('input_select.magicbluecolours', 'blue')        %} "'set_color #0000FF'" 
       {% elif is_state('input_select.magicbluecolours', 'green')       %} "'set_color #00FF00'" 
       {% elif is_state('input_select.magicbluecolours', 'purple')      %} "'set_color #6400FF'" 
       {% elif is_state('input_select.magicbluecolours', 'yellow')      %} "'set_color #FBFF00'"
       {% elif is_state('input_select.magicbluecolours', 'cyan')        %} "'set_color #00C8FF'" 
       {% elif is_state('input_select.magicbluecolours', 'orange')      %} "'set_color #FF3800'"
       {% elif is_state('input_select.magicbluecolours', 'slidervalue') %} 'set_color #{{ '%0x' % states.input_slider.red_slider.state | int }}{{ '%0x' % states.input_slider.green_slider.state | int }}{{ '%0x' % states.input_slider.blue_slider.state | int }}' {% endif %}
       - service: shell_command.set_MBBulb_connect_to_slider

I’ve never seen a value_template in an action. Did i miss something?

It would seem you can use them but only as part of a condition…

  action:
    - service: notify.notify
      data:
        message: Testing conditional actions
    - condition: or
      conditions:
        - condition: template
          value_template: '{{ states.sun.sun.attributes.elevation < 4 }}'
        - condition: template
          value_template: '{{ states.sensor.sensorluz_7_0.state < 10 }}'
    - service: scene.turn_on
      entity_id: scene.DespiertaDespacho

Which is not what I want :frowning:

It seems you are not calling any services in your action since value_template is not a service.

Check out this post, hopefully it will help point you in the right direction:
https://community.home-assistant.io/t/pass-value-to-command-line/587/19

Ok ,thanks :slight_smile:

Hmm I get …
17-05-07 16:21:31 ERROR (MainThread) [homeassistant.config] Invalid config for [shell_command]: [set_MBBulbRedBot_setHexCol] is an invalid option for [shell_command]. Check: shell_command->shell_command->set_MBBulbRedBot_setHexCol. (See /home/homeassistant/.homeassistant/configuration.yaml, line 289). Please check the docs at https://home-assistant.io/components/shell_command/
17-05-07 16:21:31 ERROR (MainThread) [homeassistant.setup] Setup failed for shell_command: Invalid config.
17-05-07 16:21:32 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up platform template

Using …
set_MBBulbRedBot_setHexCol: ‘sudo magicblueshell -m C1:40:5D:6E:A3:E8 {{ value }}’

With the automation being...

  - alias: 'Select colour for MagicBlue RedBot bulb Lamp'
   trigger:
     - platform: state
       entity_id: input_select.magicbluecolours
   action:       
     - service: shell_command.set_MBBulbRedBot_setHexCol
       data_template:
         value: >
           {% if   is_state('input_select.magicbluecolours', 'cool white')  %} "-c 'set_color #FFFFFF'" 
           {% elif is_state('input_select.magicbluecolours', 'red')         %} "-c 'set_color #FF0000'"
           {% elif is_state('input_select.magicbluecolours', 'blue')        %} "-c 'set_color #0000FF'" 
           {% elif is_state('input_select.magicbluecolours', 'green')       %} "-c 'set_color #00FF00'" 
           {% elif is_state('input_select.magicbluecolours', 'purple')      %} "-c 'set_color #6400FF'" 
           {% elif is_state('input_select.magicbluecolours', 'yellow')      %} "-c 'set_color #FBFF00'"
           {% elif is_state('input_select.magicbluecolours', 'cyan')        %} "-c 'set_color #00C8FF'" 
           {% elif is_state('input_select.magicbluecolours', 'orange')      %} "-c 'set_color #FF3800'"
           {% elif is_state('input_select.magicbluecolours', 'slidervalue') %} "-c 'set_color #{{ '%0x' % states.input_slider.red_slider.state | int }}{{ '%0x' % states.input_slider.green_slider.state | int }}{{ '%0x' % states.input_slider.blue_slider.state | int }}'" {% endif %}

The only thing I see that I can recommend is possibly removing the double quotes in the value template:

I think you are sending the following command (with double quotes):

sudo magicblueshell -m C1:40:5D:6E:A3:E8 “-c ‘set_color #FFFFFF’”

When you want to be sending the command:

sudo magicblueshell -m C1:40:5D:6E:A3:E8 -c ‘set_color #FFFFFF

Another thing I notice is that you are using single quotations around the shell_command.set_MBBulbRedBot_setHexCol. This may not work as intended since you are also using single quotes around your ‘set_color #FFFFFF’ command. Maybe try surrounding your shell_command script with double quotes instead of single quotes?

set_MBBulbRedBot_setHexCol: “sudo magicblueshell -m C1:40:5D:6E:A3:E8 {{ value }}”

Same error :frowning:

I don’t think it likes {{ value }}

Can anyone see what’s wrong with this shell command?
set_MBBulbRedBot: 'sudo magicblueshell -m C1:40:5D:6E:A3:E8 "set_color {{ states.input_select.magicbluecolours.state }}"'

I’ve tried every combination of double and single quotes but I just can’t seem to get it right, thing is that the command has to have either single or double quotes round the set_color {{ states.input_select.magicbluecolours.state }} part of the command.

17-05-09 00:12:17 ERROR (MainThread) [homeassistant.config] Invalid config for [shell_command]: [set_MBBulbRedBot] is an invalid option for [shell_command]. Check: shell_command->shell_command->set_MBBulbRedBot. (See /home/homeassistant/.homeassistant/configuration.yaml, line 291). Please check the docs at https://home-assistant.io/components/shell_command/
17-05-09 00:12:17 ERROR (MainThread) [homeassistant.setup] Setup failed for shell_command: Invalid config.

Hello!

I was also having a problem with a very simple shell_command (no templating involved) that was causing an invalid config error.

I was trying single and double quotes, but in the end what fixed it was changing the name of the command to lowercase only.

My original command was:

shell_command:
turn_off_NUC: ‘net rpc shutdown --…’

But the one that got working was:

shell_command:
turn_off_nuc: ‘net rpc shutdown --…’

Might be what is happening to you?

Tried lowercase but it’s not the command name that’s the problem, it’s the single or double quotes the command needs to work that causes the problem.