Change light color and revert based on door open sensor

Hi there!

I have a use case which, until getting started with Home Assistant, I was able to somewhat manage with a mix of Ikea Home Smart and Google Home. I’d like of course to make things tidier and more robust with Home Assistant, but I’m not sure how.

I have a door sensor installed on a freezer. I also have a color bulb in another room. That color bulb lights up white as part of a “sunset” automation. What I want to do is:

-If the freezer is opened for less than 10 minutes, do nothing
-If the freezer is opened for 10 minutes or more (it’s been forgotten), turn on the light if off and turn its color to red
-When the freezer door is finally closed, revert the previous state (white if it was already turned on, white then off if it was previously off).

I’m not completely comfortable using the concept of “if”, and I’m absolutely clueless about the concept of reverting to a previous state. In short, I have no clue how to achieve this, but I’m sure it’s possible.

Thanks in advance for any input!

Hello bdery,

Using the automation editor it will help somewhat step you thru making an automation for this. I’m sure that can be done. Also having someone or something writing it for you means the second it misbehaves, you will be struggling back to find someone to fix it for you.
The best way to learn is to just try. Run thru the docs on getting started with the automation editor. If you get stuck, come back with what you have done.
Understanding automations - Home Assistant and
The Home Assistant Cookbook - Index.

Also I have a Blueprint that does most of what you want. That might work for you. You might have to ‘take control’ of it, then change some of the actions to control lights.

What is said above is true, but I guess some pointers will help you get underway.

  • A state trigger helps you act when something changes state (the freezer door)
  • You can put a “for” statement on the state condition, so it will trigger when the state is that way for the expected time (the 10 minutes the freezer door must be open for the automation to trigger).
  • You want to remember the state of something. Scenes are there for this purpose. You can add an action to save one or more states named scene.create to remember the current light state.
  • You do not need to check the state of the light. If you want it on in red, use the action light.turn_on. It allows you to specify the color and it does not matter what it was before.
  • You can use a wait_for_trigger action to wait for the door to close. (You should by now know what trigger is useful for that). Alternatively, you can create a separate automation for that.
  • And when it closes, use the action scene.turn_on to activate the scene that you used to capture the previous state of the light.

Thank you @Edwin_D and @Sir_Goodenough . You are correct that it’s better to try, fail and learn. I had no clue where to start, and you have helped me.

I read a bit about scene.create which was new to me. This led me to find and shamelessly adapt a beautiful script which appears to be written correctly as HA does not return any error. Here it is:

alias: save_light_states
description: Save passed light states to scene of passed name using snapshot_entities
fields:
  snapshot_name:
    name: Snapshot Name
    required: true
    selector:
      text: null
  lights_to_save:
    name: Lights to Save
    required: true
    selector:
      entity:
        multiple: true
sequence:
  - data:
      scene_id: "{{ snapshot_name }}"
      snapshot_entities: >
        {{ expand(lights_to_save) | selectattr('entity_id', 'defined') |
        map(attribute='entity_id') | list }}
    action: scene.create
mode: single

I’m then using all the other suggestions you guys made in an automation. Here it is:

alias: "Alarme si congélateur ouvert"
mode: single
trigger: 
  - platform: state
    entity_id: binary_sensor.congelateur_porte
    state: "on"
    for:
      minutes: 10
action: 
  - service: script.save_light_states
    data:
      snapshot_name: congelateur_ouvert
      lights_to_save:
        - light.lampe_de_table
  - service: light.turn_on
    target:
      entity_id: light.lampe_de_table
    data:
      rgb_color: [255, 0, 0]
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.congelateur_porte
        state: "off"
  - service: scene.turn_on
    target:
      entity_id: scene.congelateur_ouvert

It’s telling me that “extra keys not allowed @ data[‘state’]”. I’ve tried to find which extra key is causing the bother, no success. I think I’m close, and I learned a few nice tricks by knowing where to look, so thanks already! If I can just get my syntax right, I think I’ll be good to go.

I don’t spot any obvious errors. At what line is the error given, or is there no line number?

Did you test the script separately? For the first working attempt I would use a hardcoded scene create, because it has the same parameters that the script has. The script is primarily useful for manual scene creation.

ps. service: is the old notation. It should work but I read more suspected problems with it. try replace those with action:

If you change service: to action: then the pattern for automations should be:

triggers: 
  - trigger: state
    entity_id: binary_sensor.congelateur_porte
    state: "on"
    for:
      minutes: 10
actions: 
  - action: ...

Building the automation using the automation editor helps you get the syntax right. Then you can learn from the yaml it screates, and when you are confident write the yaml manually if you like that.

I replaced service with action, no dice.

There is no precision to the error, if I paste the entirety of it it says

Message malformed: extra keys not allowed @ data[‘state’]

I did not yet test the script separately. I like the idea of having it because I’m likely to reuse that concept several times in the future. I could replace its key elements in the automation but it doesn’t seem to cause the problem when saving said automation.

Thanks again.

When called from the automation, the script adds nothing, except extra room for errors. I too like the script, but the only thing it adds is field definitions for the UI. What it takes away is the option to use it with labels or areas.

The weird thing is, there is no obvious place state is put wrong. Is the error when saving or running? Are you sure you are not running an old version and the new one wasn’t loaded?

As I said, build it uwing the automation editor and compare the outcome.

Are you sure that you didn’t change the formatting when you pasted the code into your post?

That error doesn’t directly mean that there is an indentation issue, but it can be caused by one.

@d921 to the best of my knowledge I pasted it exactly, CTRL-A, CTRL-C, CTRL-V, no more no less.

Are you sure you are not running an old version and the new one wasn’t loaded?

I’m not sure how I would do that.

@Edwin_D I cannot save the automation, the error occurs at the saving step. Also, for your other point, I had not realized I could call labels in an automation. I figured since I would re-use that section of code in several automations the script made sense, now I see it’s always a tradeoff.

I tried including the scene.create function directly, same error message. Here’s what I have

alias: "Alarme si congélateur ouvert"
mode: single
trigger: 
  - platform: state
    entity_id: binary_sensor.congelateur_porte
    state: "on"
    for:
      minutes: 10
action: 
  - action: scene.create
    data:
      scene_id: scene.congelateur_ouvert
      snapshot_entities: 
        - light.lampe_de_table
  - action: light.turn_on
    target:
      entity_id:
        - light.lampe_de_table
    data:
      rgb_color: [255, 0, 0]
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.congelateur_porte
        state: "off"
  - action: scene.turn_on
    target:
      entity_id: scene.congelateur_ouvert

Do you mean to build it with the UI? It doesn’t seem powerful enough to build this, but I might just not be good enough with it…

Yet another post, with more information. I created building blocks one at a time using the UI editor, then added the “scene.create” part. That one I never found in the UI. The code can be saved, without errors, and when I look in the history I see the automation activating… but it does nothing. The light remains white. Here’s what I have

alias: "Alarme si congélateur ouvert"
mode: single
trigger: 
  - type: opened
    device_id: b39190e173467c98348e40fc6d0edadb
    entity_id: e0b609770b7c1ed9d33c691fae88e660
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 0
      seconds: 5
action: 
  - action: scene.create
    data:
      scene_id: scene.congelateur_ouvert
      snapshot_entities: 
        - light.lampe_de_table
  - action: light.turn_on
    metadata: {}
    data:
      rgb_color:
        - 255
        - 0
        - 0
    target:
      device_id: a67e74692d8acdfaecd59e44ae455fc3
  - wait_for_trigger:
    - type: not_opened
      device_id: b39190e173467c98348e40fc6d0edadb
      entity_id: e0b609770b7c1ed9d33c691fae88e660
      domain: binary_sensor
      trigger: device
  - action: scene.turn_on
    target:
      entity_id: scene.congelateur_ouvert

Your format on the scene entity id appears to be the issue. Use something like scene_id: test_scene

And a last one on this topic! I figured it out. That’s a lot of messages, and I apologize, I hope it can help someone else along the way. If I had found some formal syntax tutorial about “scene.create” it would have been easier for me.

The problem was that I named my scene “scene.congelateur_ouvert” . The “scene.” part shouldn’t be there. It SHOULD be there when calling back the entity, to indicate that it’s a scene. Here’s the working code (probably that what I had before would have worked if “scene.” had been removed).

alias: "Alarme si congélateur ouvert"
mode: single
trigger: 
  - type: opened
    device_id: b39190e173467c98348e40fc6d0edadb
    entity_id: e0b609770b7c1ed9d33c691fae88e660
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 10
      seconds: 05
action: 
  - action: scene.create
    data:
      scene_id: congelateur_ouvert
      snapshot_entities: 
        - light.lampe_de_table
  - action: light.turn_on
    metadata: {}
    data:
      rgb_color:
        - 255
        - 0
        - 0
    target:
      device_id: a67e74692d8acdfaecd59e44ae455fc3
  - wait_for_trigger:
    - type: not_opened
      device_id: b39190e173467c98348e40fc6d0edadb
      entity_id: e0b609770b7c1ed9d33c691fae88e660
      domain: binary_sensor
      trigger: device
  - action: scene.turn_on
    target:
      entity_id: scene.congelateur_ouvert

Good! If your latest post describes how you overcame the original problem, please mark it as a Solution, so that other people who might have a similar issue can focus on it.

1 Like

The UI can definitely do it all. Happy it works now.

However you now picked device actions, with the weird id’s. They work, but will give you heaps of problems once you start replacing devices. And you do not have to. You can write what you had in the automation editor too.

To avoid those device ids, use “state” for conditions and trigger and pick an entity. For actions, pick light and then turn on, etc.

There is not a lot that the automation editor cannot do, nowadays. It might switch to yaml at some points for certain parts though.

1 Like

@Edwin_D , you’re right, I will revert to naming the devices themselves, that content was generated by the UI and converted to YAML. I was just happy it worked yesterday evening :slight_smile:

1 Like