Template: Turn on multiple lights/devices at once

Why does this script not execute?

  - alias: "Belysning: Mysbelysning kväll tänd"
    trigger:
      platform: state
      entity_id: input_select.house_time_modes
      to: "Kväll"
    action:
      - service: homeassistant.turn_on
        data_template: 
          entity_id: >
              switch.lampor_vardagsrummet_bokhyllan_switch
              switch.lampor_vardagsrummet_soffan_switch
              switch.lampor_vardagsrummet_hornlampan_switch

Nothing happens even if i trigger it “manually” from the front-end. I do use the same trigger in other automations, so that cannot be the problem.

I guess this has something to do with the data_template since I want to turn on multiple lights at once.

This must be a simpel one, but I cannot find any examples similar to my setup.

Didn’t look very hard then :stuck_out_tongue_winking_eye:

alias: "Belysning: Mysbelysning kväll tänd"
trigger:
  platform: state
  entity_id: input_select.house_time_modes
  to: "Kväll"
action:
  service: homeassistant.turn_on
  entity_id:
    - switch.lampor_vardagsrummet_bokhyllan_switch
    - switch.lampor_vardagsrummet_soffan_switch
    - switch.lampor_vardagsrummet_hornlampan_switch
2 Likes

That worked like a charm!
Thanks alot @anon43302295 :smiley:

Now if I add some conditions for the rest of the lights, is there some way to simplify this script?

  - alias: "Belysning: Mysbelysning kväll på"
    trigger:
      platform: state
      entity_id: input_select.house_time_modes
      to: "Kväll"
    action:
      - service: homeassistant.turn_on
        entity_id:
          - switch.lampor_vardagsrummet_bokhyllan_switch
          - switch.lampor_vardagsrummet_soffan_switch
          - switch.lampor_vardagsrummet_hornlampan_switch
          - switch.lampa_isabelles_fonster_switch
          - switch.lampor_koksfonstret_switch
          - switch.lampa_pa_nedre_frysen_switch
          - switch.albins_lavalampa_switch
      - service: homeassistant.turn_on
        entity_id: light.taklampan_ovre_trapphall_level
        data:
          brightness: 100   
      - service: homeassistant.turn_on
        data_template: 
          entity_id: >
            {% set sekunder = now().hour * 3600 + now().minute* 60 + now().second %} 
            {% if sekunder < 77400 %}
              - switch.lampa_ilias_sang_switch
              - switch.lampa_annas_sang_switch      
            {% endif %}      
      - service: homeassistant.turn_on
        data_template: 
          entity_id: >
            {% if sekunder < 71100 %}
              - switch.lampa_eriks_fonster_switch
              - switch.lampa_eriks_jordglob_switch
              - light.eriks_myshorna
                  data:
                    color_temp: 447
                    brightness: 84            
            {% endif %}

Did this work for you?
It seems that no matter what I do, I can only switch one device on.
For example, if I cut-and-paste the same format as you suggested, the second one of the three lights that I list will turn on, but not the other two. If I simply swap them and try again, whichever falls in second will turn on, but still not the others.
I have also created groups, scenes, etc… I can never get multiple entities to turn on.
By the way… they are all of the same type.

1 Like

Yes. I have tons of these dotted around my configuration, they all work. This is pretty basic homeassistant config.

Then something else is wrong.

Well… That much I know (i.e. basic stuff & something else is wrong). :wink: The question is: what?
I have tried everything, but again a simple automation (i.e. in the automation.yaml) such as:

- id: '1510464937408'
  alias: Bedroom at Night
  trigger:
    - platform: time
      at: '23:30'
  condition: []
  action:
  - service: light.turn_off
    data:
      entity_id: light.bedroom_light
  - service: light.turn_on
    data:
      entity_id: light.bedroom_fan

works fine. But if I change “off” to “on” in the first service line, it won’t work anymore.

- id: '1510464937408'
  alias: Bedroom at Night
  trigger:
    platform: time
    at: '23:30:00'
  action:
    service: light.turn_on
    entity_id:
      - light.bedroom_light
      - light.bedroom_fan

I had tried that before (but did it again, just in case). Same difference: only the fan goes on. Also, with this structure, I can only turn “on” or turn “off” everything (and hence, it never works). With my original structure (above), I can make one “off” and the other “on” and that is the only case that two services will work. (i.e. with my code, “off” and “on” works, but “on” and “on” or even “on” and “off” won’t work).

Here are my config/auto .yaml files if anyone wants to take a look.

##configuration.yaml
    homeassistant:
      # Name of the location where Home Assistant is running
      name: Home
      # Location required to calculate the time the sun rises and sets
      latitude: 38.9517
      longitude: 92.3341
      # Impacts weather/sunrise data (altitude above sea level in meters)
      elevation: 274
      # metric for Metric, imperial for Imperial
      unit_system: imperial
      # Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
      time_zone: America/Chicago

    # Show links to resources in log and frontend
    introduction:

    # Enables the frontend
    frontend:

    # Enables configuration UI
    config:

    http:

    # Checks for available updates
    # Note: This component will send some information about your system to
    # the developers to assist with development of Home Assistant.
    # For more information, please see:
    # https://home-assistant.io/blog/2016/10/25/explaining-the-updater/
    updater:
      # Optional, allows Home Assistant developers to focus on popular components.
      # include_used_components: true

    # Discover some devices automatically
    discovery:

    # Allows you to issue voice commands from the frontend in enabled browsers
    conversation:

    # Enables support for tracking state changes over time.
    history:

    # View all events in a logbook
    logbook:

    # Track the sun
    sun:

    # Text to speech
    tts:
      platform: google

    automation: !include automations.yaml

    hue:
      bridges:
        - host: 10.14.1.58
          filename: phue.conf
          allow_unreachable: true
          allow_hue_groups: true





    ##automation.yaml
    - id: '1510464937408'
      alias: Bedroom at Night
      trigger:
        platform: time
        at: '23:30:00'
      action:
        service: light.turn_on
        entity_id:
          - light.bedroom_light
          - light.bedroom_fan

Try it with homeassistant.turn_on ?

I had done that too. :wink: But did it again. Same behavior, except that I have just noticed that with “light.turn_on”, it is the bedroom_fan (second one) that goes on, but with homeassistant.turn_on it is the bedroom_light (first one) that goes on.

What sort of lights are they?

Hey! I think I have a corrupted script. Could you please send me your:/srv/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/service.py file?

I have just noticed this error while turning on/off bedroom_fan:

Thu Dec 27 2018 10:46:37 GMT-0600 (Central Standard Time)

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 277, in _handle_service_platform_call
    await getattr(entity, func)(**data)
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/components/light/hue.py", line 388, in async_turn_off
    await self.light.set_state(**command)
  File "/srv/homeassistant/lib/python3.6/site-packages/aiohue/lights.py", line 75, in set_state
    json=data)
  File "/srv/homeassistant/lib/python3.6/site-packages/aiohue/bridge.py", line 64, in request
    data = await res.json()
  File "/srv/homeassistant/lib/python3.6/site-packages/aiohttp/client_reqrep.py", line 947, in json
    return loads(stripped.decode(encoding))
  File "/usr/local/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 81 (char 80)

I’m not at home, but I would guess the latest version would be in the homeassistant GitHub repo :slight_smile:

I updated it a week ago. It didn’t fix the problem.

Maybe time for a reinstall.

Like I say, this should work as standard without complications.

I’m guessing you’re an isolated case as otherwise the bug reports would be flooding in :slight_smile:

Good point. I’ll give it a try (re-install).

Just FYI… reinstalling didn’t work. It seems that the json responses from the HABridge are not being correctly decoded by HomeAssistant.
It is weird because I can’t find any other related bug reports around. So, it may be some wrong setting that is very rare, and I was “lucky” to have picked that. :wink:

Oh dear.

Possibly you may need to raise a bug report and see if the devs can help you out.

I solved the problem by adding the code below to line 947 of /srv/homeassistant/lib/python3.6/site-packages/aiohttp/client_reqrep.py
Now multiple service, actions, group of devices… everything that wasn’t working is now working. :wink:

### GND ADDED LINES FROM HERE TO "ORIGINAL" BELOW
while True:
   try:
       return loads(stripped.decode(encoding))
       break
   except ValueError:
       print("Oops! GND: Captured error  ...")
       return None
   except:
       print("Oops! GND: Non-Captured error  ...")
       return None

### GND ORIGNAL LINE return loads(stripped.decode(encoding))
2 Likes

If the ligths you want to turn off correspond to a group, you can alternatively just issue the action for the group:

action:
    - service: switch.turn_off
      data:
        entity_id: group.kitchen_lights
1 Like