Automate brightness during day, evening, night automation?

Hi everybody,

I am replacing my regular light switches step by step with motion sensors. Ideally, lights will only be on when somebody is in the room and turn off by itself. This works fine, however, I need to also control the brightness.

During the day, it should be rather high, in the evening low, and at night at minimal (so that you can see but it won’t wake you up).

Below the code I use and some explanation

attempt for binary sensors, not working

binary_sensor:
  - platform: tod
    name: "Tagsüber"
    after: sunrise
    before: sunset
  - platform: tod
    name: "Nachts"
    after: sunset
    before: sunrise
    after_offset: "+01:30:00"
  - platform: tod
    name: "Abends"
    after: sunset
    after_offset: "-01:30:00"

problem:

In developer tools => states I can see binary_sensor.tagsuber, but both binary_sensor.abends and binary_sensor.nachts are missing. I just created them, so perhaps they will not appear since the first time they have been triggered (though I highly doubt this!)?

attempted automation (which I cannot try because not all of those sensors above work yet)

automation:
  - alias: "[Licht] Helligkeit"
    trigger:
      - platform: state
        entity_id: 
          - binary_sensor.motion_flur_occupancy
          - binary_sensor.motion_schlafzimmer_occupancy
          - binary_sensor.motion_kueche_occupancy
        to: "on"
    action:
      - service: light.turn_on
          entity_id: >
            {% set myroom = trigger.to_state.object_id.split('_')[1] %}
            light.{{myroom}}_light
          data:
            brightness: >
              {% if state_attr('binary_sensor.tagsuber', 'true') %}
                180
              {% elif state_attr('binary_sensor.abends'), 'true' %}
                80
              {% elif state_attr('binary_sensor.nachts'), 'true' %}
                15
              {% else %}
                30
              {% endif %}

This is supposed to

  • trigger when either motion sensor is triggered
  • as both lights and motion sensors follow a pattern (binary_sensor.motion_<room>_occupancy and light.<room>_light), automatically decide which light to turn on
  • therefore set variable and automatically use it per sensor/light
  • check whether it is daytime (tagsuber, evening (abends), or nighttime (nachts); depending on what daytime it is, set brightness to appropriate level (these numbers are just examples, still have to figure out the right values)

Why do this?
==> I do not want to manually modify the time when the seasons change, so I cannot use, for example, at: "18:30:00 as a value for evening time; it is dark much earlier in winter and much later in summer, so I need to use sunrise/sunset and an offset to determine when it is what.

Should my automation work if I get the binary_sensors to work? I currently do this per room without templates, so I am not 100% sure, but if it works, I will adapt my automation for the off automation as well (which is pretty much identical, except that the motion sensor needs to be off for a certain period of time (to assure that if it turns off and right back on because somebody is still inn there nothing is triggered).

Thanks for your ideas :slight_smile:

Both before and after are required in the sensor config which is probably why abends isn’t appearing. The other not appearing is strange though. Any errors in your log?

Edit: also, what Tinkerer said. Any changes to configuration.yaml require a Home Assistant restart to take effect.

Did you restart Home Assistant after adding those?

Did you run a configuration check before restarting HA?

EDIT: Yes, it works now. The sensor part was missing the “before” segment.

EDIT 2: @Tinkerer …I know :wink: I have a bit of a weird setup: Home Assistant running on my Home Server, me working on my regular computer. So I wrote a script to scp the Home Assistant root folder I am working on (on my pc) to the Home Server; once it has copied it there, the script also automatically restarts Home Assistant (= me trying to save some time :smiley: ), which is the reason why I usually don’t run config check. I will look into how to solve this (other than not automating this); perhaps my script can run config check before it restarts and only do so if there are no errors).

Thank you!

Yes, I have restarted Home Assistant after adding those. I have separate .yaml files for all my automations, so I always restart (just re-loading things does not seem to work).

No, I have not run a configuration check; however, neither the webUI shows any errors (which it usually does when something is wrong), nor the log. I just ran cat home-assistant.log | grep auto, which usually always shows me what is wrong, and there is nothing.

I just made sure each sensor has before and after. Just copied the yaml to my Home Assistant and am restarting. Will edit this post to display results once I got them.

Always run the config check when you make changes :wink:

1 Like