Setting Shelly dimmer switches to max brightness on press

Hi all, somewhat of a newb here with NodeRED. I am trying to have my Shelly switches automatically adjust to max brightness when their physical switch is pressed. I am trying to do this in NodeRED (but if anyone knows an easier alternative, I’m definitely open to trying it), and have confirmed this is possible with this flow:

[{"id":"e9995e8e98de0842","type":"server-events","z":"9101afde4c3e71c9","name":"","server":"790d432d.fb71ac","version":2,"eventType":"shelly.click","exposeToHomeAssistant":false,"eventData":"","haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"}],"event_type":"","x":110,"y":1440,"wires":[["dca40aad83b99652"]]},{"id":"dca40aad83b99652","type":"api-current-state","z":"9101afde4c3e71c9","name":"","server":"790d432d.fb71ac","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.office_light","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"payload.event.device","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":390,"y":1440,"wires":[[],["9671249574d77665"]]},{"id":"9671249574d77665","type":"delay","z":"9101afde4c3e71c9","name":"","pauseType":"delay","timeout":"250","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":670,"y":1440,"wires":[["c5a7cf4a8e7907b5"]]},{"id":"c5a7cf4a8e7907b5","type":"api-call-service","z":"9101afde4c3e71c9","name":"Light Brightness Max","server":"790d432d.fb71ac","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":["5a3f823af3ad1c7e0c59dde92295124c"],"entityId":[],"data":"{\"brightness_step_pct\":100}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":880,"y":1440,"wires":[[]]},{"id":"790d432d.fb71ac","type":"server","name":"Home Assistant","addon":true}]

The issue here is “events: shelly.click” only returns the device id and name, not the entity ID, so I am unable to pass the specific entity into the current state and call service nodes.

As a result, the above flow will only affect the hard-coded “light.office_light” and not necessarily the light that was actually phiscally switched on/off.

Is there is a way to convert a “device_id” or “device” to an “entity_id”? Is this even the right way of trying to achieve what I want? I would appreciate any assistance :slight_smile:

Okay, I have found something that works, but it requires me to manually add each light.

[{"id":"599f5165aca12c75","type":"api-call-service","z":"9101afde4c3e71c9","name":"Light Brightness Max","server":"790d432d.fb71ac","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["{{payload}}"],"data":"{\"brightness_step_pct\":100}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1040,"y":1220,"wires":[[]]},{"id":"5564a5a540d977d0","type":"server-events","z":"9101afde4c3e71c9","name":"","server":"790d432d.fb71ac","version":2,"eventType":"shelly.click","exposeToHomeAssistant":false,"eventData":"","haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"}],"event_type":"","x":110,"y":1280,"wires":[["0e1958966035e2f1"]]},{"id":"0e1958966035e2f1","type":"switch","z":"9101afde4c3e71c9","name":"","property":"payload.event.device","propertyType":"msg","rules":[{"t":"eq","v":"shellydimmer2-3C6105E479B0","vt":"str"},{"t":"eq","v":"shellydimmer2-C45BBE570BBD","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":310,"y":1280,"wires":[["0b3844971d08c288"],["e2501e6a2ccf7bf4"]]},{"id":"0b3844971d08c288","type":"api-current-state","z":"9101afde4c3e71c9","name":"","server":"790d432d.fb71ac","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.office_light","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"triggerId"}],"for":"","forType":"num","forUnits":"milliseconds","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":530,"y":1240,"wires":[[],["c8691aa62da1904e"]]},{"id":"c8691aa62da1904e","type":"delay","z":"9101afde4c3e71c9","name":"","pauseType":"delay","timeout":"250","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":810,"y":1240,"wires":[["599f5165aca12c75"]]},{"id":"e2501e6a2ccf7bf4","type":"api-current-state","z":"9101afde4c3e71c9","name":"","server":"790d432d.fb71ac","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.lounge_out","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"triggerId"}],"for":"","forType":"num","forUnits":"milliseconds","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":530,"y":1300,"wires":[[],["c8691aa62da1904e"]]},{"id":"790d432d.fb71ac","type":"server","name":"Home Assistant","addon":true}]

Is there a way I can simplify this by just reading from the event node?

I know that in an automation there are variables exposed temporarily (they are not even saved in history) which I use reliably to determine whether or not a switch was turned on or off manually (physically - rather than through an HA automation). This works 100% reliably but when you look at the automation with the visual editor it does not properly show the condition set within the automation - as in the first picture below).

In my use case I had a problem where, if people turn off a light and then walk out of a room, my motion sensors would immediately turn the light back on becuase they sense the motion within the room while the person is walking out of it! So to resolve that issue any time a light switch is ONLY - MANUALLY - turned off, it would update a helper variable I would use to which I always then used in other automations as a condition to check to not turn the light back on if it had been turned off MANUALLY within the last 5 minutes. So, here is my code to check to see if it was turned off manually when it was turned off -

(Note the above does not show the condition for testing whether or not it was MANUALLY (physically) turned off - but when you look at it in the YAML editor instead, you see the logic:

alias: >-
  Dining Room Chandelier Turned Off -> If Done Manually -> Set Last Manual Off
  Timestamp
description: >-
  If the dining room chandelier is turned off (manually),  then make sure the
  "last_manual_off-dining_room" timestamp is updated
trigger:
  - platform: state
    entity_id:
      - light.dining_room_chandelier
    to: "off"
condition:
  - "{{ trigger.to_state.context.id != none }}"
  - "{{ trigger.to_state.context.parent_id == none }}"
  - "{{ trigger.to_state.context.user_id == none }}"
action:
  - service: input_text.set_value
    data:
      value: "{{ as_timestamp(now()) }}"
    target:
      entity_id: input_text.last_manual_off_dining_room
mode: parallel
max: 1000

Note the three items used in the condition - all three of those must always be used together for it to 100% reliably work:

  - "{{ trigger.to_state.context.id != none }}"
  - "{{ trigger.to_state.context.parent_id == none }}"
  - "{{ trigger.to_state.context.user_id == none }}"

Hope that helps. Of course since I must tell you how the watch works when you asked me what time it was, here is an example of the automation logic I use when the motion detector for that same room is triggered but only turns on the lights if the last time they were manually, physically turned off is more than 5 minutes ago:

alias: >-
  Dining Room Motion Detected (Chandelier Automation - if not switched off
  manually within last 5 minutes)
description: ""
trigger:
  - type: motion
    platform: device
    device_id: bb102cf7fbd20ffd14ac00fe56682ca4
    entity_id: binary_sensor.dining_room_motion_sensor_motion
    domain: binary_sensor
condition:
  - condition: template
    value_template: >-
      {{ (float(as_timestamp(now())) -
      float(states('input_text.last_manual_off_dining_room'),0)) > 300 }}
action:
  - service: script.motion_detected
    data:
      enablement_input_selector_entity: input_select.dining_room_chandelier_automation_is
      timer_duration_entity: input_number.dining_room_adjust_chandelier_timer
      timer_entity: timer.dining_room_chandelier_timer
mode: parallel

A few items of note “for a discussion for an other day” so to speak -

  1. I used a helper which is an input_text instead of an input_datetime due to a bug in the datetime helper which existed back when I originally wrote the automation, and

  2. …I discovered the similar paradigm I was using in numerous rooms was code that I had to repeat all over the place in automations, so I made that section of code generic within a script (the “script.motion_detected”) so that I can call the same script from many different places (instead of repeating the same logic many times in different automations)…

  3. It could be that the condition is not shown properly in the above automation in the visual editor because I was missing this, but I guess I will check at some point how to fix the ‘YAML grammar’ if that does in fact solve the issue:

condition:
  - condition: template
    value_template: >-

Hope that helps!

Hi KruseLuds, thanks for your detailed explanation, I find it very interesting in how you’ve tackled your problem. Unfortunately I don’t think it’s relevant to what I’m trying to do. The switches I use (Shelly - and I’m not sure if this is an exclusive thing for them), already allow me to identify when they are turned off using an app, automation, or through the physical switch.

My question was more around the technical how-to of identifying the entity ID from the device ID before passing that paramter through different nodes in NodeRED.

The solution I mentioned in my second posts works, albeit through more brute force than simple code.

Thank you for your response regardless, it has given me an idea of how I will need to tackle my own motion-detection sensors (once I buy some :D)