Switch on/off automation

I would like to create an automation with which, by detecting the on / off status of a switch.plug_158d00010aadf8, I can turn on / off a light light.cucina_1. Do you have any ideas?
thank you

Try this…

#light light.cucina_1 Light ON after switch.plug_158d00010aadf8 ON
- id: LightOnAfterSwitchON
  alias: Cucina Light 1 On
  trigger:
    platform: state
    entity_id: switch.plug_158d00010aadf8
    to: 'on'
  condition:
    condition: state
    entity_id: light.cucina_1
    state: 'off'
  action:
    service: homeassistant.turn_on
    entity_id: light.cucina_1

To turn it off again copy and paste the same code then rework the ‘states’ and change the alias to reflect what you are doing…

Hope that helps!

This is my follower automation

##########################################################################################
##########################################################################################
automation outside_side_light_follower:
  initial_state: 'on'
  alias: outside_side_light_follower
  trigger:
    - platform: state
      entity_id: light.outside_lights
      to: 'on'
    - platform: state
      entity_id: light.outside_lights
      to: 'off'
  action:
    - service_template: 'light.turn_{{trigger.to_state.state}}'  
      entity_id: light.outside_side_light
##########################################################################################
##########################################################################################

Hmmm…that looks neat RobDYI

trigger:
  entity_id: switch.plug_158d00010aadf8
  platform: state
action:
  data:
    entity_id: light light.cucina_1
  service_template: light.turn_{{ trigger.to_state.state | lower }}

Makes light light.cucina_1 follow the state of switch.plug_158d00010aadf8.

2 Likes

or try:

automation:
  outside_side_light_follower:
    alias: 'Outside light follower'
    id: 'Outside light follower'
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: light.outside_lights
    condition: template
    value_template: >
      {{trigger.to_state.state in ['on','off']}}
    action:
      service_template: >
        light.turn_{{trigger.to_state.state}}
      entity_id: light.outside_side_light

with this code could it work?

- id: '44444444444444'
  alias: Mensola
  trigger:
    platform: state
    entity_id: switch.plug_158d00010aadf8
  action:
  - data:
      brightness: 1
      entity_id: light.tradfri_driver
    service: light.turn_off
  - data:
      entity_id: light.tradfri_driver_2
    service: light.turn_off
  - data:
      entity_id: light.tradfri_bulb
    service: light.turn_off
  - data:
      entity_id: light.tradfri_bulb2
    service: light.turn_off
  - data:
      entity_id: light.tradfri_bulb3
    service: light.turn_off
  - data:
      entity_id: light.tradfri_bulb4
    service: light.turn_off
  - data:
      entity_id: light.tradfri_bulb5
  - data:
      brightness: 3
      entity_id: light.cucina_1
      rgb_color:
      - 255
      - 162
      - 71
    service_template: light.turn_{{ trigger.to_state.state | lower }}
  - data:
      brightness: 3
      entity_id: light.cucina_2
      rgb_color:
      - 255
      - 162
      - 71
    service_template: light.turn_{{ trigger.to_state.state | lower }}
  - data:
      brightness: 3
      entity_id: light.cucina_3
      rgb_color:
      - 255
      - 162
      - 71
    service_template: light.turn_{{ trigger.to_state.state | lower }}
  - data:
      brightness: 3
      entity_id: light.gateway_light_f0b4299a4613
      rgb_color:
      - 255
      - 162
      - 71
    service_template: light.turn_{{ trigger.to_state.state | lower }}

not this won’t work. you’ re mixing a lot of things, and have some yaml errors…

see this:

 - data:
      brightness: 1
      entity_id: light.tradfri_driver
    service: light.turn_off

if you turn it off there’s no need for brightness. If you want brightness 1, (which i think isn’t available ) you need to turn the light On.

  - data:
      entity_id: light.tradfri_driver_2
    service: light.turn_off
  - data:
      entity_id: light.tradfri_bulb
    service: light.turn_off
  - data:
      entity_id: light.tradfri_bulb2
    service: light.turn_off
  - data:
      entity_id: light.tradfri_bulb3
    service: light.turn_off
  - data:
      entity_id: light.tradfri_bulb4
    service: light.turn_off

can be:

  - service: light.turn_off
    entity_id: 
      - light.tradfri_driver_2
      - light.tradfri_bulb
      - light.tradfri_bulb2
      - light.tradfri_bulb3
      - light.tradfri_bulb4

why do you use |lower, is your state in capitals?

and as for this. besides the yaml itself (spacing, indenting):

 - data:
      brightness: 3
      entity_id: light.cucina_1
      rgb_color:
      - 255
      - 162
      - 71
    service_template: light.turn_{{ trigger.to_state.state | lower 

this would only be alright it the state.to_state would be ‘on’.

you might be better of trying to write scenes and call these in the automation.
scene_lights_{to_state.state}}

and create a
scene.lights_on and scene.lights_off

ok
this in new code…

- id: '44444444444444'
  alias: Mensola
  trigger:
    platform: state
    entity_id: switch.plug_158d00010aadf8
  action:
  - data:
      brightness: 1
      entity_id: light.tradfri_driver
    service: light.turn_on
  - data:
  - service: light.turn_off
    entity_id: 
      - light.tradfri_driver_2
      - light.tradfri_bulb
      - light.tradfri_bulb2
      - light.tradfri_bulb3
      - light.tradfri_bulb4
  - data:
      brightness: 3
      entity_id: light.cucina_1
      rgb_color:
      - 255
      - 162
      - 71
    service_template: light.turn_{{ trigger.to_state.state | lower }}
  - data:
      brightness: 3
      entity_id: light.cucina_2
      rgb_color:
      - 255
      - 162
      - 71
    service_template: light.turn_{{ trigger.to_state.state | lower }}
  - data:
      brightness: 3
      entity_id: light.cucina_3
      rgb_color:
      - 255
      - 162
      - 71
    service_template: light.turn_{{ trigger.to_state.state | lower }}
  - data:
      brightness: 3
      entity_id: light.gateway_light_f0b4299a4613
      rgb_color:
      - 255
      - 162
      - 71
    service_template: light.turn_{{ trigger.to_state.state | lower }}

how could I change the code and make it simpler?

ok
this in new code…
i make changes about brightness and united all the state off

- id: '44444444444444'
  alias: Mensola
  trigger:
    platform: state
    entity_id: switch.plug_158d00010aadf8
  action:
  - data:
      brightness: 1
      entity_id: light.tradfri_driver
    service: light.turn_on
  - data:
  - service: light.turn_off
    entity_id: 
      - light.tradfri_driver_2
      - light.tradfri_bulb
      - light.tradfri_bulb2
      - light.tradfri_bulb3
      - light.tradfri_bulb4
  - data:
      brightness: 3
      entity_id: light.cucina_1
      rgb_color:
      - 255
      - 162
      - 71
    service_template: light.turn_{{ trigger.to_state.state | lower }}
  - data:
      brightness: 3
      entity_id: light.cucina_2
      rgb_color:
      - 255
      - 162
      - 71
    service_template: light.turn_{{ trigger.to_state.state | lower }}
  - data:
      brightness: 3
      entity_id: light.cucina_3
      rgb_color:
      - 255
      - 162
      - 71
    service_template: light.turn_{{ trigger.to_state.state | lower }}
  - data:
      brightness: 3
      entity_id: light.gateway_light_f0b4299a4613
      rgb_color:
      - 255
      - 162
      - 71
    service_template: light.turn_{{ trigger.to_state.state | lower }}

how could I change the code and make it simpler?
the lamps light.cucina_2 … you have to turn off if the shelf switch.plug_158d00010aadf8 goes off and on if the shelf switch.plug_158d00010aadf8 turns on but with the colors in rgb

its not only about simpler, it’s mostly about validity and getting things to behave the way you want.

If you use trigger.to_state.state, you should have your services (allow)use that. And not mix on and off like you now have.
If you turn_off a light, you needn’t specify rgb_color and brightness.

that’s why I suggested to use dedicated scenes for this:

- id: '44444444444444'
  alias: Mensola
  trigger:
    platform: state
    entity_id: switch.plug_158d00010aadf8
  condition: template
  value_template: >
    {{ states('switch.plug_158d00010aadf8') in ['on','off'] }}
  action:
    service_template: >
      scene.turn_{{trigger.to_state.state}}
    entity_id: scene.light_turn_{{trigger.to_state.state}}

and then write the scenes:

scene:
  name: light turn on
  entities:
    light.cucina_1:
      state: on
      # transition: 4
      brightness: 3
      rgb_color: [255,162,71]
    light.cucina_2:
      state: on
      # transition: 4
      brightness: 3
      rgb_color: [255,162,71]
etc

and:

scene:
  name: light turn off
  entities:
    light.tradfri_driver_2: off
    light.tradfri_bulb: off
    light.tradfri_bulb2: off
    light.tradfri_bulb3: off
    light.tradfri_bulb4: off

you current automation does this:

on each change of the state of the switch
it turns on the trådfri driver, turns off the bulbs and driver 2 and sets several cucina lights in the state of the switch (trigger.to_state.state)

my example does this:

it checks on each driver state change (can be more than only the state.state, could also be battery, or whatever other attributes that are recorded)
The condition filters out only the ‘on’ and ‘off’ changes
the action switches the respective light scenes ‘on’ or ‘off’

this would be the base for the automation. You can always add or change the scene if you’d like, but that would make it much simpler. to get back to your question… :wink:

1 Like

Ow when I return at home check. Thank you

with this configuration give this error
Invalid config for [automation]: expected a dictionary @ data[‘condition’][0]. Got None
extra keys not allowed @ data[‘value_template’]. Got None
not a valid value for dictionary value @ data[‘action’][0][‘entity_id’]. Got None. (See /config/configuration.yaml, line 359). Please check the docs at Automation - Home Assistant
:frowning:

my bad, sorry, should have been:

- id: '44444444444444'
  alias: Mensola
  trigger:
    platform: state
    entity_id: switch.plug_158d00010aadf8
  condition:  
    condition: template
    value_template: >
      {{ states('switch.plug_158d00010aadf8') in ['on','off'] }}
  action:
    service_template: >
      scene.turn_{{trigger.to_state.state}}
    entity_id: scene.light_turn_{{trigger.to_state.state}}

hmmm, your sure this with this automation?

can you go to dev-template and enter

  condition:  
    condition: template
    value_template: >
      {{ states('switch.plug_158d00010aadf8') in ['on','off'] }}

what does that return?

but we need a ‘data_template’ field in the action:

- id: '44444444444444'
  alias: Mensola
  trigger:
    platform: state
    entity_id: switch.plug_158d00010aadf8
  condition:  
    condition: template
    value_template: >
      {{ states('switch.plug_158d00010aadf8') in ['on','off'] }}
  action:
    service_template: >
      scene.turn_{{trigger.to_state.state}}
    data_template:    
      entity_id: scene.light_turn_{{trigger.to_state.state}}

condition:
condition: template
value_template: >
True

ok that’s cool, so we’re sure the condition evaluates correctly

the trigger action can’t be tested in the dev-template. So I fear you have to restart once more with this new automation…

so i restart home assistant?

yes please, for security, first check configuration

the configuration is ok but the lth light not open

scene:
  - name: light turn on
    entities:
      light.cucina_1:
        state: on
        # transition: 4
        brightness: 3
        rgb_color: [255,162,71]
      light.cucina_2:
        state: on
        # transition: 4
        brightness: 3
        rgb_color: [255,162,71]
      light.cucina_3:
        state: on
        # transition: 4
        brightness: 3
        rgb_color: [255,162,71]
      light.gateway_light_f0b4299a4613:
        state: on
        # transition: 4
        brightness: 3
        rgb_color: [255,162,71]
      light.tradfri_driver:
        state: on
        # transition: 4
        brightness: 1
      light.tradfri_bulb: off
      light.tradfri_bulb2: off
      light.tradfri_bulb3: off
      light.tradfri_bulb4: off
      light.tradfri_driver_2: off
  - name: light turn off
    entities:
      light.tradfri_driver: off
      light.tradfri_driver_2: off
      light.tradfri_bulb: off
      light.tradfri_bulb2: off
      light.tradfri_bulb3: off
      light.tradfri_bulb4: off
      light.cucina_1: off
      light.cucina_2: off
      light.cucina_3: off
      light.gateway_light_f0b4299a4613: off