fixing randmonized vacation logic that was causing issues

This commit is contained in:
Jeffrey Stone 2022-02-21 19:22:07 -05:00
parent 9f4adae93f
commit 14bcf0c076
1 changed files with 24 additions and 27 deletions

View File

@ -102,6 +102,14 @@ randomize_vacation_lights:
entity_id: input_text.current_random_light
value: None
mode: single
# I have modified this from the verison on the video.
# Due to a logic issue I went with the homeassistant.toggle
# service so that it wontmatter if an entity is on or off, it will
# simply toggle it to the oppostite state.
# I also added a service call that would execute after the repeat section that
# turn off all the entities, for example when the sun is up, turn off all the lights
# in the vacation group
improved_randomize_vacation_lights:
alias: improved_randomize vacation lights
sequence:
@ -114,45 +122,34 @@ improved_randomize_vacation_lights:
- service: notify.mobile_app_jeffrey_ha_app
data:
message: Running Vacation Lights
- service: homeassistant.turn_on
data:
entity_id: '{{ expand(''group.vacation_lights'') | selectattr(''state'', ''eq'', ''off'') | map(attribute=''entity_id'') | list | random }}'
- delay:
minutes: '{{ range(1,3) | random | int }}'
- service: >
{% set service = ['homeassistant.turn_on', 'homeassistant.turn_off' ] | random %}
{{ service }}
- service: homeassistant.toggle
data:
entity_id: >
{% if service == 'homeassistant.turn_on' %}
{{ expand('group.vacation_lights') | selectattr('state', 'eq', 'off') | map(attribute='entity_id') | list | random }}
{% else %}
{{ expand('group.vacation_lights') | selectattr('state', 'eq', 'on') | map(attribute='entity_id') | list | random }}
{% endif %}
{{ expand('group.vacation_lights') | map(attribute='entity_id') | list | random }}
- delay:
minutes: '{{ range(1,3) | random | int }}'
# - service: group.turn_off
# entity_id: group.vacation_lights
mode: single
# Below is a demo script that can be used to test the effect. It uses a set count for repeat,
# and seconds for delay.
# It has also been updated to use the cleaner homeassistant.toggle service so we dont have to
# worry about which entities are on or off.
randomize_vacation_lights_demo:
alias: demo - randomize vacation lights
alias: randomize vacation lights - demo
sequence:
- repeat:
count: '8'
sequence:
- service: homeassistant.turn_on
data:
entity_id: '{{ expand(''group.vacation_lights'') | selectattr(''state'', ''eq'', ''off'') | map(attribute=''entity_id'') | list | random }}'
- delay:
seconds: '{{ range(2,5) | random | int }}'
- service: >
{% set service = ['homeassistant.turn_on', 'homeassistant.turn_off' ] | random %}
{{ service }}
- service: homeassistant.toggle
data:
entity_id: >
{% if service == 'homeassistant.turn_on' %}
{{ expand('group.vacation_lights') | selectattr('state', 'eq', 'off') | map(attribute='entity_id') | list | random }}
{% else %}
{{ expand('group.vacation_lights') | selectattr('state', 'eq', 'on') | map(attribute='entity_id') | list | random }}
{% endif %}
{{ expand('group.vacation_lights') | map(attribute='entity_id') | list | random }}
- delay:
seconds: '{{ range(2,5) | random | int }}'
- service: homeassistant.turn_off
entity_id: group.vacation_lights
mode: single
activate_arches_lamp:
alias: Activate Arches Lamp