Combine lights/switches to make multistate switch

I did manage to combine on and off switches in one switch (and i hide original ones) and it works great, instead of 20+ i now have 3x times less. Great :slight_smile:
I used template light, i picked that one first and it worked in few minutes. Thanks!

Now, to figure out dimming…
First i thought I could do it via node-red, to be honest never heard of rest command and it sounded something complicated. Than i took second look, and it looked it couldn’t be easier so i gave it a try.
And it works (not really) from the start! :smiley:
To be exact it works but not as it should, since it always sends brightness level 0 to my Dynalite.
Whatever brightness level i choos it sends 0 and ligths go out.
But, still, hey, great work! :smiley:
Never played with variables, so i presume problem is there.
If i use fixed command (fixed brightness level) it works good, so definately variable and how it makes it. Im positive it is solvable.
Thanks once again!

Grrr… cant get this to work.
Is syntax for this brightness part ok ?

Whatever i do with syntax rest command allways sends zero for brightness.
Still, if i fix bri variable to some value, it will send it perfectly so it sounds rest is doing fine, issues is how to translate brightness 0-255 to 0-100 which my hardware can use.

Please. could someone confirm correct syntax.

Thanks

Can you post the full code for your template light? Makes it easier to troubleshoot.

Edit1: Try perhaps
bri: “{{ (brightness|float /255*100) | int }}”

We’ll get there :upside_down_face:

Edit2: I think I found my mistake. I used ‘data:’ instead of ‘data_template:’. So it should be:

set_level:
  - service: rest_command.rest_brightness
    data_template:
      bri: "{{ (brightness/255*100) | int }}"  # regular brightness is scaled 0-255, but you need a percentage

Does that work?

If you are still using nodered, you can use a “range” node to scale from 0-255 to 0-100.

https://cookbook.nodered.org/basic/map-between-different-number-ranges

Ha, still no go :smiley:
Here it is (different light, not kitchen but living room, same thing)

configuration.yaml

rest_command:
 dynet_level:
   url: "http://192.168.1.25/SetDyNet.cgi?a={{ area }}&c=0&l={{ bri }}"

Plan is to copy/paste to all lights, and change fixed Area: variable to specific Area.

light.yaml (!included in config)

  - platform: template
    lights:
      dnevna_svetlo:
        friendly_name: "Dnevna"
        turn_on:
          service: light.turn_on
          data:
            entity_id: light.dnevna_on
        turn_off:
          service: light.turn_on
          data:
            entity_id: light.dnevna_off
        set_level:
          - service: rest_command.dynet_level
            data_template:
              area: 3
              bri: “{{ (brightness|float /255*100) | int }}”

Well that just looks fine. The brightness calculation is correct in the template editor preview, even without the float.

An alternative instead of rest command is here. Perhaps @tom_l can help you out here with a shell command?

Edit:
@uzelac The quotes don’t look correct to me. You need to use " , but you’re using and .

 bri: “{{ (brightness|float /255*100) | int }}”

Alternative:

        set_level:
          - service: rest_command.dynet_level
            data_template:
              area: 3
              bri: >
                {{ (brightness /255*100) | int }}
1 Like

Oh yeah, that’s it :smile:
Thanks again, works on all lights, now to fix lovelace/cards :smile:

Hi

Need help, hope it is last time on this one :slight_smile:

Been polishing this up and encountered an problem with switching on/off and setting brightness…

So, when i switch on the light, HA will send command to turn on light and Dynalite will turn it on at maximum brightness.
And HA will show brightness at maximum level.
When i turn light off, all is ok, and brightness slider shows zero value.
If i use slider for brightness light will turn on at that level, so that works OK also.
If i set slider to zero, light will turn off and switch will be at OFF position, also OK.
Issues are:

  • if i set brightness to some value which is not maximum and then turn off light, next time i turn it on brightness level will show previous level even though command turned on light to maximum brightness…

  • if i turn on light by setting slider, it will turn on light but HA will show switch at off position.

So, i wonder, is there a way when i turn on light, to tell HA to set it up to maximum just so it will also “think” and show on slider that that light is at maximum after turning on ?

Hope someone understood me :smiley:

Here is a code for one template light:

  - platform: template
    lights:
      dnevna_spot:
        friendly_name: "Dnevna"
        value_template: "{{ is_state('light.dy_dnevna_on', 'on') }}"      
        value_template: "{{ is_state('light.dy_dnevna_off', 'off') }}"
        level_template: "{{ state_attr('light.dnevna','brightness') | int }}"
        turn_on:
          service: light.turn_on
          entity_id: light.dy_dnevna_on
        turn_off:
          service: light.turn_on
          entity_id: light.dy_dnevna_off
        set_level:
          service: rest_command.dynet_area_channel_level
          data_template:
            area: 3
            chan: 10
            bri: >
                {{ (brightness /255*100) | int }}

I’ve never seen this before:

        value_template: "{{ is_state('light.dy_dnevna_on', 'on') }}"      
        value_template: "{{ is_state('light.dy_dnevna_off', 'off') }}"

Two value_templates? I believe Home Assistant will only use the second one (it supersedes the first one). It may also explain the behavior you described.

A single value_template should report the Template Light’s state.

You are right, only one needed. I corrected this

This is how connection to my other “smart home” system (aka Dynalte) is done, via presets, so i have bot off and on lights in HA, When dynalite_some_light_off is on other light (dynalte_some_light_on) is ON. Light is on when ON version is ON… simple :slight_smile:

Not that important, it worked with to templates, and it works the same with just one :slight_smile:

I admit I don’t understand the interaction between the three lights:
dy_deneva_on
dy_dneva_off
dneva

However, have you considered using the following value_template to indicate the template light’s state?

        value_template: "{{ state_attr('light.dnevna','brightness') | int > 0 }}"  

If the light’s brightness is greater than zero then the template evaluates to true and the template light’s state will be on.

Hi

Ill go to english, dnevna = living room.

So, i have connection between dynalite home automation devices (through hassio - mqtt - dynalite bridge Troy Kelly made for home assistant).
When it runs it will create following lights:

  • light.dy_livingroom_off
  • light.dy_livingroom_on

So, not one but two. It’s the way it works, not perfect, and far from finished. But it helps me big time.

So, if i switch on that light the dynalite-mqtt connection will report:

  • light.dy_livingroom_on goes ON
  • light.dy_livingroom_off goes OFF

When i turn that light off, opposite happens:

  • light.dy_livingroom_on goes OFF
  • light.dy_livingroom_off goes ON

To make life easier, i connected these two switches to one, called light.livingroom

Earlier, few people helped me to tie them up, and it works.
When dynalite reports:

  • light.dy_livingroom_on goes OFF
  • light.dy_livingroom_off goes ON

light.livingroom goes ON, and opposite happens.

Problem happens when i dimm these lights, since there is such cappability.

It makes sense but it doesnt work.

If i turn light off, it will go of.
If i raise brightness, phisically stuff works, light will go on at asked brightness but the switch in HA will stay off and icon will look off.

Full code is like this at the moment:

  - platform: template
    lights:
      dnevna_spot:
        friendly_name: "Dnevna"
        value_template: "{{ is_state('light.dy_dnevna_on', 'on') }}"
        value_template: "{{ state_attr('light.dnevna','brightness') | int > 0 }}"  
        turn_on:
          service: light.turn_on
          entity_id: light.dy_dnevna_on
        turn_off:
          service: light.turn_on
          entity_id: light.dy_dnevna_off
        set_level:
          service: rest_command.dynet_area_channel_level
          data_template:
            area: 3
            chan: 10
            bri: >
                {{ (brightness /255*100) | int }}

There should really be only 1 value_template, not two. They should both give you the same result, but you’ll have to experiment which one is working for you. But just enable one at a time!

It seems perhaps the state of light.dy_livingroom_on & _off is not updated when you set the brightness. What is the state of these entities right after you change the brightness level (so when a) light is off and b) you set a brightness level)? Are they updated properly?

What platform is light.dy_dnevna_on, light.dy_dnevna_off and light.dnevna coming from?

Not sure what you mean by platform but all light.dy_somelight_on/off are made with hassio-dynalite component:

It is the way it works, entities are made automaticaly.

Then i “connect” them with light.template in light.SOMELIGHT

They are all off, since I use rest command to set brightness, it is not possible through Dynalite component, it doesnt change any of them.

Can i make an script?
So i call script when i change brightness via rest command, but also to change brightness of light.livingroom which should than show that light is on ?

Except you continue to use two value_templates. Why?

Show me where in the documentation it indicates a Template Light supports more than one value_template.

No, I don’t, i accepted your comment, here is latest version:

  - platform: template
    lights:
      dnevna_spot:
        friendly_name: "Dnevna"
        value_template: "{{ is_state('light.dy_dnevna_on', 'on') }}"
        level_template: "{{ state_attr('light.dy_dnevna_on','brightness') |int }}"
        turn_on:
          service: light.turn_on
          entity_id: light.dy_dnevna_on
          data:
            brightness: 255
        turn_off:
          service: light.turn_on
          entity_id: light.dy_dnevna_off
#          data:
#            brightness_pct: 0
        set_level:
          service: rest_command.dynet_area_channel_level
          data_template:
            area: 3
            chan: 10
            bri: >
                {{ (brightness /255*100) | int }}
  • dnevna = living room

Huh, made it work, nor nice not looks good, but works.
Following recommendation for similar issue, i made dummy template light (not sure does it have to be template, since it does not even behave like light…) and two triggers:
To me it look i need to have only variable with intreger numeric value… not sure… :confused:

  - platform: template
    lights:
      dnevna_dummy:
        friendly_name: "Dnevna dummy"
        turn_on:
          service: input_boolean.turn_on
          entity_id: input_boolean.dnevna_dummy
        turn_off:
          service: input_boolean.turn_off
          entity_id: input_boolean.dnevna_dummy
        set_level:
          service: input_number.set_value
          data_template:
            value: "{{ brightness }}"
            entity_id: input_number.dnevna_dummy

        
  - platform: template
    lights:
      dnevna_spot:
        friendly_name: "Dnevna"
        value_template: "{{ states('input_number.dnevna_dummy')|int > 0 }}"
        level_template: "{{ states('input_number.dnevna_dummy')|int }}"
        turn_on:
          - service: light.turn_on
            data_template: 
              entity_id: light.dy_dnevna_on
          - service: input_number.set_value
            data_template:
              entity_id: input_number.dnevna_dummy
              value: 255
        turn_off:
          - service: light.turn_on
            data_template: 
              entity_id: light.dy_dnevna_off
          - service: input_number.set_value
            data_template:
              entity_id: input_number.dnevna_dummy
              value: 0
        set_level:
          - service: rest_command.dynet_area_channel_level
            data_template:
              area: 3
              chan: 10
              bri: >
                  {{ (brightness /255*100) | int }}
          - service: rest_command.dynet_area_channel_level
            data_template:
              area: 3
              chan: 5
              bri: >
                  {{ (brightness /255*100) | int }}
          - service: input_number.set_value
            data_template:
              entity_id: input_number.dnevna_dummy
              value: "{{ brightness }}"

Two triggers are here to change input_boolean.dnevna value when i switch light on/off directly on Dynalite keypads.

  alias: Dnevna dummy ON
  trigger:
  - entity_id: light.dy_dnevna_off
    from: 'on'
    platform: state
    to: 'off'
  condition: []
  action:
  - data: 
      entity_id: input_number.dnevna_dummy
      value: 255
    service: input_number.set_value

- id: '1569109156901'
  alias: Dnevna dummy OFF
  trigger:
  - entity_id: light.dy_dnevna_on
    from: 'on'
    platform: state
    to: 'off'
  condition: []
  action:
  - data: 
      entity_id: input_number.dnevna_dummy
      value: 0
    service: input_number.set_value

Now, need to find a way to make this in some script which will use variables. I have 10 lights in house, plan some more, and don’t consider making 20+ triggers and 10+ dummy lights.

Maybe someone will have a smarter solution :slight_smile:
Thanks