How to make devices mirror each other?

I’m looking to make a handful of devices mimic one another. I just integrated Smartthings but am using nothing but simulated devices on that platform so my family can use that app as their front end. I didn’t want to force them to install anything new and they are stuck in their ways.

I have different types of simulated devices from switches to colored bulbs and simulated garage doors to simulated locks. I want to be sure if any attribute of a specific device is changed on one platform, its mirrored on the other platform. I can mirror the on/off state with Node-Red ezpz but mirroring things like brightness and color temperature seems to be more difficult. I guess it can be done with A LOT of templating but I’m still not entirely sure accomplish it. In Smartthings you can make the trigger be “on device changes” and make the action be “set device B brightness to device A brightness”. Hopefully, there could be something that easy in Hass too!

Thank you!

The equivalent trigger would be a state trigger without specifying a to/from state (so it reacts to all changes of state and attributes):

trigger:
  platform: state
  entity_id: light.my_device_a
action:

Then copy the wanted state and attributes from device A to B in the actions.

What he said ^^

:rofl: just getting my own back (but I do agree ! )

@tom_l that’s great news that not specifying a state will trigger on all changes, exactly what I was looking for! But, making templates is always a challenge for me and it takes a lot of trial and error. This is what I’ve got…

  trigger:
  - entity_id: light.st_living_room_fan_lights
    platform: state
  condition: []
  action:
  - data_template:
      brightness: >
        {{ state_attr('light.living_room_fan_lights', 'brightness') }}
      color_temp: >
        {{ state_attr('light.living_room_fan_lights', 'color_temp') }}
      hs_color: >
        {{ state_attr('light.living_room_fan_lights', 'color_temp') }}
      state: >
        {{ states('light.living_room_fan_lights') }}
    entity_id: light.st_living_room_fan_lights
    service: light.turn_on

I feel like this should be correct, but I get nothing. I’m sure I’m missing a comma or something somewhere. Also, is this the correct path for matching the on/off states? Thanks again!

Template looks good except you did not specify ‘device B’ for the one you want to change at the end of your template (you used ‘device A’)

    entity_id: light.st_living_room_fan_lights  # should be the other entity you want to change.
    service: light.turn_on

There’s another problem though.

You need a service template as well otherwise you second light will always turn on.

      state: >
        {{ states('light.living_room_fan_lights') }}
    entity_id: light.other light
    service: light.turn_on

You’re right. Easy stuff like that I miss. Still not working, though. And I fixed the hs_color template, I had color_temp in there again. I even took out the state template just in case that was causing errors.

Tom you’re using (copied again) a turn on, when the state may be turned off ???

Ie use the state trigger state

That’s not a solution. That’s pointing out the problem. I’m just about to leave and don’t have time to write the service template as it should be.

I added a service template too.

  trigger:
  - entity_id: light.st_living_room_fan_lights
    platform: state
  condition: []
  action:
  - data_template:
      brightness: >
        '{{ state_attr('light.living_room_fan_lights', 'brightness') }}'
      color_temp: >
        '{{ state_attr('light.living_room_fan_lights', 'color_temp') }}'
      hs_color: >
        '{{ state_attr('light.living_room_fan_lights', 'hs_temp') }}'
    entity_id: light.living_room_fan_lights
    service_template: >
      {% if is_state('light.st_living_room_fan_lights', 'on') -%}
        light.turn_on
      {%- else -%}
        light.turn_off
      {%- endif %}

Again, I think thats right. Probably missing something easy. But I’m not getting anything. Either way, practicing templates is good for me and hopefully one day I’ll be able to create one without having to copy-paste from this forum.

Derek, try : -

      state: > 
        {{ states('light.living_room_fan_lights') }}
    entity_id: light.other light
    service_template: light.turn_{{trigger.to_state.state}} 

Be careful with the cutting/pasting, I’m doing this from a phone

Again they all seem to be device A

Mutt, I tested both mine and your service templates and they work when I removed all the data templates. So, I guess the problem is in the data template somewhere and it makes the whole automation hang.

UPDATE

Ok, I found this on defining attributes and inserted it in my automation…

  trigger:
  - entity_id: light.st_living_room_fan_lights
    platform: state
  condition: []
  action:
    entity_id: light.green_room_lights
    service_template: >
      {% if is_state('light.st_living_room_fan_lights', 'on') -%}
        light.turn_on
      {%- else -%}
        light.turn_off
      {%- endif %}
    data_template:
      brightness: >
        {% if states.light.st_living_room_fan_lights %}
          {{ state_attr('light.st_living_room_fan_lights', 'brightness') }}
        {% else %}
          none
        {% endif %}

I can now mirror brightness but not on/off state. If I remove the brightness data template altogether I can mirror state changes. I used @Mutt’s trigger.to_state.state as well as the service template and got the same result. So something somewhere is causing the automation to hang when there is data. Or maybe it just doesn’t like having data on a turn_off service. Thank you guys for your help! If you see anything that I may be doing wrong I would greatly appreciate it. As for now though, I think I’l just make a separate automation for on and off.

Yeah the light turn off service dose not take extra attributes. You might be better off with two automations, one for on, one for off.

  trigger:
    platform: state
    entity_id: light.st_living_room_fan_lights
    to: 'on'
  action:
    service: light.turn_on
    entity_id: light.green_room_lights
    data_template:
      brightness: >
          {{ state_attr('light.st_living_room_fan_lights', 'brightness') }}

  trigger:
    platform: state
    entity_id: light.st_living_room_fan_lights
    to: 'off'
  action:
    service: light.turn_off
    entity_id: light.green_room_lights

I think you could also do this in one automation with a light.turn_on service for all outcomes and a templated brightness of zero for the outcome where you want it to turn off, and the appropriate brightness when you want to turn it on.

1 Like

That’s a good idea.

  trigger:
  - platform: state
    entity_id: light.st_living_room_fan_lights
  action:
    service: light.turn_on
    entity_id: light.green_room_lights
    data_template:
      brightness: >
        {% if trigger.to_state == 'on' %}
          {{ state_attr('light.st_living_room_fan_lights', 'brightness') }}
        {% else %}
          0
        {% endif %}

Yep, this is current ‘best of breed’.

I knew the brightness to 0 thing but just never thought of applying it here, good call Marc

Hey guys. I implemented the 0 brightness into my automation but it still won’t turn the lights off. It will turn them on from an off state, though.

    data_template:
      brightness: >
        {% if trigger.to_state != 'off' %}
          {{ state_attr('light.st_living_room_fan_lights', 'brightness') }}
        {% else %}
          0
        {% endif %}

i switched == on to != off because it was turning the light off on any brightness change so at least we know the brightness of 0 acts as an “off” command.

In case anyone needs this in the future, the following handles on and off correctly.

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

3 Likes

One easy way:

- alias: Test me!
  trigger:
  - platform: state
    entity_id: binary_sensor.alarm_pir_3, switch.alarm_relay_1
  action:
  - service_template: switch.turn_{{ trigger.to_state.state }}
    entity_id: switch.sonoff_s20_01_relay

Whatever those two entities do, sonoff_s20_01_relay does too!

2 Likes