Hi all,
i bought a HLK-SW16 relay module and i configured it into HA.
I want to create two automation/script to start a loop where
an automation turn on one of 8 relay for 5 seconds in a random selection.
an automation that rotate between 64 combination of on/off state (2^8 possibilities) for 5 seconds
From HA i will decide manually with a switch when start the automation
Someone have done something of similar or give me an hints to start?
trigger:
platform: time_pattern
# You can also match on interval. This will match every 5 minutes
minutes: '/5'
action:
- service: switch.turn_on
data_template:
entity_id: >
{%- macro getrelays() %}
{%- set relay = 'switch.relay1_' %}
{%- set flags = '{:08b}'.format(range(256) | random) | list %}
{%- for i in range(flags | length) %}
{{- relay+'{} '.format(i) if flags[i] == '1' else '' }}
{%- endfor %}
{%- endmacro %}
{{ getrelays().strip().split(' ') | join(', ') }}
Then just turn on and off this automation.
EDIT: I Just realized yours goes all the way to 8 from 0… that would be more than 64 combos. That’s 511 combinations… so you’d want to change this line to account for that.
{%- set flags = '{:09b}'.format(range(511) | random) | list %}
^ ^
EDIT2: I made a simple one where you just change the inputs
trigger:
platform: time_pattern
# You can also match on interval. This will match every 5 minutes
minutes: '/5'
action:
- service: switch.turn_on
data_template:
entity_id: >
{%- set relay_count = 9 %}
{%- set relay = 'switch.relay1_' %}
{%- macro getrelays() %}
{%- set bin = '1' * relay_count %}
{%- set rng = bin | int(bin, 2) %}
{%- set fmat = '{'+':0{}b'.format(relay_count)+'}' %}
{%- set flags = fmat.format(range(rng) | random) | list %}
{%- for i in range(flags | length) %}
{{- relay+'{} '.format(i) if flags[i] == '1' else '' }}
{%- endfor %}
{%- endmacro %}
{{ getrelays().strip().split(' ') | join(', ') }}