Copy brightness from one entity to another

After switching to MQTT from Home Bridge in the past few days, I’m looking for help on setting the brightness level of one entity (an LED) with the brightness setting on another entity (a Clipsal switch)

This is the devtools section from the clipsal switch

In the configuration.yaml I’ve specified

automation_clipsal: !include clipsalautomations.yaml 

This code below doesn’t work, but thinking this will literally only copy the state anyway?

  alias: SW2-LED
  description: ''
  trigger:
  - platform: state
    entity_id: light.mq_ensuite_sw2
  condition: []
  action:
  - condition: state
    entity_id: light.mq_ensuite_sw2
    attribute: brightness
    state: brightness
  - service: light.turn_on
    target:
      entity_id: light.shower_light
      data: null
      brightness: '{{ trigger.to_state.state }}'

I also say this code from @matcha (I’ve tweaked it) but again no life. What am I missing, it has to be something silly!

    trigger:
      - platform: state
        entity_id: light.mq_ensuite_sw2
    action:
      service: light.turn_on
      entity_id: light.shower_light
      data_template:
        brightness: >
          {{ state_attr('light.mq_ensuite_sw2', 'brightness') | default(0, true) }}

Try this:

  alias: SW2-LED
  trigger:
  - platform: state
    entity_id: light.mq_ensuite_sw2
    attribute: brightness
  action:
  - service: light.turn_on
    target:
      entity_id: light.shower_light
    data:
      brightness: '{{ state_attr('light.mq_ensuite_sw2', 'brightness')|int(0) }}'

You cant use trigger.to_state.state as you don’t want the light state (on/off), you want the brightness attribute value.

1 Like

When trying to save I get:

can not read an implicit mapping pair; a colon is missed at line 10, column 82:
     ... e_sw2', 'brightness')|int(0) }}'

I’m still struggling to get my head around YAML formatting, could be that!

I’ve tweaked it slightly using double quotes in the last line and it accepted that. I’ve put this code into the default empty scripts.yaml. Is that the right place to test? I keep moving this from the config file to automations (where it asks for an ID) and now I’m trying this empty scripts.yaml file

Even here tho, nothing.

  alias: SW2-LED
  trigger:
  - platform: state
    entity_id: light.mq_ensuite_sw2
    attribute: brightness
  action:
  - service: light.turn_on
    target:
      entity_id: light.shower_light
      brightness: "{{ state_attr('light.mq_ensuite_sw2','brightness') |int(0) }}"
      

It is an automation not a script.

You need it to be triggered when the brightness of the ensuite light changes. Scripts don’t have triggers.

I’ve tweaked the configuration file to be:

automation: !include_dir_merge_list automation/

And moved the default automation.yaml there (these UI based simple automations still work) and I have a new file with the above script. Still no life.

Is there a way to debug this to see where it’s failing, or even if it’s not being triggered?
This is all I have in this new file

I’m going to admit, I’m not getting anywhere having spent days trying everything I can to get this to work. It’s exasperating.

-I don’t know if anything in my yaml is being triggered
-I don’t know if the yaml is properly formatted
-I don’t know if the code is right
-I don’t know if the attributes are being picked up
-I don’t know of the attributes to the target are being sent
-I don’t know how to debug any of it

There are a lot of permutations of things to go wrong, and it’s literally the only thing I moved to HA to do, and that’s to link two disparate control systems together. Not sure how to proceed to be honest. Hands up who offers a paid for service, I’m stumped.

This is my current piece of code.
-The file is in clipsalautomations.yaml in a directory called automation.
-The main configuration.yaml says this, and the simple UI based automations are working from there

automation: !include_dir_merge_list automation/
- id: sw21
  alias: SW2-LED
  trigger:
  - entity_id: light.mq_ensuite_sw2
    platform: state
    attribute: brightness
  action:
  - service: light.turn_on
    entity_id: light.shower_light
    data_template:
    brightness: "{{ state_attr('light.mq_ensuite_sw2','brightness') |int(0) }}"
    trace:
    stored_traces: 1
  1. Go to Configuration > Automations
  2. Do you see your SW2-LED automation in the list?
  3. If you don’t, it was never loaded. Check Configuration > Logs for error messages.
  4. If you do, does it have a date and time displayed indicating when it triggered recently or does it say Never?
  5. If it has a time, but the shower light never turned on, there’s something wrong in the automation’s action. Check the automation’s trace (refer to Troubleshooting Automations)
  6. If it has no time, it never triggered.

You should know that your automation’s service call is incorrectly indented. Home Assistant probably refused to load it but you didn’t inspect the log for errors. Always execute Configuration > Server Controls > Check Configuration after creating/modifying an automation with a text editor.

Change it to this and remove the references to trace.

  action:
  - service: light.turn_on
    target:
      entity_id: light.shower_light
    data:
      brightness: "{{ state_attr('light.mq_ensuite_sw2','brightness') |int(0) }}"
1 Like

Not all heroes wear capes, some reside on this board. It’s now working with the change, thankyou!!!

I did look in the logs but found nothing which was frustrating, but now SW2 appears in the list. This gives me the springboard to go and create a whole slew of automations from my existing hard wired clipsal system into anything :slight_smile:

Thanks again, @123 and @tom_l !

1 Like