How to turn group (of 3 devices) off, Else if 1 known device in that group uses over 6 watts, only turn 2 devices off

Hi,

I’m trying to make a complex routine (I feel anyway) but can I have some help in making it work, please?
I have a TV setup with 3 separate switches around the room with extension leads in with wireless speakers in them and it’s set to turn them all off if the TV isn’t being used after a set amount of time.
How do I turn off a group (“group.lounge_group”, which has: “Switch.TV”, “Switch.L” and “Switch.R”) unless “Switch.L” in that group is using over 6 watts, as a phone is being charged also for example, then only turn off “Switch.TV” and “Switch.R”?

I have part of my “automations.yaml as”:

208 - id: '#############'
209   alias: Lounge Plugs - Turn off if Idle
210   description: Turn off Lounge if Idle
211   trigger:
212   - platform: state
213     entity_id:
214     - switch.lounge_tv
215     from: 'off'
216     to: 'on'
217     id: tv plug goes on
218     for:
219       hours: 0
220       minutes: 15
221       seconds: 0
222   - platform: numeric_state
223     entity_id: sensor.lounge_tv_current_consumption
224     for:
225       hours: 0
226       minutes: 5
227       seconds: 0
228     below: 40
229   - platform: state
230     entity_id:
231     - sensor.lounge_tv_audio_input_format
232     for:
233       hours: 0
234       minutes: 15
235       seconds: 0
236     to: PCM 2.0 no audio
237     from: PCM 2.0
238   condition:
239   - condition: not
240     conditions:
241     - condition: state
242       entity_id: media_player.living_room_tv
243       state: playing
244       for:
245         hours: 0
246         minutes: 0
247         seconds: 0
248   action:
249   - service: >
250         {% if numeric_state('switch_l_current_consumption') < 6 %} homeassistant.turn_off
251         {% else %} switch_r.turn_off switch_tv.turn_off {% endif %}
252     entity_id: group.lounge_group
253     target:
254       area_id: living_room
255   mode: single

For Starters, I’m not convinced {% else %} switch_r.turn_off switch_tv.turn_off {% endif %} is how to easily specify 2 devices to turn off in an ‘else’ clause, I guess how to specify those 2 devices is all I need, unless you tell me otherwise?

Many thanks in advance :slight_smile:

Below is my best guess based on what you have provided. If it doesn’t work, please post screen shots showing the entities in question in the Developer Tools > States tool.

action:
  - service: homeassistant.turn_off
    target:
      entity_id: >
        {% if state_attr('switch.l', 'current_consumption') < 6 %}
        {{ state_attr('group.lounge_group', 'entity_id') }}
        {% else %} {{ ['switch.r', 'switch.tv'] }}
        {% endif %}

Thanks for your speedy reply! This is your code with the proper entities in them:

`action:
  - service: homeassistant.turn_off
    target:
      entity_id: >
        {% if state_attr('switch.sonos_play_1_l', 'current_consumption') < 6 %}
        {{ state_attr('group.lounge_group', 'entity_id') }}
        {% else %} {{ ['switch.sonos_play_1_r', 'switch.lounge_tv'] }}
        {% endif %}`

Unfortunately, it just turns off the whole group regardless of any current consumption readings on the ‘Switch.l’.

These are the screenshots you asked for:-
The Group, which shows all the entities anyway:


And the individual Entities:

According to what you have posted, the current consumption data is not available as an attribute of the switch entity. Does switch.sonos_play_1_l have an associated sensor that gives that data? If you click the the information button to the left of the entity name then click the “Related” tab of the pop-up it should be in the list.

Yes, it has the sensor sensor.sonos_play_1_l_current_consumption



and I can see the readying on my dashboard also.

Great, so we just need to use that sensor in the template:

action:
  - service: homeassistant.turn_off
    target:
      entity_id: >
        {% if states('sensor.sonos_play_1_l_current_consumption') | float(0) < 6 %}
        {{ state_attr('group.lounge_group', 'entity_id') }}
        {% else %} {{ ['switch.r', 'switch.tv'] }}
        {% endif %}

Unfortunately it’s still turning the whole lot off :frowning:
This is the entire block if you can see something else that may be affecting it. I’ve put the time to seconds so I can test it and to make sure HA is registering the current consumption as over 6 watts I turn on that plug separately before turning on the TV plug which should trigger this routine:

- id: '1673994034512'
  alias: Lounge Plugs - Turn off if Idle
  description: Turn off Lounge if Idle
  trigger:
  - platform: state
    entity_id:
    - switch.lounge_tv
    from: 'off'
    to: 'on'
    id: tv plug goes on
    for:
      hours: 0
      minutes: 0
      seconds: 13
  - platform: numeric_state
    entity_id: sensor.lounge_tv_current_consumption
    for:
      hours: 0
      minutes: 0
      seconds: 12
    below: 40
  - platform: state
    entity_id:
    - sensor.lounge_tv_audio_input_format
    for:
      hours: 0
      minutes: 0
      seconds: 11
    to: PCM 2.0 no audio
    from: PCM 2.0
  condition:
  - condition: not
    conditions:
    - condition: state
      entity_id: media_player.living_room_tv
      state: playing
      for:
        hours: 0
        minutes: 0
        seconds: 0
  action:
  - service: homeassistant.turn_off
    target:
      entity_id: >
        {% if states('sensor.sonos_play_1_l_current_consumption') | float(0) < 6 %}
        {{ state_attr('group.lounge_group', 'entity_id') }}
        {% else %} {{ ['switch.sonos_play_1_r', 'switch.lounge_tv'] }}
        {% endif %}
    target:
      area_id: living_room
    data: {}
  mode: single

As shown in my examples, you need to remove:

target: 
  area_id: living_room

… leaving it in is overwriting the target generated by the entity_id: template.

Fantastic :smiley: Many thanks for your help on this, this behaving how I wanted now when it’s triggered :smiley: I just need to work on why it is triggering unexpectedly now, but that is a different issue I have just discovered, so thanks again :).