Skip to content

Instantly share code, notes, and snippets.

@WimObiwan
Created March 7, 2026 16:20
Show Gist options
  • Select an option

  • Save WimObiwan/d1fe93ed47af361e7d306779dcf9a737 to your computer and use it in GitHub Desktop.

Select an option

Save WimObiwan/d1fe93ed47af361e7d306779dcf9a737 to your computer and use it in GitHub Desktop.

P1 to shelly

  • Shelly app
    • Home icon
    • Room (eg. Living)
    • Shelly plug (...)
    • Gear icon
    • Device information
    • Device IP (eg. "192.168.0.179")
  • Home assistant:
    • configuration.yaml
       	rest_command:
           shelly_plug_color:
             url: "http://{{ ip }}/rpc/PLUGS_UI.SetConfig"
             method: POST
             content_type: "application/json"
             payload: >
               {
                 "config":{
                   "leds":{
                     "mode":"switch",
                     "colors":{
                       "switch:0":{
                         "on":{"rgb":[{{ r }},{{ g }},{{ b }}],"brightness":100},
                         "off":{"rgb":[{{ r }},{{ g }},{{ b }}],"brightness":100}
                       }
                     }
                   }
                 }
               }
    • Add automation yaml:
      alias: Shelly Plug LED using P1-power
      description: Changes the LED-color of the Shelly Plug S based on the P1-meter-power (or any other value)
      triggers:
        - entity_id: sensor.p1_meter_power
      	trigger: state
      actions:
        - data:
      	  ip: 192.168.0.179
      	  r: "{{ r }}"
      	  g: "{{ g }}"
      	  b: "{{ b }}"
      	action: rest_command.shelly_plug_color
      variables:
        p: "{{ states('sensor.p1_meter_power') | float(0) }}"
        rgb: |
      	{% set stops = [
      	  (-1000, 0,   0,   100),
      	  (0,     0,   0,   0  ),
      	  (1000,  0,   100, 0  ),
      	  (2000,  100, 100, 0  ),
      	  (3000,  100, 0,   0  ),
      	  (4000,  50,  0,   100)
      	] %} {% if p <= stops[0][0] %}
      	  {{ stops[0][1] }},{{ stops[0][2] }},{{ stops[0][3] }}
      	{% elif p >= stops[-1][0] %}
      	  {{ stops[-1][1] }},{{ stops[-1][2] }},{{ stops[-1][3] }}
      	{% else %}
      	  {% for i in range(stops | length - 1) %}
      		{% if stops[i][0] <= p < stops[i+1][0] %}
      		  {% set t = (p - stops[i][0]) / (stops[i+1][0] - stops[i][0]) %}
      		  {{ (stops[i][1] + t * (stops[i+1][1] - stops[i][1])) | round | int }},
      		  {{ (stops[i][2] + t * (stops[i+1][2] - stops[i][2])) | round | int }},
      		  {{ (stops[i][3] + t * (stops[i+1][3] - stops[i][3])) | round | int }}
      		{% endif %}
      	  {% endfor %}
      	{% endif %}
        r: "{{ rgb.split(',')[0] | trim | int }}"
        g: "{{ rgb.split(',')[1] | trim | int }}"
        b: "{{ rgb.split(',')[2] | trim | int }}"
      mode: restart
  • Change
    • sensor.p1_meter_power by your P1 meter 'Power' (in Watt)
    • 192.168.0.179 by the IP-address of your Shelly plug
  • Optionally change the colors:
    • Original colors

      Power Color
      <=-1000 Watt Blue
      0 Watt (out)
      1000 Watt Green
      2000 Watt Yellow
      3000 Watt Red
      >=4000 Watt Purple
    • Between the values, a gradient is used. e.g. 2500 = Orange

    • Original table

       (-1000, 0,   0,   100),
       (0,     0,   0,   0  ),
       (1000,  0,   100, 0  ),
       (2000,  100, 100, 0  ),
       (3000,  100, 0,   0  ),
       (4000,  50,  0,   100)
      
    • Explanation of the 'stops':

      • Every line contains a Power to color mapping

      • First column is Power in Watt, negative (injection) or positive (consumption)

      • The others are the 3 rgb-colors: Red, Green, Blue (0 - 100)

        Color R G B
        (Off) 0 0 0
        Blue 0 0 100
        Green 0 100 0
        Yellow 100 100 0
        Red 100 0 0
        Purple 50 0 100
    • If you want fixed colors, use this script instead: (only the rgb parameter is changed)

      alias: Shelly Plug LED op P1-verbruik
        description: Verandert de LED-kleur van de Shelly Plug S op basis van het P1-metervermogen
        triggers:
          - entity_id: sensor.p1_meter_power
        	trigger: state
        actions:
          - data:
        	  ip: 192.168.0.179
        	  r: "{{ r }}"
        	  g: "{{ g }}"
        	  b: "{{ b }}"
        	action: rest_command.shelly_plug_color
        variables:
          p: "{{ states('sensor.p1_meter_power') | float(0) }}"
          rgb: |
        	{% if p < 0 %}
        	  0,0,100
        	{% elif p < 2000 %}
        	  0,100,0
        	{% elif p < 3000 %}
        	  100,100,0
        	{% elif p < 4000 %}
        	  100,0,0
        	{% else %}
        	  50,0,100
        	{% endif %}
          r: "{{ rgb.split(',')[0] | trim | int }}"
          g: "{{ rgb.split(',')[1] | trim | int }}"
          b: "{{ rgb.split(',')[2] | trim | int }}"
        mode: restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment