Set_value for several types

Hi,

I often write a entity state (for example selection from a input_select) to a other input entity. To not create always a separate script I try a little universal script but not sure if this is a good option to do so and my script is ok:

script_universal_setinputvalue:
  alias: 'Universal - Entity Zustand in Input Entity speichern'
  mode: parallel
  max: 10
  max_exceeded: silent
  fields:
    source_entity_id:
      name: Source Entity
      description: Entity der Quelle.
      example: 'input_select.is_sonosfavs'
      required: true
      selector:
        entity:
    dest_entity_id:
      name: Desitination Entity
      description: Entity in dem gespeichert werden soll.
      example: 'input_text.it_sonosfavcurrent_allgemein'
      required: true
      selector:
        entity:
  sequence:
    - choose:
        - conditions:
            - condition: template
              value_template: >-
                {{ dest_entity_id.startswith('input_text') }}
          sequence:
          - service: input_text.set_value
            target:
              entity_id: '{{ dest_entity_id }}'
            data_template:
              value: >-
                {{- states(source_entity_id) -}}
        - conditions:
            - condition: template
              value_template: >-
                {{ dest_entity_id.startswith('input_number') }}
          sequence:
          - service: input_number.set_value
            target:
              entity_id: '{{ dest_entity_id }}'
            data_template:
              value: >-
                {{- states(source_entity_id) -}}
        - conditions:
            - condition: template
              value_template: >-
                {{ dest_entity_id.startswith('input_boolean') }}
          sequence:
          - service: input_boolean.set_value
            target:
              entity_id: '{{ dest_entity_id }}'
            data_template:
              value: >-
                {{- states(source_entity_id) -}}

Thanks for any support.
Steffen

Input boolean use turn_on/turn_off service calls, not set_value.

script_universal_setinputvalue:
  alias: 'Universal - Entity Zustand in Input Entity speichern'
  mode: parallel
  max: 10
  max_exceeded: silent
  fields:
    source_entity_id:
      name: Source Entity
      description: Entity der Quelle.
      example: 'input_select.is_sonosfavs'
      required: true
      selector:
        entity:
    dest_entity_id:
      name: Desitination Entity
      description: Entity in dem gespeichert werden soll.
      example: 'input_text.it_sonosfavcurrent_allgemein'
      required: true
      selector:
        entity:
  sequence:
    - variables:
        domain: "{{ dest_entity_id.split('.')[0] }}"
        source_state: "{{ states(source_entity_id) }}"
    - choose:
        - conditions:
            - condition: template
              value_template: "{{ domain in ['input_text', 'input_number'] }}"
          sequence:
            - service: "{{ domain}}.set_value"
              target:
                entity_id: "{{ dest_entity_id }}"
              data:
                value: "{{- source_state -}}"
        - conditions:
            - condition: template
              value_template: "{{ domain == 'input_boolean' }}"
          sequence:
            - service: >
                input_boolean.turn_{{ iif(source_state in [on','an','ein','true','1'], 'on', 'off) }}
              target:
                entity_id: '{{ dest_entity_id }}'
      default:
        - service: persistent_notification.create
          data:
            message: >
              A {{domain}} helper has been set as destination for script.script_universal_setinputvalue. 
              No action exists to handle this helper type.
            title: Script Error

Thank you, now the script looks more professional. I think about the input_boolean to translate my use cases in turn_on/turn_off.

Steffen

Hi,

I think you mistaken source and destination at the domain variable. Not source but destination gives the service. So I changed some thinks and get this:

script_universal_setinputvalue:
  alias: 'Universal - Entity Zustand in Input Entity speichern'
  mode: parallel
  max: 10
  max_exceeded: silent
  fields:
    source_entity_id:
      name: Source Entity
      description: Entity der Quelle.
      example: 'input_select.is_sonosfavs'
      required: true
      selector:
        entity:
    dest_entity_id:
      name: Desitination Entity
      description: Entity in dem gespeichert werden soll.
      example: 'input_text.it_sonosfavcurrent_allgemein'
      required: true
      selector:
        entity:
  sequence:
    - variables:
        dest_domain: >-
          {{- dest_entity_id.split('.')[0] -}}
        source_state: >-
          {% set value = states(source_entity_id) | string %}
          {% if dest_entity_id.split('.')[0] == 'input_boolean' %}
            {% if value in ['on','an','ein','true','1'] %}
              {% set value = 'on' %}
            {% else %}
              {% set value = 'off' %}
            {% endif %}
          {% endif %}
          {{- value -}}
    - choose:
        - conditions:
            - condition: template
              value_template: >-
                {{ dest_domain in ['input_text', 'input_number'] }}
          sequence:
            - service: '{{ dest_domain}}.set_value'
              target:
                entity_id: '{{ dest_entity_id }}'
              data:
                value: >-
                  {{- source_state -}}
        - conditions:
            - condition: template
              value_template: >-
                {{ dest_domain == 'input_boolean' }}
          sequence:
            - service: 'input_boolean.turn_{{ source_state }}'
              target:
                entity_id: '{{ dest_entity_id }}'

Happy to get your comments learning some advanced script technical.

Steffen

Right, it is the destination entity’s domain that determines which service needs to be used.

You have added some unnecessary complications to the template for source_state:

  1. {% set value = states(source_entity_id) | string %}
    States are always strings, so there is no need to “convert” them.

  2. {% if dest_entity_id.split('.')[0] == 'input_boolean' %}
    There’s no reason to re-render the domain value:
    {% if domain == 'input_boolean' %}

EDIT: Removed comment about translations. See Below.

No, it is the destination entity’s domain that determines which service needs to be used.

Thank you, I have seen that you corrected your version and edit the input. Now it works perfect for my use case and I remove the unnecessary parts.

Actually, I just realized that I made the same assumption/error regarding your translations…

If you use a non-boolean source to set a boolean, you will need the translations… I would add them in the service as follows:

sequence:
  - service: >
      input_boolean.turn_{{ iif(source_state in [on','an','ein','true','1'], 'on', 'off) }}
    target:
      entity_id: '{{ dest_entity_id }}'

Also, you may want to define a default for your Choose action in case you select a destination entity that isn’t covered. Something like:

      default:
        - service: persistent_notification.create
          data:
            message: >
              A {{domain}} helper has been set as destination for
              script.script_universal_setinputvalue. 
              No action exists to handle this helper type.
            title: Script Error
1 Like