Dynamically turn off lights and switches

Hi,

I am trying to use one automation to turn off lights and devices when i am not home. I have made up the following code, but it does not seems to work. I will receive the push notification with the devices which were on correctly, but the device is never turned off.

- id: Notify device or lamp still on when leaving
  alias: 'Notify device or lamp still on when leaving' 
  initial_state: 'on' 
  trigger: 
    - platform: state 
      entity_id: group.all_devices
      to: 'not_home'
      for:
        minutes: 10
  condition: 
   condition: or
   conditions: 
     - condition: state 
       entity_id: switch.plug_158d00019d43e8
       state: 'on'
     - condition: state 
       entity_id: light.level #huiskamer_tafel
       state: 'on'
     - condition: state 
       entity_id: light.level_2 #huiskamer_plafond
       state: 'on'
     - condition: state 
       entity_id: switch.plug_158d0001a6592f
       state: 'on'
     - condition: state 
       entity_id: switch.plug_158d0001dae65a
       state: 'on'
     - condition: state 
       entity_id: light.kast
       state: 'on'
     - condition: state 
       entity_id: media_player.tv
       state: 'on'
     - condition: state 
       entity_id: media_player.denon_receiver
       state: 'on'
     - condition: template 
       value_template: "{{ states.media_player.ziggo_mediabox.state != 'off' }}"
  action:
    - service: notify.iphone
      data_template:
        title: Automatische actie ivm afwezigheid
        message: >
          {% if states.switch.plug_158d00019d43e8.state == 'on' %}
          De huiskamer hoeklamp stond nog aan. Deze is uitgezet.
          {%endif%}
          {% if states.light.level.state == 'on' %}
          De huiskamer tafellamp stond nog aan. Deze is uitgezet.
          {%endif%}
          {% if states.light.level_2.state == 'on' %}
          De huiskamer plafondlamp stond nog aan. Deze is uitgezet.
          {%endif%}
          {% if states.switch.plug_158d0001a6592f.state == 'on' %}
          De keukenlamp stond nog aan. Deze is uitgezet.
          {%endif%}
          {% if states.switch.plug_158d0001dae65a.state == 'on' %}
          De tuinverliching stond nog aan. Deze is uitgezet.
          {%endif%}
          {% if states.light.kast.state == 'on' %}
          De kastverlichting stond nog aan. Deze is uitgezet.
          {%endif%}
          {% if states.media_player.tv.state == 'on' %}
          De TV stond nog aan. Deze is uitgezet.
          {%endif%}
          {% if states.media_player.denon_receiver.state == 'on' %}
          De Receiver stond nog aan. Deze is uitgezet.
          {%endif%}
          {% if states.media_player.ziggo_mediabox.state != 'off' %}
          De Ziggobox stond nog aan. Deze is uitgezet.
          {%endif%}
    - service: homeassistant.turn_off
      data_template:
        entity_id: >
          {% if states.switch.plug_158d00019d43e8.state == 'on' %}
          switch.plug_158d00019d43e8
          {%endif%}
          {% if states.light.level.state == 'on' %}
          light.level
          {%endif%}
          {% if states.light.level_2.state == 'on' %}
          light.level_2
          {%endif%}
          {% if states.switch.plug_158d0001a6592f.state == 'on' %}
          switch.plug_158d0001a6592f
          {%endif%}
          {% if states.switch.plug_158d0001dae65a.state == 'on' %}
          switch.plug_158d0001dae65a
          {%endif%}
          {% if states.light.kast.state == 'on' %}
          light.kast
          {%endif%}
          {% if states.media_player.tv.state == 'on' %}
          media_player.tv
          {%endif%}
          {% if states.media_player.denon_receiver.state == 'on' %}
          media_player.denon_receiver
          {%endif%}
          {% if states.media_player.ziggo_mediabox.state != 'off' %}
          media_player.ziggo_mediabox
          {%endif%}

somebody an idea?
thanks

It’s not a direct answer, but turning off an already turned off device does not harm. So why not remove all the “if” statements from the second service call and just enumerate all the entities? And - the entity_id value must be either an array or a comma-separated string. The result of your data_template must result in either

entity_id: switch.plug_158d00019d43e8, light.level, light.level2

or

entity_id:
  - switch.plug_158d00019d43e8
  - light.level
  - light.level2

Thats quite a good idea. I will try that. The push notification on the other hand is giving a nive notification of the particular device. thanks will try it