Is it possible to have conditional data parameters in a platform template?

I have created a platform template for a cover. In this template I need to call 2 different services depending on the state of a switch.

The problem is that the 2 services has different data arguments.

I have tried a lot. Is it possible to call the services with conditional data parameters?

cover:
  - platform: template
    covers:
      test_slow_speed:
        friendly_name: "Test slow speed"
        device_class: shutter
        optimistic: false
        open_cover: 
          service_template: >
            {% if is_state('input_boolean.run_slow_speed_test_slow_speed_cover', 'on') %}
              tahoma.set_cover_position_low_speed
            {% else %}
              cover.open_cover
            {% endif %}    
          entity_id: cover.05_hal_1e_etage
          data_template:
            position: >
              {% if is_state('input_boolean.run_slow_speed_test_slow_speed_cover', 'on') %}
                100
              {% else %}
              {% endif %}         

So the positon data parameter is required with the tahoma.set_cover_position_low_speed service. And with the normal cover.open_cover this position data paramater is not allowed to have.

Yes it should be possible to use any valid service, which includes using data or service templates. However data_template and service_template are no longer required. You can just use data and service.

cover:
  - platform: template
    covers:
      test_slow_speed:
        friendly_name: "Test slow speed"
        device_class: shutter
        optimistic: false
        open_cover: 
          service: >
            {% if is_state('input_boolean.run_slow_speed_test_slow_speed_cover', 'on') %}
              tahoma.set_cover_position_low_speed
            {% else %}
              cover.open_cover
            {% endif %} 
          target:  
            entity_id: cover.05_hal_1e_etage
          data:
            position: >
              {% if is_state('input_boolean.run_slow_speed_test_slow_speed_cover', 'on') %}
                100
              {% else %}
                  ######## YOU NEED SOMETHING HERE ########
              {% endif %}      

This is indeed the problem. The cover.open_cover service doesn’t allow you to specifiy the postion argument.

With this template everything works perfect when controlling the cover on slow speed (with the custom Tahoma service).

But when you control the cover with the normal speed (with the built in cover services) you get the message: “Can’t call the service cover/open_cover extra keys not allowed @data[‘position’]”

Maybe you can try with variables:

cover:
  - platform: template
    covers:
      test_slow_speed:
        friendly_name: "Test slow speed"
        device_class: shutter
        optimistic: false
        open_cover: 
          service: >
            {% if is_state('input_boolean.run_slow_speed_test_slow_speed_cover', 'on') %}
              tahoma.set_cover_position_low_speed
            {% else %}
              cover.open_cover
            {% endif %} 
          target:  
            entity_id: cover.05_hal_1e_etage
          data:
            variables:
              position: >
                {% if is_state('input_boolean.run_slow_speed_test_slow_speed_cover', 'on') %}
                  100
                {% else %}
                    ######## YOU NEED SOMETHING HERE ########
                {% endif %}

You can use a choose action then.

        open_cover: 
          choose:
            - conditions: "{{ is_state('input_boolean.run_slow_speed_test_slow_speed_cover', 'on') }}"
              sequence:
                - service: tahoma.set_cover_position_low_speed
                  target:
                    entity_id: tahoma.set_cover_position_low_speed
                  data:
                    position: 100
          default:
            - service: cover.open_cover
              target:
                entity_id: tahoma.set_cover_position_low_speed
1 Like

I get the same error message:

“Can’t call service cover/open_cover extra keys not allowed @ data[‘variables’]”

I don’t understand it. What do you mean for action?

The action that should be executed is cover.open_cover.

See my edited respose above.

Thanks Tom!! It is working now perfectly! (there was one error in your post: entity_id). This is my complete template cover that works:

      test_slow_speed:
        friendly_name: "Test slow speed"
        device_class: shutter
        optimistic: false
        open_cover: 
          choose:
            - conditions: "{{ is_state('input_boolean.run_slow_speed_test_slow_speed_cover', 'on') }}"
              sequence:
                - service: tahoma.set_cover_position_low_speed
                  target:
                    entity_id: cover.05_hal_1e_etage
                  data:
                    position: 100
          default:
            - service: cover.open_cover
              target:
                entity_id: cover.05_hal_1e_etage        
        close_cover:
          choose:
            - conditions: "{{ is_state('input_boolean.run_slow_speed_test_slow_speed_cover', 'on') }}"
              sequence:
                - service: tahoma.set_cover_position_low_speed
                  target:
                    entity_id: cover.05_hal_1e_etage
                  data:
                    position: 0
          default:
            - service: cover.close_cover
              target:
                entity_id: cover.05_hal_1e_etage        
        stop_cover:
          - service: cover.stop_cover
            data:
              entity_id: cover.05_hal_1e_etage
        set_cover_position:
          choose:
            - conditions: "{{ is_state('input_boolean.run_slow_speed_test_slow_speed_cover', 'on') }}"
              sequence:
                - service: tahoma.set_cover_position_low_speed
                  target:
                    entity_id: cover.05_hal_1e_etage
                  data:
                    position: "{{position}}"
          default:
            - service: cover.set_cover_position
              target:
                entity_id: cover.05_hal_1e_etage        
              data:
                position: "{{position}}"
        position_template: "{{ state_attr('cover.05_hal_1e_etage', 'current_position' ) | int }}"

Is the choose and default actually an ‘if’ and ‘else’?

It is indeed. If you look at the document I linked to it shows exactly that description in the example comments.

Okay so I learned something again :slight_smile: Thanks.

1 Like