Too complex for one automation?

Hi again Everyone,

I have been reworking my automation’s and my wife asked for something that I think is a good idea. We have a mudroom which I have two Hue lights in and a Neo Coolcam motion sensor. I have an automation that does the following:

  • When motion is detected, turn on the lights in this room if:
    a) Either one of us is home
    b) It’s an hour before sunset or an hour before sunrise

So I wrote this automation, it seems to work well:

- alias: Mudroom-Motion
  trigger:
    - platform: state
      entity_id: binary_sensor.neo_coolcam_battery_powered_pir_sensor_mudroom
      from: 'off'
      to: 'on'
  condition:
    - condition: state
      entity_id: light.mudroom
      state: 'off'
    - condition: or
      conditions:
        - condition: state
          entity_id: input_select.chris_status_dropdown
          state: 'Home'
        - condition: state
          entity_id: input_select.val_status_dropdown
          state: 'Home'
    - condition: or
      conditions:
        - condition: sun
          after: sunset
          after_offset: "-1:00:00"
        - condition: sun
          before: sunrise
          before_offset: "1:00:00"
  action:
    - service: homeassistant.turn_on
      entity_id: light.mudroom

So my wife asked that if the time is midnight to 1 hour before sunrise that the brightness of these lights be 50% of normal brightness (reason being, she might get up at 2AM to go for a smoke and she would use this door to go outside to do so and doesn’t want super-bright light to flood the area.) I think that this is a really good idea (I wouldn’t see the light but that doesn’t make it a bad idea as when the door opens, the light would flood out into the porch) but I’m stuck how to integrate such a notion into the existing automation above.

So my question is, how do I do this? I was hoping for some kind of variable assignment (which I’ve been crawling the forum for an answer) with the basic idea of having an additional condition such as (pseudo-code):

  • condition: time
    after: “00:00:00”
    before: sunrise
    value: rgb[x,y,z]
    else
    value: rgb[a,b,c]

Which would then be applied to the action. Anyone have any ideas how I can accomplish this or do I need to write a separate automation and/or script to do this?

Thanks!

Use a data template in your action, and define the light settings in scenes.

I’m on the my phone atm, but it’s basically…

action:
  service: scene.turn_on 
  data_template:
    entity_id: >
       {% if...
           scene.mudroom_night
        {% else... 
           scene.mudroom_normal
        {% endif %} 

Isn’t the motion detector and one of you home the same or do you have a pet?

Yup and I don’t have door sensors yet :slight_smile: But I have a big goofy chocolate lab who likes to wander around when we’re not home.

So I’m thinking of something like this based on what you said:

action:
 - service: homeassistant.turn_on
   data_template:
   entity_id: >
     {% '{{ now().time().strftime("%H") >= "23" and  now().time().strftime("%H") <= "06" }}' %}
       light.mudroom
       brightness_pct: 50
     {% else %}
       light.mudroom
       brightness_pct: 100
     {% endif %}

She said between midnight and 6AM :slight_smile:

Will try this but I’m definitely on the right track! Thank you very much!

if now().hour < 6would be a lot neater :stuck_out_tongue_winking_eye:

But you can’t put the entity id and the brightness in the same result as they will come out as one long string, needs to be a scene.

Ah OK. This is non-ambient hue lights but I’ll try scenes. now().hour is much neater :slight_smile:

So I tried this:

  action:
    - service: scene.turn_on
      data_template:
      entity_id: >
        {% '{{ if now().hour >= 23 and now().hour <= 6 }}' %}
          scene.nightlight
        {% else %}
          scene.bright
        {% endif %}

But it’s generating an error in config-check, can’t see what the issue is per se:

2018-02-16 20:31:40 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: Entity ID {% '{{ if now().hour >= 23 and now().hour <= 6 }}' %}
  scene.nightlight
{% else %}
  scene.bright
{% endif %} is an invalid entity id for dictionary value @ data['action'][0]['entity_id']. Got None
expected a dictionary for dictionary value @ data['action'][0]['data_template']. Got None. (See /home/hass/.homeassistant/configuration.yaml, line 126). Please check the docs at https://home-assistant.io/components/automation/
  action:
    - service: scene.turn_on
      data_template:
        entity_id: >
          {% if now().hour >= 23 and now().hour <= 6 %}
            scene.nightlight
          {% else %}
            scene.bright
          {% endif %}

Ah, no evaluation required and spacing-gotcha :slight_smile: Will try this tonight, if it works, I will mark this as solution – much appreciated good Sir!

1 Like

OK, this passed config-check but I realized it won’t work because it doesn’t specify any light, in this case light.mudroom

The light is specified in the scene, no?

I just realized that my scenes only target the livingroom lights as they’re ambient bulbs. Normally we’ve been using brightness (vs scene) for all other lights. I may have to duplicate the scene for mudroom I guess.

EDIT: I should note that my living lights were indeed changing to nightlight / bright as I altered the automation time frame to test :stuck_out_tongue: This is the solution, thank you my friend :slight_smile:

1 Like

Wife is super happy :slight_smile: Thanks again!

1 Like

Always fun putting a smile on the face of another man’s wife :wink:

Heh :slight_smile:

1 Like