I deleted everything from my automation folder and just put the following.
The lights do go on and off randomly. But as soon as I put the condition in for the alarm re-boot the system then arm the system it stops working. So it’s like it doesn’t see the state of the alarm. But from my overview screen I see the alarm is armed in away mode.
I don’t understand any more. could it be armed away is not the right wording for my DSC Alarm? but from the screen shot it does say armed away. The screen shot does say armed 24 seconds ago just because I re-armed for this post. But tried earlier and left it for 10 minutes.
Without the alarm condition it was changing every 2 minutes
################################################################
## Turn on/off indoor lights randomly while night and armed away
################################################################
- alias: Random Away Lights
initial_state: True
hide_entity: False
trigger:
- platform: time
minutes: '/1'
seconds: 00
condition:
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: 'armed_away'
action:
- delay: '00:{{ range(1,2) | random | int }}:00'
- service: homeassistant.toggle
data_template:
entity_id: "{{ state_attr('group.lights','entity_id') | random }}"
Ok I think I figured part of it out.
In my configuration.yaml for the alarm partition I called it my house but
entity_id: alarm_control_panel.ha_alarm calls for ha_alarm. When changing it seems to work. I will try more tomorrow
Eveything was working great now I was asked to update Home assistant and I am now getting this error if doing a check configuration
Invalid config for [automation]: [minutes] is an invalid option for [automation]. Check: automation->trigger->0->minutes. (See /config/configuration.yaml, line 68). Please check the docs at https://home-assistant.io/components/automation/
If I delete this part of the automation I get a valid configuration.
Did the update not like something in the following?
EDIT: If I remove line 68 from configuration file
automation: !include automations.yaml
Then it says configuration valid. But automatons from overview dissapeared and it doesn’t seem to work
################################################################
## Turn on/off indoor lights randomly while night and armed away
################################################################
- alias: Random Away Lights
initial_state: True
hide_entity: False
trigger:
- platform: time
minutes: '/20'
seconds: 00
condition:
- condition: state
entity_id: alarm_control_panel.my_house
state: 'armed_away'
- condition: sun
after: sunset
action:
- delay: '00:{{ range(05,11) | random | int }}:00'
- service: homeassistant.toggle
data_template:
entity_id: "{{ state_attr('group.all_lights','entity_id') | random }}"
loving this more and more my random light is working perfectly. Although there is one thing I want to change but can’t see anywhere how to do is or looking at the code where the setting is.
If I understand correctly the minutes: /20 tells it to start after 20 minutes after being armed.
Then the range(5,11) it will randomly switch every 5 to 11 minutes.
When arming system and it is after sunset the lights go randomly on but yesterday I noticed while I was away that almost all lights in my group were on. Can I limit the random feature to only 2 lights at a time?
Here is my code again
- alias: Random Away Lights
initial_state: true
hide_entity: false
trigger:
- platform: time_pattern
minutes: /20
seconds: 0
condition:
- condition: state
entity_id: alarm_control_panel.my_house
state: armed_away
- condition: sun
after: sunset
action:
- delay: 00:{{ range(5,11) | random | int }}:00
- service: homeassistant.toggle
data_template:
entity_id: '{{ state_attr(''group.alarm_lights'',''entity_id'') | random }}'
In other words, random will select one entity from all the entities listed in group.all_lights. However, you’re saying multiple lights are turned on? That implies random is selecting multiple entities. I don’t understand how it’s doing that.
data_template:
entity_id: '{{ state_attr(''group.alarm_lights'',''entity_id'') | random }}'
yes last night I noticed a light was switching on every 5-11 minutes give or take and none seem to close(but not 100% sure if any closed)
All I know is at a point out of 11 lights in the group 8 were on. My electrical company sent me a thank-you for your business letter hihi. Thank goodness they are all led.
When I got home it looked like there was a party in my house …
Thanks for the explanation. Now I think I understand how it works.
random is not selecting multiple entities, just one. Each time it randomly selects an entity, from group.alarm_lights, it toggles the entity’s state. The pattern can look something like this:
Assume there are 5 lights and all are initially OFF.
Light 1 -> ON
Light 5 -> ON
Light 3 -> ON
Light 5 -> OFF
Light 4 -> ON
Light 5 -> ON
Light 1 -> OFF
Light 4 -> OFF
Light 1 -> ON
Light 3 -> OFF
Light 2 -> ON
Light 3 -> ON
Question: At this end of this sequence of random toggling, how many lights are on?
Answer: Four of the five lights are ON. Lights 1, 2, 3, and 5.
If you want no more than 2 lights on at any time, the algorithm can no longer simply be based on random toggling (of a single entity). Ensuring a maximum of 2 lights are on, at any time, is a considerably more challenging algorithm to invent.
Frankly, the simplest solution is to just create a group of 2 or 3 lights and toggle them randomly. At most you will have 2 or 3 lights on.
thank-you for a great explanation.
Although not to clear on your suggestion. If I create example 3 groups with 3 lights in each and I add those as groups won’t it be the same thing? I can have example group 1 toggle on then it can toggle group 3 on . So I will still have 6 lights open.
Would the algorithm be easier if I just have one light at a time instead of 2? So random switches one off every time it switches one on?
My suggestion was to use the same algorithm but with a group containing only 2 or 3 lights. Basically, out of the 11 lights you have, pick 3 that you want to have randomly toggled and add them to a new group.
If you are willing to accept having only one out of 11 lights on at any time, then the existing algorithm can be used but with modifications.
First step is to turn off all lights in group.alarm_lights. This will ensure any light that may already be on is turned off. Ideally you only want to turn off the light that is on but that would take more code to achieve (but feel free to implement it if you wish).
Second step, is to use the existing algorithm that randomly toggles one of the 11 lights in group.alarm_lights.
action:
- delay: 00:{{ range(5,11) | random | int }}:00
- service: homeassistant.turn_off
data:
entity_id: 'group.alarm_lights'
- service: homeassistant.toggle
data_template:
entity_id: '{{ state_attr(''group.alarm_lights'',''entity_id'') | random }}'
Ok I will try that. I think I understand the logic please correct me if I am wrong so I will learn from this experience.
Previous code randomly opened a light every 5-11 minutes but could have but not neccessairly close lights.
This code every 5-11 minutes it will close all lights in group and then randomly open a light in the group.
thank-you Petro
I tried yours
I put the first part in the automatons yaml
and the second part in the script yaml without the word script or else I get an error when doing a configuration validation.
I changed the timing to
minutes: /1
and the
delay: 00:{{ range(1,2) | random | int }}:00
did this just to test and not wait all night
at first nothing was happening when armed then I tried opening three lights manually after waiting about 10 minutes.
It randomly closed the lights one at a time every few minutes but no light was ever turned on. Kept it going for about an hour.
So it seems the random off trigger is working but not the random on
this is exactly what I was looking for
only thing I would like to add is more groups. would i need an extra script for that. or could it be done with the same script, adding a service. They should be toggling the lights in 2 groups, at the same time.
Or maybe I need 2 almost identical automations, each for a single group triggered by the same trigger and condition?
first, testing it in one condition calling the script twice…
automation:
- alias: Home mode locked lights
id: Home mode locked lights
# initial_state: 'on'
trigger:
platform: time #_pattern
minutes: /20
seconds: 0
condition:
# - condition: state
# entity_id: input_boolean.home_mode_locked
# state: 'on'
condition: sun
after: sunset
after_offset: '-01:00:00'
action:
- delay: 00:{{ range(5,11) | random | int }}:00
- service: script.home_mode_locked_lights
data_template:
random_group: group.home_mode_locked_lights_1
random_entity_id: >
{{ state_attr('group.home_mode_locked_lights_1','entity_id') | random }}
light_count_limit: 4
- service: script.home_mode_locked_lights
data_template:
random_group: group.home_mode_locked_lights_2
random_entity_id: >
{{ state_attr('group.home_mode_locked_lights_2','entity_id') | random }}
light_count_limit: 3
- condition: template
value_template: >
{{is_state('input_boolean.notify_system','on')}}
- service: notify.notify
data_template:
message: >
{{as_timestamp(now()) | timestamp_custom('%X') }}: House in locked mode and randomly lit to
mock occupancy... }}
script:
home_mode_locked_lights:
sequence:
- condition: template
value_template: >
{%- set count = states | selectattr('entity_id','in',state_attr(random_group,'entity_id'))
| selectattr('state','eq','on') | list | length %}
{{ is_state(random_entity_id, 'on') or
(is_state(random_entity_id, 'off') and count < light_count_limit) }}
- service: homeassistant.toggle
data_template:
entity_id: >
{{ random_entity_id }}
was a bit annoyed by the fact that the original automation triggers every 20 minutes, no matter what. Seems a waste of resources. Might not come into action, but it is listening…
So, I trigger it (turn it on) from an external script, (based on the state of the inout_boolean, and stopping it again when that inout_boolean.home_mode_locked is Off. That way it will only run each 20 minutes when necessary…
hence the initial: ‘off’.
edit
add the time condition (might be time_pattern on newer HA)
- condition: time #pattern
after: '17:00:00'
before: '01:30:00'
Hi this is the code I got I armed the sytem
turned on lights added the code you asked but it is not sunset yet so not sure if it is ok. Or else I can re-do later
<generator object do_map at 0x6da59db0>
<generator object do_map at 0x6da59120>
2
well the good news is that is working so the only thing it can be is that it’s randomly always choosing the lights that are on. Try reducing the random time to a shorter period for testing. Lets say, 30 seconds or so.