Vacation Lighting - Replay Historical Lighting

It depends on how long you have the history saved in your recorder.

If you are trying to look back 7 days but you only have 6 days saved it won’t work.

so if 6 worked but 7 didn’t before then it probably won’t this time either unless you change how long your recorder retains data.

Hey, maybe I’m just crazy, but I’ve been having issues with multiple lights that may be turned on/off at exactly the same time not re-triggering this blueprint.

See error below from Trace Timeline:

Stopped because only a single execution is allowed at July 19, 2021, 10:06:20 PM (runtime: 0.00 seconds)

I looked really hard, but couldn’t find any documentation regarding how HA handles automation modes within blueprints.

On this page of the documentation, an automation mode appears to be written into the blueprint YAML, so I tried adding one to this blueprint in the same manner to see if it would solve my problem of the blueprint only allowing a single run at a time and ignoring concurrent re-triggers.

Here is how I added it in (right before triggers):

blueprint:
  name: Blueprint - Vacation Lighting 
  description: Vacation Lighting - Replay Sensors to Light Behavior
  domain: automation
  input:
    vacation_mode_toggle:
      name: Vacation Mode - Input Boolean
      selector:
        entity:
          domain: input_boolean
    default_brightness:
      name: Default Brightness
      default: 50
      selector:
        number:
          min: 1
          max: 100
          unit_of_measurement: "%"
          mode: slider
    replay_sensor_1:
      name: Replay Sensor (1)
      selector:
        entity:
          domain: sensor
    light_target_1:
      name: Light Target (1)
      selector:
        entity:
    replay_sensor_2:
      name: Replay Sensor (2)
      selector:
        entity:
          domain: sensor
    light_target_2:
      name: Light Target (2)
      selector:
        entity:
    replay_sensor_3:
      name: Replay Sensor (3)
      selector:
        entity:
          domain: sensor
    light_target_3:
      name: Light Target (3)
      selector:
        entity:
    replay_sensor_4:
      name: Replay Sensor (4)
      selector:
        entity:
          domain: sensor
    light_target_4:
      name: Light Target (4)
      selector:
        entity:
    replay_sensor_5:
      name: Replay Sensor (5)
      selector:
        entity:
          domain: sensor
    light_target_5:
      name: Light Target (5)
      selector:
        entity:
    replay_sensor_6:
      name: Replay Sensor (6)
      selector:
        entity:
          domain: sensor
    light_target_6:
      name: Light Target (6)
      selector:
        entity:
    replay_sensor_7:
      name: Replay Sensor (7)
      selector:
        entity:
          domain: sensor
    light_target_7:
      name: Light Target (7)
      selector:
        entity:
    replay_sensor_8:
      name: Replay Sensor (8)
      selector:
        entity:
          domain: sensor
    light_target_8:
      name: Light Target (8)
      selector:
        entity:
    replay_sensor_9:
      name: Replay Sensor (9)
      selector:
        entity:
          domain: sensor
    light_target_9:
      name: Light Target (9)
      selector:
        entity:
    replay_sensor_10:
      name: Replay Sensor (10)
      selector:
        entity:
          domain: sensor
    light_target_10:
      name: Light Target (10)
      selector:
        entity:
# Declare blueprint inputs as variables for use in {{templates}}        
variables:
  replay_sensor_1: !input replay_sensor_1
  light_target_1: !input light_target_1
  replay_sensor_2: !input replay_sensor_2
  light_target_2: !input light_target_2
  replay_sensor_3: !input replay_sensor_3
  light_target_3: !input light_target_3
  replay_sensor_4: !input replay_sensor_4
  light_target_4: !input light_target_4
  replay_sensor_5: !input replay_sensor_5
  light_target_5: !input light_target_5
  replay_sensor_6: !input replay_sensor_6
  light_target_6: !input light_target_6
  replay_sensor_7: !input replay_sensor_7
  light_target_7: !input light_target_7
  replay_sensor_8: !input replay_sensor_8
  light_target_8: !input light_target_8
  replay_sensor_9: !input replay_sensor_9
  light_target_9: !input light_target_9
  replay_sensor_10: !input replay_sensor_10
  light_target_10: !input light_target_10

############ ADDED AN AUTOMATION MODE HERE #############
mode: parallel

# Trigger when replay sensor changes state
trigger:
  - platform: state
    entity_id: !input replay_sensor_1
  - platform: state
    entity_id: !input replay_sensor_2
  - platform: state
    entity_id: !input replay_sensor_3
  - platform: state
    entity_id: !input replay_sensor_4
  - platform: state
    entity_id: !input replay_sensor_5
  - platform: state
    entity_id: !input replay_sensor_6
  - platform: state
    entity_id: !input replay_sensor_7
  - platform: state
    entity_id: !input replay_sensor_8
  - platform: state
    entity_id: !input replay_sensor_9
  - platform: state
    entity_id: !input replay_sensor_10

# As long as Vacation Mode is on
condition:
  - condition: state
    entity_id: !input vacation_mode_toggle
    state: 'on'


action:
  - variables:
      corresponding_light: >
        {% if trigger.entity_id == replay_sensor_1 %}
          {{ light_target_1 }}
        {% elif trigger.entity_id == replay_sensor_2 %}
          {{ light_target_2 }}
        {% elif trigger.entity_id == replay_sensor_3 %}
          {{ light_target_3 }}
        {% elif trigger.entity_id == replay_sensor_4 %}
          {{ light_target_4 }}
        {% elif trigger.entity_id == replay_sensor_5 %}
          {{ light_target_5 }}
        {% elif trigger.entity_id == replay_sensor_6 %}
          {{ light_target_6 }}
        {% elif trigger.entity_id == replay_sensor_7 %}
          {{ light_target_7 }}
        {% elif trigger.entity_id == replay_sensor_8 %}
          {{ light_target_8 }}
        {% elif trigger.entity_id == replay_sensor_9 %}
          {{ light_target_9 }}
        {% elif trigger.entity_id == replay_sensor_10 %}
          {{ light_target_10 }}
        {% endif %}
  - choose:
      # Replay turned on && entity_id is "LIGHT"
      - conditions:
          condition: and
          conditions: 
            - condition: template
              value_template: '{{ trigger.to_state.state | int >= 1 }}'
            - condition: template
              value_template: '{{ corresponding_light.split(".")[0] == "light" }}'
        sequence:
          - service: light.turn_on
            data:
              entity_id: '{{ corresponding_light }}'
              brightness_pct: !input default_brightness
          - service: system_log.write
            data:
              message: 'Vacation - Replay Lighting (Blueprint): {{trigger.to_state.entity_id}}: turning on: {{ corresponding_light }}'
              level: info

      # Replay turned on && entity_id is "SWITCH"
      - conditions:
          condition: and
          conditions: 
            - condition: template
              value_template: '{{ trigger.to_state.state | int >= 1 }}'
            - condition: template
              value_template: '{{ corresponding_light.split(".")[0] == "switch" }}'
        sequence:
          - service: switch.turn_on
            data:
              entity_id: '{{ corresponding_light }}'
          - service: system_log.write
            data:
              message: 'Vacation - Replay Lighting (Blueprint): {{trigger.to_state.entity_id}}: turning on:{{ corresponding_light }}'
              level: info

      # Replay turned off
      - conditions:
          condition: and
          conditions: 
            - condition: template
              value_template: '{{ trigger.to_state.state == "0" }}'
        sequence:
          - service: homeassistant.turn_off
            data:
              entity_id: '{{ corresponding_light }}'
          - service: system_log.write
            data:
              message: 'Vacation - Replay Lighting (Blueprint): {{trigger.to_state.entity_id}}: turning off:{{ corresponding_light }}'
              level: info

It seems to have worked so far. Has anyone else had a similar issue? Or any experience using automation modes in blueprints?

Interesting, I haven’t thought about that as a use case scenario. Most of my lights are just user controlled by my family so pretty low probability of that ocurring. If someone can confirm this is the appropriate solution I’m happy to add this into the parent post!

2 Likes

Ok, I have finally found in the documentation what I feel confident is confirmation that this is the appropriate way to add modes into an automation.

In the Blueprint Schema documentation, it says, “. . . in the case of creating a blueprint for an automation, the full schema for an automation applies.”

So, if you look at how automation modes are implemented in an automation, this would be the correct level to add modes in a blueprint.

Once again, this is the only blueprint example I could find in the documentation that uses an automation mode, but this is how it is implemented.

In addition, I was out of town the beginning of this week, and once I added:

mode: parallel

to the blueprint, it worked as expected when multiple triggers fired at once.

2 Likes

Hi, trying to add this blueprint but keep getting the below error when pasting the url in or trying to import with my blueprints?
image

Thanks @davearneson ! I’ve added it to the original post

@Eldudemeister this was covered above. in short, HA looks for the first code block to import. this won’t work for this post (and other posts as well) so you’ll need to manually create the blueprint. The other configurations for the replay sensors and recorder would need to be manually coded into your configuration as well.

1 Like

Ah sorry I missed that, makes sense though, will give it a try!

1 Like

Hi @timgiwo ,

Great Idea!
I also tried to play with it once on one specific light but did not move further to generalizing through blueprints.

Thanks for this Blueprint that makes it easier to maintain :slight_smile:

One thing I am thinking of a way to make it even more simple not needing specify which lights to apply to and have to make an automation for each 10 lights.

But this will require the ability to loop/iterate through all light entities. Is that possible at all?

If possible, then no risk on looping through all lights and will only work on those where recording is enabled/configured. But not necessarily all switches may be includes. Therefor we will need to indicate which ones to apply to and can do that adding the entity names to some kind of a list.

If this is possible the only thing users need to do:

  • Enable recording for entities
  • Add selected switches on a list

Then In fact, there will be no need for a blue print but one generic Automation that handles the whole work without hardcoding any entity names.

What do you think?

Any body have experience with looping on a set of entities?

Thanks :slight_smile:

It’s a good idea, but I’m not sure of there’s a straight forward way to interact with the home assistant database directly from an automation without the use of more sensors. Would love to see others implementation of the idea if possible!

Seems possible, look at the following example
Switch everything off except excluded entities - Share your Projects! - Home Assistant Community (home-assistant.io)

It includes most of the needed code. We just need, in stead of always turning off, to set to state from recording.

What do you think?

How can I test this without waiting a week?
Can I see what has been stored for each for each entity?

You can change the statistical sensor specifying another amount of time to go back and match.as in the following example :

##########
# Sensors
##########
sensor:
  # Replay Sensor - Make a new one for each light to monitor
  # All sensors will have naming convention "sensor.replay_xxxxx"
  - platform: history_stats
    name: "replay_office_lamp"
    entity_id: light.office_lamp
    state: "on"
    type: count
    start: >
      {{ as_timestamp(now()) - (7*24*60*60) }}
    duration: 00:00:30

The figures 7*24*60*60 means 60 second x 60 minutes x 24 hours x 7 days

You can change it to be 3 hours like this 3*60*60 or 2 days like this 2*24*60*60 and so on,
Hope this makes sense otherwise let me know and I’ll try to explain further.

I’d like to control more than 10 devices. Twelve is what I have in mind. Is controlling such a number possible simply by adding code to the Blueprint to include;

**Add new sensors and targets**
 replay_sensor_11:
      name: Replay Sensor (11)
      selector:
        entity:
          domain: sensor
    light_target_12:
      name: Light Target (12)
      selector:
        entity:

**Declaring blueprint inputs and variables**
 replay_sensor_11: !input replay_sensor_11
  light_target_11: !input light_target_11
  replay_sensor_12: !input replay_sensor_12
  light_target_12: !input light_target_12

**Adding additional trigger's**
  - platform: state
    entity_id: !input replay_sensor_11
  - platform: state
    entity_id: !input replay_sensor_12

**Adding additional actions**
        {% elif trigger.entity_id == replay_sensor_11 %}
          {{ light_target_11 }}
        {% elif trigger.entity_id == replay_sensor_12 %}
          {{ light_target_12 }}

**Then add two Replay sensors**

Is this the correct approach or is there another way to add two additional device?

Update: problem solved. I added the code and sensors mentioned above, which resulted in exactly what I wanted. The updated blueprint works wonderfully.

2 Likes

I am unable to import this blueprint, and instead get the error message: No valid blueprint found in the topic. Blueprint syntax blocks need to be marked as YAML or no syntax.

2 Likes

Read my posts above. Basically, you need to add it manually.

Thanks, that helped. I had tried adding it manually, but stupidly forgot to name it explicitly as a yaml file.

Why did adding blueprints from the forum stop working? Do they now need to be on GitHub? Quite unhandy to manually add a blueprint using the smartphone… this is not working as intended, right?

The error message I got suggests a problem with this particular page, and how the yaml file is named. I just tried another blueprint, and it imported fine.

Hi everyone,

I made another take that might be easier to configure and maintain.
It is just one automation, no need for blueprints.
Take a look at this:

Let me know if I can help you getting to top work :slight_smile:

Hi Everyone.

This is exactly what I’ve been looking for. I just have a few questions:

  1. do I have to set the entity record time manually to 7 days in my config.yaml?
  2. once I’ve completed the all the steps, should I see an automation called Vacation Mode in on the automation page. currently I can see the code added to automation.yaml, but nothing new is showing up on the automations page.

its a bit difficult to work out if this automation is actually running and working. :expressionless:

regards

Sups

1 Like