Combine lights/switches to make multistate switch

Hi

Pretty much noob fort few months now with hass.io.
It works great, but would like to make it bit easier because of specifics of my installation.

For lights I use Philips Dynalite relays and dimmers, and there is specific integration for it, still in hard beta so bit limited.

What i have at the moment is that every state of every light is represented with two or three switches in HA.

For example, i have Kitchen light single bulb connected to dimmer.
Because of the way it is integrated i have three light/switches for this one bulb:

image
(Kuhinja = serbian/croatian for Kitchen :))

If i turn on Kuhinja On, Kuhinja Dimm and Kuhinja Off will turn Off.
Also, if i turn on, Kuhinja Off, other two will turn off.
It works, but makes it more compliated than needed, also making list of my lights 3x times longer.
Beside, All lights OFF command in HA makes a mess, some lights turn on, some off some get dimmed… terrible :slight_smile:

How can i make one “multistate” switch for one lights something like:
Kitchen light (OFF) (Dimmed) (ON)
Where i choose state for that light and single states are hidden ?

Thanks!

The problem is that a switch has two states (on / off) not three (on / dim / off).

You could use a template switch to combine your on and off switches into one but the dim switch would still be seperate.

Or you could define an input number (slider) from 0 to 2 with a step of 1. Then automate your switches depending on the state of the slider:

Slider = 0
Turn on switch Kuhinja Off

Slider = 1
Turn on switch Kuhinja Dimm

Slider = 2
Turn on switch Kuhinja On


Input number:

kuhinja:
  name: Kuhinja 
  min: 0
  max: 2
  step: 1
  icon: mdi:lightbulb

Automation:

- id: kuhinja
  alias: 'Kuhinja'
  trigger:
    platform: state
    entity_id: input_number.kuhinja
  action:
  - service: switch.turn_on
    data_template:
      entity_id: >
        {% if states('input_number.kuhinja') | int == 1 %}
          switch.kuhinja_dimm
        {% elif states('input_number.kuhinja') | int == 2 %}
          switch.kuhinja_on
        {% else %}
          switch.kuhinja_off
        {% endif %}

I know nothing about the particular hardware, but if you have a dimable light, then it seems more appropriate that the integration appear as a light type entity, that has the notion of a brightness/dim level?

It seems that any other way lies madness…

1 Like

Looks like presets are the only option in the custom component that talks to this hardware. No direct dimming control:

Perhaps a template light would allow you to catch this situation. Just check it out in the docs…

It has been suggested.

A template switch is different from a template light right? That’s why I suggested it. Just figuring out the template light myself now, hoping it will also be dimmable.

Edit: here is my functioning template light:

- platform: template
    lights:
      keukentafel_auto:
        friendly_name: "Lamp keukentafel auto"
        level_template: "{{ state_attr('light.keukentafel','brightness') | int }}"
        value_template: "{{ states('light.keukentafel') }}"
        turn_on:
          - service: light.turn_on
            data_template:
              entity_id: light.keukentafel
              brightness_pct: "{{ states('sensor.brightness') }}"
              transition: 3
        turn_off:
          service: light.turn_off
          entity_id: light.keukentafel
        set_level:
          service: light.turn_on
          data_template:
            entity_id: light.keukentafel
            brightness: "{{ brightness | int }}"

@uzelac: My suggestion would be to figure out the value templates for on, off and brightness level. Then integrate them into a template light. I don’t know much about your particular hardware though, so you’ll have to figure out if it’s working for you by yourself :wink:

1 Like

Wow, thanks a lot, seems like some of this could work :).
It would be great to have one switch instead of 2/3, even if i waste dimmability. I have full control through original keypads, so simple on/off is still usable.

Of course, it would be much better to have full dimming control, I guess it is possible, but not through this custom component, at least until Troy makes it better :).

Since i have this gateway part of Dynalite installation, i can send something what i would call http request… not sure if this is correct name.
For example, if i “visit” this page through my web browser (192.168.1.25 is my Dynalite gateway):

http://192.168.1.25/SetDyNet.cgi?a=5&c=1&l=100
It will set my lights in Kitchen (Area 5) Main light (Channel 1) to Full power (Level 100)

So, can i make slider which will I program to send level I choose (X) via this HTTP request:
http://192.168.1.25/SetDyNet.cgi?a=5&c=1&l=X

How can i make… dimmable light entity ??? to have slider with 100 positions which will send this http request ?
I tried some of this with node-red, but without success … too much noob :smiley:

Interesting case :slight_smile:
I think it could be done with a REST command, example is found here.
I think you don’t actually need a separate slider. A template light contains a brightness slider by default, as long as you include the ‘set_level’ part. In your set_level part, I imagine you would call the REST command.

In your configuration.yaml:

rest_command:
  rest_brightness:
    url: "http://192.168.1.25/SetDyNet.cgi?a=5&c=1&l={{ bri }}"

Part of your template light:

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

Ofcourse I haven’t tested this… I skipped the script (as shown in the linked example) but perhaps you need a script.

1 Like

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.