Error when passing variables

I try to pass a variable to a script. I want my Google Home lower it’s volume when my son is sleeping, based on a input_select. Unfortunately I get an error and I am not that experienced with variables. I think I messed up some examples I found on the forum.

I got this automation and script:

alias: change volume when son is sleeping
trigger:
  platform: state
  entity_id: 
    - input_select.niek_status
action:
  service_template: >-
    {%
      if (
        is_state('input_select.niek_status', 'sleeping')
      )
    %}
      service: script.set_volume_googlehome
      data_template:
        variables:
          volume: '{{ 30 }}'
    {%
      elif (
        is_state('input_select.niek_status', 'awake')
      )
    %}
      service: script.set_volume_googlehome
      data_template:
        variables:
          volume: '{{ 70 }}'
    {%- endif %}
set_volume_googlehome:
  alias: 'Set volume Google Home'
  sequence:
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.home
        volume_level: '{{ volume }}'

The error I get:

2020-03-06 15:32:44 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.change_volume_when_son_is_sleeping. Invalid data for call_service at pos 1: Service service: script.set_volume_googlehome
  data_template:
    variables:
      volume: 70 does not match format <domain>.<name>

Your service_template is incorrect. Should be something like:

  service: script.set_volume_googlehome
  data_template:
    volume: >
      {% if is_state('input_select.niek_status', 'sleeping') %}
        0.3
      {% else %}
        0.7
      {% endif %}

This might be an alternative (though Home Assistant might complain about the output being a string instead of a float in which case it wouldn’t work):

  service: script.set_volume_googlehome
  data_template:
    volume: "{{ '0.3' if is_state('input_select.niek_status', 'sleeping') else '0.7' }}"

I changed the automation to this, but got the same error:

alias: change volume when son is sleeping
trigger:
  platform: state
  entity_id: 
    - input_select.niek_status
action:
  service_template: >-
    service: script.set_volume_googlehome
    data_template:
      volume: >
        {% if is_state('input_select.niek_status', 'sleeping') %}
          0.3
        {% else %}
          0.7
        {% endif %}
  volume: >
    
      0.3 does not match format <domain>.<name>

Sorry, I wasn’t clear. Remove service_template: entirely.

Thanks! It’s working! :slight_smile:

You were clear for a average HA user, but with my lack of experience in this it was unclear :wink: