Door sensor configuration with time/light

Hi there,

I’m pretty new out here and i have some questions…
I have a daughter who, when she supposed to be in bed sneaks around, now i have put a door sensor on her door, horrible me :japanese_ogre:.
I want to automate it with te following:
when she goes to bed at 19:00/19:30 and she leaves her room, i want to turn a light in our livingroom in a colour and if we put her in bed again and close the door, i want to turn the light back to it original colour.
this what i have, at this point

   - alias: deur open
     initial_state: true 
    trigger: 
      - platform: state
        entity_id: binary_sensor.0x00158d00024605ed_contact
        from: 'off'
        to: 'on'
    action:
      - service: light.turn_on
        entity_id: light.hoeklampje
        data:
          brightness: 150
          rgb_color: [255, 0, 0]
   - alias: deur dicht
     initial_state: true 
     trigger:
      - platform: state
        entity_id: binary_sensor.0x00158d00024605ed_contact
        from: 'on'
        to: 'off'
    action:
      - service: light.turn_on
        entity_id: light.hoeklampje
        data:
          brightness: 150
          rgb_color: [247, 245, 163]

it’s working, but…
these are 2 different automations, is there a way to put in in 1?
the colour in the 2nd automation is not the original colour, where it was, is it possible to do so?
i do not know where i have to put a “timer” in, so the automation is active from 19:00 until 24:00/23:59

door sensors: Xiaomi
via: zigbee2mqtt cc2531

thanks in advance!

Michael

It can probably be combined into one automation, but it’s probably not worth the effort. It would become much more complicated for little pay pack. If it were me, I’d leave it as two automations like you have it (although I tend not to use the from: option with binary_sensor’s.)

To save and restore the light’s state, you could use a python_script I wrote for just this purpose. It is documented here, including an example of how you might use it.

Regarding limiting the time period in which these automations will run, you do that by adding a condition to your automations. E.g.:

    condition:
      condition: time
      after: 19:00:00
      before: 24:00:00
1 Like

Thanks Phil!

question, where do i put the script in the automation(s)? also, i think i need to remove the homeassistant.turn_on service?

thanks in advance

Michael

Try:

  - alias: deur open
    initial_state: true 
    trigger: 
      platform: state
      entity_id: binary_sensor.0x00158d00024605ed_contact
      from: 'off'
      to: 'on'
    action:
      - service: python_script.light_store
        data:
          store_name: deur
          entity_id: light.hoeklampje
      - service: light.turn_on
        entity_id: light.hoeklampje
        data:
          brightness: 150
          rgb_color: [255, 0, 0]

and

  - alias: deur dicht
    initial_state: true 
    trigger:
      platform: state
      entity_id: binary_sensor.0x00158d00024605ed_contact
      from: 'on'
      to: 'off'
    action:
      service: python_script.light_store
      data:
        store_name: deur
        operation: restore

thanks again Phil,

in my config folder i created a python_scripts folder and there i dropped your light_store.py file and saved it as a python source file.
then i copied the automations wich you suggested, it was a valid config, but no luck.
when i opened and closed the door (didn’t wake my daughter), there was also no change in my entity card.

Did you add python_script: to your configuration? Does the account that is running HA have permission to the folder and script? Do you see python_script.light_store listed in the available services on the Services page? Did you restart HA, or at least run the python_script.reload service?

got it working!

but…when i put the condition in i get the following message:

  - alias: deur dicht
    initial_state: true
    trigger: 
      platform: state
      entity_id:  binary_sensor.0x00158d0002460515_contact
      to: 'off'
    condition:
      condition: time
        after: 19:00:00
        before: 24:00:00
    action:
      service: python_script.light_store
      data:
        store_name: deur
        operation: restore

Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/configuration.yaml”, line 283, column 14

That because @pnbruckner wrote this;

condition:
  condition: time
  after: 19:00:00
  before: 24:00:00 

And you did this;

condition:
  condition: time
    after: 19:00:00
    before: 24:00:00

Small but essential difference. If it still fails you probably need to use single quotes as in the docs.

doesn’t work either:

Invalid config for [automation]: extra keys not allowed @ data[‘condition’][0][‘after’]. Got None
extra keys not allowed @ data[‘condition’][0][‘before’]. Got None
not a valid value for dictionary value @ data[‘condition’][0][‘condition’]. Got None
required key not provided @ data[‘condition’][0][‘entity_id’]. Got None. (See /config/configuration.yaml, line 261). Please check the docs at https://home-assistant.io/components/automation/

also tried the single quotes, no luck

the time’s should be in quotes;

condition:
  condition: time
  after: '19:00:00'
  before: '24:00:00'

when i try your suggestion

 - alias: deur open
   initial_state: true 
   trigger:
     platform: state
     entity_id:  binary_sensor.0x00158d0002460515_contact
     to: 'on'
   condition:
     condition: time
     after: '19:00:00'
     before: '24:00:00'
   action:
     - service: python_script.light_store
        data:
          store_name: deur
          entity_id: light.hoeklampje
     - service: light.turn_on
       entity_id: light.hoeklampje
       data:
         brightness: 150
         rgb_color: [255, 0, 0]

i get the following message:

Invalid config for [automation]: extra keys not allowed @ data[‘condition’][0][‘after’]. Got None
extra keys not allowed @ data[‘condition’][0][‘before’]. Got None
not a valid value for dictionary value @ data[‘condition’][0][‘condition’]. Got None
required key not provided @ data[‘condition’][0][‘entity_id’]. Got None. (See /config/configuration.yaml, line 261). Please check the docs at https://home-assistant.io/components/automation/

Well there is another mistake in the action

 - service: python_script.light_store
    data:
      store_name: deur
      entity_id: light.hoeklampje

Must be;

  - service: python_script.light_store
    data:
      store_name: deur
      entity_id: light.hoeklampje

You need to watch the indentation very carefully.

it’s a typo in the preformatted tekst, in the yaml file it’s correct. sharp eyes tho…

the yaml line in the error message is the line of the condition in the config.yaml

24 hours is not allowed… It should be 00:00:00

1 Like

Yep, as @Pippyn wrote. He is the winner :joy:

yep, @Pippyn …you’re the man!

thank guys for helping me out

Next :beer: is on me!

@marinesjeng please put the topic to solved, this might help others.

It seems after some try-outs the condition: time doesn’t work, my mistake i guess. even at 16:00 the lights go to red and back to it original colour.

- alias: deur dicht
  initial_state: true
  trigger: 
    platform: state
    entity_id:  binary_sensor.0x00158d0002460515_contact
    to: 'off'
  condition:
    condition: time
    after: '19:00:00'
    before: '23:30:00'
  action:
    service: python_script.light_store
    data:
      store_name: deur
      operation: restore

and

- alias: deur open
  initial_state: true 
  trigger:
    platform: state
    entity_id:  binary_sensor.0x00158d0002460515_contact
    to: 'on'
  condition:
    condition: time
    after: '19:00:00'
    before: '23:30:00'
  action:
    - service: python_script.light_store
      data:
        store_name: deur
        entity_id: light.hoeklampje
    - service: light.turn_on
      entity_id: light.hoeklampje
      data:
        brightness: 150
        rgb_color: [255, 0, 0]

I don’t think that’s necessary, but of course, it doesn’t hurt to have them.

EDIT: Ok, I might be wrong here. I’m pretty sure times for at: in a time trigger don’t need to be quoted, but it does appear that times in a time condition do. Again, doesn’t hurt to quote times in both cases.

Hmm, can’t explain that. Are you sure the clock is set correctly on the system running HA, and the time zone setting in the environment (e.g., OS) and HA agree and are set correctly?