Use dynamic entities in script

Hey everyone,

I am trying to automate some ceiling fans that have RF remotes. I am able to work them out, but there is a lot of code duplication between the fans, so I am trying to come up with parametrized scripts to avoid the duplication.

I am able to do this just fine:

backyard_fan_power:
  sequence:
    - service: remote.send_command
      target:
        entity_id: remote.rm4_pro
      data:
        command: "{{ fan_command_power }}"
    - service: input_boolean.turn_{{ fan_operation }}
      target:
        entity_id: "{{ fan_state_entity_id }}"
    - service: input_number.set_value
      target:
        entity_id: "{{ fan_speed_entity_id }}"
      data:
        value: "{{ 33 if fan_command == 'on' else 0 }}"
    - service: input_number.set_value
      target:
        entity_id: "{{ fan_speed_previous_entity_id }}"
      data:
        value: "{{ 33 if fan_command == 'on' else 0 }}"

But this doesn’t work:

backyard_fan_set_speed:
  sequence:
    - service: input_number.set_value
      target:
        entity_id: "{{ fan_speed_entity_id }}"
      data:
        value: "{{ percentage }}"
    - choose:
        # want to get fan to 33%
        - conditions:
            - condition: state
              entity_id: "{{ fan_speed_entity_id }}"
              state: "33.0"
          sequence:
            - choose:
                # fan is at 66%, decrease once
                - conditions:
                    - condition: state
                      entity_id: "{{ fan_speed_previous_entity_id }}"
                      state: "66.0"
                  sequence:
                    - service: remote.send_command
                      target:
                        entity_id: remote.rm4_pro
                      data:
                        command: "{{ fan_command_speed_dec }}"
                    - service: input_number.set_value
                      target:
                        entity_id: "{{ fan_speed_previous_entity_id }}"
                      data:
                        value: 33
                # fan is at 100%, decrease twice
                - conditions:
                    - condition: state
                      entity_id: "{{ fan_speed_previous_entity_id }}"
                      state: "100.0"
                  sequence:
                    - service: remote.send_command
                      target:
                        entity_id: remote.rm4_pro
                      data:
                        command: "{{ fan_command_speed_dec }}"
                        num_repeats: 2
                    - service: input_number.set_value
                      target:
                        entity_id: "{{ fan_speed_previous_entity_id }}"
                      data:
                        value: 33
        # want to get fan to 66%
        - conditions:
            - condition: state
              entity_id: "{{ fan_speed_entity_id }}"
              state: "66.0"
          sequence:
            - choose:
                # fan is at 33%, increase once
                - conditions:
                    - condition: state
                      entity_id: "{{ fan_speed_previous_entity_id }}"
                      state: "33.0"
                  sequence:
                    - service: remote.send_command
                      target:
                        entity_id: remote.rm4_pro
                      data:
                        command: "{{ fan_command_speed_inc }}"
                    - service: input_number.set_value
                      target:
                        entity_id: "{{ fan_speed_previous_entity_id }}"
                      data:
                        value: 66
                # fan is at 100%, decrease once
                - conditions:
                    - condition: state
                      entity_id: "{{ fan_speed_previous_entity_id }}"
                      state: "100.0"
                  sequence:
                    - service: remote.send_command
                      target:
                        entity_id: remote.rm4_pro
                      data:
                        command: "{{ fan_command_speed_dec }}"
                    - service: input_number.set_value
                      target:
                        entity_id: "{{ fan_speed_previous_entity_id }}"
                      data:
                        value: 66

        # want to get fan to 100%
        - conditions:
            - condition: state
              entity_id: "{{ fan_speed_entity_id }}"
              state: "100.0"
          sequence:
            - choose:
                # fan is at 33%, increase twice
                - conditions:
                    - condition: state
                      entity_id: "{{ fan_speed_previous_entity_id }}"
                      state: "33.0"
                  sequence:
                    - service: remote.send_command
                      target:
                        entity_id: remote.rm4_pro
                      data:
                        command: "{{ fan_command_speed_inc }}"
                        num_repeats: 2
                    - service: input_number.set_value
                      target:
                        entity_id: "{{ fan_speed_previous_entity_id }}"
                      data:
                        value: 100
                # fan is at 66%, increase once
                - conditions:
                    - condition: state
                      entity_id: "{{ fan_speed_previous_entity_id }}"
                      state: "66.0"
                  sequence:
                    - service: remote.send_command
                      target:
                        entity_id: remote.rm4_pro
                      data:
                        command: "{{ fan_command_speed_inc }}"
                    - service: input_number.set_value
                      target:
                        entity_id: "{{ fan_speed_previous_entity_id }}"
                      data:
                        value: 100
      default: []

The error I get is:

2023-05-31 22:20:38.124 ERROR (MainThread) [homeassistant.components.script] Script with object id 'backyard_fan_set_speed' could not be validated and has been disabled: Entity {{ fan_speed_entity_id }} is neither a valid entity ID nor a valid UUID for dictionary value @ data['sequence'][1]['choose'][0]['conditions'][0]['entity_id']. Got '{{ fan_speed_entity_id }}'
Entity {{ fan_speed_previous_entity_id }} is neither a valid entity ID nor a valid UUID for dictionary value @ data['sequence'][1]['choose'][0]['sequence'][0]['choose'][0]['conditions'][0]['entity_id']. Got '{{ fan_speed_previous_entity_id }}'

It feels like the script doesn’t allow the nested choose blocks to access the variables being passed.

Anyone has any clue on how to solve this issue?

Thank you

I think you would have to use a template condition to be able to have the entity as a variable.

@michaelblight is correct, State conditions do not accept templates. But, if you’re already passing entity names and variables, why not use them in a more systematic way instead of nesting Choose actions?

backyard_fan_set_speed:
  sequence:
    - variables:
        prev_speed: "{{ states[fan_speed_previous_entity_id] | int }}"
        command:  "{{ fan_command_speed_inc if percentage|int > prev_speed else fan_command_speed_dec }}"
        repeats: |-
          {% set sp_list = [33,66,100] %}
          {{ (sp_list.index(prev_speed) - sp_list.index(percentage|int))|abs }}
    - service: input_number.set_value
      target:
        entity_id: "{{ fan_speed_entity_id }}"
      data:
        value: "{{ percentage }}"
    - service: remote.send_command
      target:
        entity_id: remote.rm4_pro
      data:
        command: "{{ command }}"
        num_repeats: "{{repeats}}"
    - service: input_number.set_value
      target:
        entity_id: "{{ fan_speed_previous_entity_id }}"
      data:
        value: "{{ percentage }}"

You guys rock, this is an awesome solution. I had to make some minor adjustments and was able to drop the previous speed entity by leveraging the variables.

Here is the final version:

backyard_fan_set_speed:
  sequence:
    - variables:
        current_speed: "{{ states(fan_speed_entity_id) | int(0) }}"
        command: "{{ fan_command_speed_inc if percentage|int > current_speed else fan_command_speed_dec }}"
        repeats: |-
          {% set sp_list = [0, 33, 66, 100] %}
          {{ (sp_list.index(current_speed) - sp_list.index(percentage|int)) | abs }}
    - service: input_number.set_value
      target:
        entity_id: "{{ fan_speed_entity_id }}"
      data:
        value: "{{ percentage }}"
    - service: remote.send_command
      target:
        entity_id: remote.rm4_pro
      data:
        command: "{{ command }}"
        num_repeats: "{{repeats}}"

Appreciate the help.

1 Like