Hi,
I have setup a script sequence that reads a MQTT text message, converts it to an array and switches a total of 12 power ports according to status ON or OFF:
The complete YAML script looks like this:
alias: Update PDU States via MQTT 3
sequence:
- service: mqtt.publish
data:
topic: de/gudesystems/epc/00:19:32:01:79:ee/cmd
payload: "{\"type\": \"cli\", \"cmd\": \"port all state 1 show\"}"
- delay:
hours: 0
minutes: 0
seconds: 5
milliseconds: 0
- alias: MQTT Text to Array
variables:
pdu_states: "{{ states('text.pdu_port_states').split(',') }}"
- if:
- condition: template
value_template: |
{% if pdu_states[0][-2:] == "FF" %}
true
{% else %}
false
{% endif %}
then:
- service: switch.turn_off
target:
entity_id: switch.pdu_port_1_switch
data: {}
else:
- if:
- condition: template
value_template: |
{% if pdu_states[0][-2:] == "ON" %}
true
{% else %}
false
{% endif %}
then:
- service: switch.turn_on
target:
entity_id: switch.pdu_port_1_switch
data: {}
mode: single
icon: mdi:power-socket
Now I basically need to repeat this part 11 times for all 12 ports and with each repetition I need to count one up for “pdu_states[0 +1 ][-2:]” and “switch.pdu_port_1 +1 _switch”:
- if:
- condition: template
value_template: |
{% if pdu_states[0][-2:] == "FF" %}
true
{% else %}
false
{% endif %}
then:
- service: switch.turn_off
target:
entity_id: switch.pdu_port_1_switch
data: {}
else:
- if:
- condition: template
value_template: |
{% if pdu_states[0][-2:] == "ON" %}
true
{% else %}
false
{% endif %}
then:
- service: switch.turn_on
target:
entity_id: switch.pdu_port_1_switch
data: {}
How to do that elegantly?