Terrific way to use a Shelly Dimmer 2 in a thorny situation

Here in Australia I used these switches:
https://www.clipsal.com/products/power-points-switches/pro-series/switch-mechanism-3-position-bell-press-return-10a-250v-p49mbpr?itemno=P49MBPR-XW&tab-document-1=0

I have just started my journey with these little treasures and am wondering when you actually import them into HA?

Do you do the initial config with ON/OFF url confirming which way the switch goes?
OR
It doesn’t matter?

Thanks

I have combined the script for the shelly 2 dimmer control into one single automation, which makes the HA setup much cleaner. I am intending to make a blueprint for it eventually containing all the docs, etc and credit of course :slight_smile: in the meantime here’s my update:

alias: dining room light
description: >-
  Turn on/off then increase/decrease the brightness of the light using shelly
  dimmer inputs
trigger:
  - platform: state
    entity_id:
      - binary_sensor.dining_room_light_channel_1_input
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 0.25
  - platform: state
    entity_id:
      - binary_sensor.dining_room_light_channel_2_input
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 1
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.dining_room_light_channel_1_input
            state: "on"
        sequence:
          - repeat:
              while:
                - condition: state
                  entity_id: light.dining_room_light
                  state: "off"
              sequence:
                - service: light.turn_on
                  data: {}
                  target:
                    entity_id: light.dining_room_light
            alias: Turn on light
          - alias: Increase light
            repeat:
              while:
                - condition: state
                  entity_id: binary_sensor.dining_room_light_channel_1_input
                  state: "on"
              sequence:
                - service: light.turn_on
                  data:
                    brightness_step: 10
                    transition: 1
                  target:
                    entity_id: light.dining_room_light
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 250
      - conditions:
          - condition: state
            entity_id: binary_sensor.dining_room_light_channel_2_input
            state: "on"
        sequence:
          - repeat:
              while:
                - condition: state
                  entity_id: binary_sensor.dining_room_light_channel_2_input
                  state: "on"
              sequence:
                - service: light.turn_on
                  data:
                    brightness_step: -10
                    transition: 1
                  target:
                    entity_id: light.dining_room_light
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 250
    default:
      - stop: Nothing to do
mode: single

4 Likes

This is great, but say I have ten (10) dimmers, is there a way to pass the name of the light switch to the script so that I don’t have to have 10 versions of the same script and just use the variable?

Yes… you are in luck - it took me a while because it is documented very poorly if anywhere… but how I figured it out is shown below.

I have not done whaty you are doing as I have it only for one dimmer, but as an example I do have many autromations passing parameters to the same script -

So that you understand my paradigm in my setup my crappy motion detectors (that is why I do it this way to overcome that) - call a script to (re)start a related timer. The Script does nothing if the automation is disabled otherwise it starts the timer with a duration that I have set by a slider in my dashboard. All of those entities are passed to the script which is called from all over the place (This way I was able to remove a lot of redundant code which is what you are trying to do.) When the timer (re)starts it turns the light on (if not already on) and when the timer finishes it turns the light off.

So, for a specific light, this is when motion is sensed - (note the action section which calls a script with parameters):

alias: Den Motion Detected (Lamp By Closet Automation)
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 5ef8f43f23c1d09910584c8c6a3cb29c
    entity_id: binary_sensor.den_motion_sensor_motion
    domain: binary_sensor
condition: []
action:
  - service: script.motion_detected
    data:
      enablement_input_selector_entity: input_select.den_automation_lamp_by_closet_is
      timer_duration_entity: input_number.den_adjust_couch_lamp_by_closet_timer
      timer_entity: timer.den_couch_lamp_by_closet_timer
mode: parallel
max: 1000

And here is the script that it calls. I call this from all over the place.

alias: Motion Detected
sequence:
  - condition: template
    value_template: "{{ is_state(enablement_input_selector_entity,'Enabled') }}"
  - service: timer.start
    data:
      duration: "{{ (states(timer_duration_entity)|int*60) }}"
    target:
      entity_id: "{{ timer_entity }}"
mode: parallel
max: 1000
icon: mdi:run-fast

I got this code working now for multiple lights. Simply add your appropriate binary_sensor(s) and the rest just works.

For the below code to work the way I wanted, I have my UP button set as Channel 1 and my DOWN button set as Channel 2.

alias: Shelly Dimming
description: This allows the shelly to dim the lights
# I have Channel 1 set as UP and Channel 2 and DOWN
trigger:
# Push button up for 1/4 second to brighten
  - platform: state
    entity_id:
      - binary_sensor.waiting_channel_1_input
      - binary_sensor.hallway_channel_1_input
      - binary_sensor.mud_room_channel_1_input
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 0.25
# Push button down for 1 second to dim
  - platform: state
    entity_id:
      - binary_sensor.waiting_channel_2_input
      - binary_sensor.hallway_channel_2_input
      - binary_sensor.mud_room_channel_2_input
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 1
condition: []
action:
# Set a variable for the light we are going to adjust
  - variables:
      light_entity_id: >-
        light.{{
        trigger.to_state.object_id|replace('_channel_1_input','')|replace('_channel_2_input','')
        }}
# Before we do anything need to turn the light on if it is off
  - alias: Turn on light
    repeat:
      while:
        - condition: template
          value_template: >-
            {{ is_state('light.' ~
            trigger.to_state.object_id|replace('_channel_1_input','')|replace('_channel_2_input',''),
            'off') }}
      sequence:
        - service: light.turn_on
          data: {}
          target:
            entity_id: "{{ light_entity_id }}"
  - choose:
# Test to check we are holding the up button
      - conditions:
          - condition: template
            value_template: "{{ ('channel_1' in trigger.entity_id) == true }}"
          - condition: template
            value_template: "{{ is_state (trigger.entity_id, 'on') }}"
        sequence:
          - alias: Increase light
            repeat:
              while: # Whilst holding the up button
                - condition: template
                  value_template: "{{ is_state (trigger.entity_id, 'on') }}"
              sequence:
                - service: light.turn_on
                  data:
                    brightness_step: 10
                    transition: 1
                  target:
                    entity_id: "{{ light_entity_id }}"
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 250
# Test to check we are holding the down button
      - conditions:
          - condition: template
            value_template: "{{ ('channel_2' in trigger.entity_id) == true }}"
          - condition: template
            value_template: "{{ is_state (trigger.entity_id, 'on') }}"
        sequence:
          - repeat:
              while: # Whilst holding the down button
                - condition: template
                  value_template: "{{ is_state (trigger.entity_id, 'on') }}"
              sequence:
                - service: light.turn_on
                  data:
                    brightness_step: -10
                    transition: 1
                  target:
                    entity_id: "{{ light_entity_id }}"
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 250
# Otherwise do nothing.
    default:
      - stop: Nothing to do
mode: single

2 Likes

Best thing ever; so glad you posted this solution mate!

I’m not sure I get why some of you decided to set the dim down conditions to 1 second as opposed to 1/4, nor how you turn off the lights except by diming all the way down?

I have a Circadian lighting custom component that I use to modulate brightness and color temp depending on the time of day that syncs all my smart lights. So in my case, the trigger of manually diming will need to override this, which is not hard to do through a binary toggle.

I will try to add a off function with a push of a button for channel 2 lower than the condition to trigger which resets the Circadian Lighting mechanism.

This post was a godsend and quite possibly saved my marriage, cheers!

With the OFF, if you look at the earlier posts you will see that is simply part of the switch configuration.

Following the set up for the URLs a quick press up turns the light on and a quick press down turns it off. This is also in the event that HA is not working properly you can still turn the light on and off.

With the timings, especially the dimming, yeah I wasn’t sure about the 1 second delay, but I left it that way till I check the WAF and see what the verdict is. The best thing about HA is you can simply change the value to something that works for you.

1 Like

Just the short press down and release will turnjh it off, then a short press up will turn it on - to the brightness it had before

1 Like

Thanks for the clarifications, that trick is neat.

Unfortunately, my implementation is different: I am using a Shelly i3.

That switch doesn’t have a binary sensor when set to momentary switch.
So I hacked it by configuring it as a Toggle Switch wired to a physical momentary switch.
When configured this way, a binary sensor is available and triggered when I press the physical switch, which works with this automation.

However, the URL actions are now limited to Switch On or Off which doesn’t seem to play nice with this idea unfortunately.

Using a Dimmer 2 just for the detached switch seemed a waste to me as the i3 was pretty much all I needed. If the WAF degrades too much, I might have to reconsider.

You aren’t able to use long press to incrementally change brightness? Bummer!

Nope the brightness change works fine :slight_smile: It’s the URL implementation to switch on/off that can’t be implemented. So long pressing either button will turn the lights on, but the only way to switch off at the moment is to dim all the way down.

If acceptable maybe increase the speed at which the lights dim or brighten (or by larger increments like 25% instead of 10% each time…)?

Sorry, no, you miht be able to find it at Home Depot or something like that but thyese levitons wil outlast your house and you and me. They are built to last forever. Built like a tank.

This is the end result. Switch on the left is the dimmer! (For people who have the direct link to this post, scroll up for my original post and all the configuration details.)

and the wiring (actually two shelly’s behind this faceplate (a Shelly Dimmer 2 and a Shelly 1L) for two different things):

There are a few button actions missing in the original post. With just the on/off commands there is no way to control the brightness with the switch. So, add the following to the Button Actions just like previously:

BUTTON 1 SWITCHED OFF URL = http://localhost/light/0?dim=stop
BUTTON 2 SWITCHED OFF URL = http://localhost/light/0?dim=stop
BUTTON 1 LONG PRESS URL = http://localhost/light/0?dim=up&step=1
BUTTON 2 LONG PRESS URL = http://localhost/light/0?dim=down&step=1

The Long Press commands increase and decrease the brightness until another command is received, that is why we need to add the dim stop commands to the switch released actions. You can change the Step value to modify the rate of brightness change to suit your taste.

These additions will allow full control; on/off, bright/dim from the wall switch regardless of Internet availability.

For reference, here’s a guide to Shelly Webhooks - Webhooks / HTTP(S) requests – The unofficial Shelly guide!

After I created the previous post, I updated the firmware to 20230913-114008 and it appears that Shelly has changed the operation of dim up and down URL Actions. They no longer repeat. So, you have to trigger the switch multiple times to ramp up/down.

Because of this, I rearranged my URL actions to the following:

BUTTON 1 LONG PRESS URL = http://localhost/light/0?turn=on
BUTTON 2 LONG PRESS URL = http://localhost/light/0?turn=off
BUTTON 1 SHORT PRESS URL = http://localhost/light/0?dim=up&step=10
BUTTON 2 SHORT PRESS URL = http://localhost/light/0?dim=down&step=10

And, I removed the switched off actions.

Maybe this is fine for you if you are using the code written by @girkers ? My orginal one still works fine (long press automations for dimming and brightening) and the firmware updates did not affect the dimming or brightening either… ?

Putting the dimming on the switch instead of HA, makes the light fully functional without HomeAssistant. I just took your original goal of “Still be able to at least turn the light on and off even if HA is dead” one step further and enabled dimming from the switch as well.

There is no need for your dimming code in HA, but you still have full control over on/off and dimming within HA.

But you said in your previous post that holding the switch will not continuously brighten or dim the lights, “So, you have to trigger the switch multiple times…” - bummer, it would be great to have it work properly without ha at all…

Hey girkers,

this automation looks great.

I am not so into yaml-coding, but I think, this is quite close to what I am looking for.

I want to combine a motion sensor and a classic button-switch (only 1 button, which can either be pressed long or short).

When motion is detcted, the lights should turn on and switch off 1 minute after movement detection → that is working

When I press the button short, the light should toggle and the running “shutdown timer” of the movement automation should be deleted or the automation should stop.

When I press the button long, it should dim the light. For example it should dim up to 100% at the first long press and dim down to 0% at the second long press.
Or another possibility could be to dim up, when the light is below 50% or dim down, if the lights are above 50% brightness.

Do you have any hints for me how I could solve this?