Pass Variable to Script - Use in Condition

Trying to avoid building dozens of unique scripts and instead pass a variable to a script.

Current Script:

htd_update_sensor:
  sequence:
  - service: system_log.write
    data_template:
      message: "UPDATE SENSOR ON ZONE: {{ zone }} - switch.htd_pwr_zone{{ zone }}"
      level: info  
  - condition: template
    value_template: >-
      {% set device = 'switch.htd_pwr_zone{{ zone }}' %}
      {{ is_state( device, 'off') }}
  - service: homeassistant.update_entity
    data_template: 
      entity_id: >-
        {% set prefix = 'sensor.htd_sensor_zone' %}
        {{ prefix }}{{ zone }}

Testing by calling script in Developer Tools -> Services
image

The log write is showing the correct ‘device’ value `switch.htd_pwr_zone1’, the switch state = off:
image
but the condition is FAILING in the logs:
image

Running this in template editor (hard coding device value) shows True
image

Thoughts? Thx in advance!

1 Like

@Markus99
Did you manage to figure out what the problem is with your script.
I am trying to achieve somewhat the same and I am stuck at the same point.

Someone updated that custom_component to work, so didn’t end up using that code. What are you trying to accomplish more specificially @matthijsv93?

I am trying to make an automation that sends a variable with the groupname of a group of lights when a wall switch is pressed. A script reads the variable containing the groupname and switches the corresponding lights off and on with different brighness based on the time of the day.

The automation:

- alias: 'Wandschakelaar Woonkamer 1'
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: schakelaar_1
      event: 2002 
  action:
    service: script.light_toggle
    data:
      group: 'keuken'

the script:

light_toggle:
  alias: 'schakel groepen'
  sequence:
  - choose:
      - conditions:
          - condition: and
            conditions:
            - condition: template
              value_template: >-
                {% set device = 'group.{{ group }}' %}
                {{ state(device, 'off') }}
            - condition: sun
              before: sunset
              after: sunrise
              before_offset: '0'
              after_offset: '0'
        sequence:
          - service: light.turn_on
            data:
              entity_id: 'group.{{ group }}'
              brightness: 180
              transition: 1
      - conditions:
          - condition: and
            conditions:
            - condition: template
              value_template: >-
                {% set device = 'group.{{ group }}' %}
                {{ state(device, 'off') }}
            - condition: sun
              before: sunrise
              after: sunset
              before_offset: '0'
              after_offset: '0'
        sequence:
          - service: light.turn_on
            data:
              entity_id: 'group.{{ group }}'
              brightness: 100
              transition: 1
    default:
      - service: light.turn_on
        data:
          entity_id: 'group.{{ group }}'
          brightness: 0
          transition: 1
  mode: single

I think the problem is with the use of a variable within the condition template
@Markus99

1 Like