Always surprises me when I go to do something in HA and realize that I can’t figure out how.

This time its lights, specifically making sure that they don’t get left on.

Until now I’ve simply been creating an automation for each light switch so that if it changes state from Off to On and when it’s 30 minutes after sunrise it’s starts a 15 minute wait and then changes the state of the switch to off.

This approach mostly works but it’s less than ideal.

First I’m having to create an automation for each device. How do I do it by Area, or list / group of devices, instead?

Second if a device is turned on too early there’s no state change for the automation to catch and it never fires. I could fix this by creating another automation that checks for it but then I’ll have even more of them to manage.

Third this doesn’t work very well if you want different things to happen on the weekends as opposed to during the weekday. For instance on a Saturday I may WANT that closet light to stay on longer because I’m putting away clothes.

It’d be really nice if I could program HA like this ‘On a weekday if you see any device on this list turn on 30 minutes after Sunrise I want you to turn whichever one(s) it was off again 15 minutes later.’.

I’m must be missing something here because surely HA can do this, right?

  • comradegreetingcard@lemmy.ml
    link
    fedilink
    English
    arrow-up
    15
    ·
    edit-2
    10 months ago

    So, you don’t need an automation per entity. You can add additional entities to the trigger seen here (you can add groups too, kitchenlights is a group of 4 lights):

    As circled above, you can also set a “for” time, in this case when the light is on for 15 min.

    The time condition has the option for a day off the week. Just leave the time portion blank.

    To keep this all in one automation, skip the condition section and go to the action section.

    Use the choose block and set the conditions under an option

    The action will be a template

    service: light.turn_off target: entity_id: “{{ trigger.entity_id }}” data: {}

    Edit formatting

    • Buelldozer@lemmy.todayOP
      link
      fedilink
      English
      arrow-up
      2
      ·
      edit-2
      10 months ago

      Wow, this is one of the most thorough answers I’ve ever seen on any HA forum! Thank you!

  • echo64@lemmy.world
    link
    fedilink
    English
    arrow-up
    3
    ·
    10 months ago

    You can call a service on an area, that’s what I do

    If you want different behaviors on different days, you can use conditionals to check. I’d probably have a weekend automation vs a weekday one tho.

    Generally, I just base my light states on motion sensors, turn on if motion, turn off if the motion sensor reports unoccupied

    Keep things as simple in ha automations as possible because it really sucks trying to do anything more complicated. That’s when people turn to node red

  • AlternateRoute@lemmy.ca
    link
    fedilink
    English
    arrow-up
    3
    ·
    10 months ago

    Create a helper group that has all the lights.

    Trigger:

    Helper group turns on for 30 min

    Condition:

    I used time after and before as a fixed time.

    Action:

    service turn off with a target of the helper group

    Should result in the trigger firing 30min after ANY light turns on and only turning them off at the set time.

  • m_randall@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    3
    ·
    10 months ago

    Not a particularly helpful comment but I struggled with this kinda thing until switching to Node Red. It made complex things much easier to get working.

    • Panq@lemmy.nz
      link
      fedilink
      English
      arrow-up
      1
      ·
      10 months ago

      Node Red by far gave me the best automation for numerous lights. X minutes after sunrise, it iterates every light that is on and calls turn off with a fairly long (2 min?) transition time, so the lights all gradually fade off.

      It’s been running for years without me needing to touch anything, it doesn’t care if you replace/rename any lights, and the slow fade when it’s still getting brighter outside makes the change invisible.

      I’ll bet you could do the same thing without Node Red, but nowhere near as easily.

  • CondorWonder@lemmy.ca
    link
    fedilink
    English
    arrow-up
    3
    ·
    10 months ago

    This is a great case for templates. Templates are Home Assistant’s way of doing dynamic actions or data.

    When: whatever triggers you need for the lights - can be multiple triggers or one trigger with every light/switch listed, with a For clause. The list is "OR"ed so do however you want. You could use a group but then all devices would be changed together - which doesn’t sound like is what you want.

    And if: Whatever conditions you want, time, day of week, etc.

    Then do: The action is where it gets interesting add an action to execute “Generic turn off”, select any device (we’ll change that in a moment). The reason for the “Generic turn off” is that it works with devices in any domain (light or switch). Then drop into the YAML view and chcange the entity_id value to this template: '{{ trigger.from_state.entity_id }}'. The trigger is a variable that’s available inside the automation and can be used instead of specifying a static value somewhere. https://www.home-assistant.io/docs/automation/trigger/ for details on triggers.

    Here’s the complete automation, you could create a new empty automation, then copy and paste this in place and just change the trigger and conditions as you want.

    alias: Action - Turn off lights 15min after turned on during day
    description: ""
    trigger:
      - platform: state
        entity_id:
          - switch.porch_light
        for:
          hours: 0
          minutes: 15
          seconds: 0
        enabled: true
        to: "on"
        from: "off"
    condition:
      - condition: sun
        after: sunrise
        after_offset: "30"
      - condition: time
        after: "01:00:00"
        before: "23:00:00"
        weekday:
          - sun
          - mon
          - tue
          - wed
          - thu
          - fri
          - sat
    action:
      - service: homeassistant.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: "{{ trigger.from_state.entity_id }}"
    mode: queued
    max: 10
    
    
  • GlitzyArmrest@lemmy.world
    link
    fedilink
    English
    arrow-up
    3
    ·
    10 months ago

    I use scenes to control my lights and use those in my automations instead. This lets me create “groups” of lights to control.