kolia
August 10, 2022, 7:24pm
1
Hello,
This code works fine
- id: aeea0bd8-e977-4aa4-a77d-1401b2d259f3
alias: XMC-1 SFX volumes DZ to HA
description: ""
variables:
indexes:
612: input_number.idx612
613: input_number.idx613
614: input_number.idx614
615: input_number.idx615
entity: "{{ indexes.get(trigger.topic.split('/') | last | int(0), 'unknown') }}"
trigger:
platform: mqtt
topic: domoticz/out/#
condition: "{{ entity != 'unknown' }}"
action:
service: input_number.set_value
data:
entity_id: "{{ entity }}"
value: '{{ ((trigger.payload_json.svalue1 | int / 100 * 32) - 16) | round(0) }}'
However I’d like to use the same code for another index (619) for which the value computation is somewhat different.
I tried many flavors without success so far, like:
value: >
{% if {{ entity }} == 'input_number.idx619' %}
'{{ ((trigger.payload_json.svalue1 | int / 100 * 75) - 70) | round(0) }}'
{% else %}
'{{ ((trigger.payload_json.svalue1 | int / 100 * 32) - 16) | round(0) }}'
{% endif %}
or
value: >
"{{ iif ( {{ entity }} == 'input_number.idx619',
'{{ ((trigger.payload_json.svalue1 | int / 100 * 32) - 16) | round(0) }}',
'{{ ((trigger.payload_json.svalue1 | int / 100 * 32) - 16) | round(0) }} ) }}'
Can I achieve what I’m trying to do?
Thank you
You can’t nest templates like {% {{ }} %}
or {{ {{ }} }}
, just use the variable as is…
- id: aeea0bd8-e977-4aa4-a77d-1401b2d259f3
alias: XMC-1 SFX volumes DZ to HA
description: ""
variables:
indexes:
612: input_number.idx612
613: input_number.idx613
614: input_number.idx614
615: input_number.idx615
619: input_number.idx619
entity: "{{ indexes.get(trigger.topic.split('/') | last | int(0), 'unknown') }}"
trigger:
platform: mqtt
topic: domoticz/out/#
condition: "{{ entity != 'unknown' }}"
action:
service: input_number.set_value
data:
entity_id: "{{ entity }}"
value: >
{% set v_pct = trigger.payload_json.svalue1 | int / 100 %}
{{ iif ( entity == 'input_number.idx619', ((v_pct * 75) - 70), ((v_pct * 32) - 16) ) | round(0) }}
kolia
August 11, 2022, 12:48pm
3
Thank you very much for your help
123
(Taras)
August 11, 2022, 1:15pm
4
If you are interested, here’s another way to do the same thing.
- id: aeea0bd8-e977-4aa4-a77d-1401b2d259f3
alias: XMC-1 SFX volumes DZ to HA
description: ""
variables:
i: "{{ trigger.topic.split('/') | last | int(0) }}"
index: "{{ i if i in [612, 613, 614, 615, 619] else 'unknown' }}"
a: "{{ 32 if index != 619 else 75 }}"
b: "{{ 16 if index != 619 else 70 }}"
trigger:
platform: mqtt
topic: domoticz/out/#
condition: "{{ index | is_number }}"
action:
service: input_number.set_value
data:
entity_id: "input_number.idx{{ index }}"
value: "{{ ((trigger.payload_json.svalue1 | int(0) / 100) * a - b) | round(0) }}"
kolia
August 11, 2022, 5:15pm
5
Thanks you. It is always very interesting to discover new techniques. It helps a lot.