Add IP address as an entity or as an attribute for Shelly Device

I love the way how the official Shelly integration works nowadays. The only thing I am missing is the IP address of the device. Would it be possible to add it as a (default disabled) entity? Preferably turn it into a ‘direct link’ so it is possible to click the link and set specific local settings on the device itself. If the developer prefers to keep the amount of entities for the device limited it would also be possible to add the IP address as an attribute to the RSSI entity considering they are related…

You can add custom attributes to your entities using customize.

In that case I guess I would have to add the IP address to every device manually? I have about 300 of them and their IP address can change every once in a while… :sweat_smile:

2 Likes

Yes, this is very much needed. Adding the IP by hand is not an option

I’m using Shelly discover scripts (maybe you already know about), different from official integration.

for example: it has all these infos for a roller shutter (ip address too):

This is the git repository: GitHub - bieniu/ha-shellies-discovery: Script that adds MQTT discovery support for Shellies devices

I did a small lovelace card with the link for opening the shellies web interface and managed them

immagine

this is the code:

type: 'custom:config-template-card'
variables:
  IP: 'states[''sensor.shelly_2_5_68c63afaad44_ip''].state'
entities:
  - sensor.shelly_2_5_68c63afaad44_ip
  - binary_sensor.shelly_2_5_68c63afaad44_firmware_update
card:
  type: entities
  entities:
    - entity: sensor.shelly_2_5_68c63afaad44_ip
      type: 'custom:multiple-entity-row'
      name: Tapparella Destra
      show_state: false
      icon: 'mdi:open-in-new'
      entities:
        - entity: sensor.shelly_2_5_68c63afaad44_uptime
          name: uptime
        - entity: binary_sensor.shelly_2_5_68c63afaad44_firmware_update
          name: Fw
        - entity: binary_sensor.shelly_2_5_68c63afaad44_firmware_update
          name: false
          icon: 'mdi:open-in-new'
          tap_action:
            action: url
            url_path: '${"http://" + IP}'

That looks very useful. Would it work side by side with the official integration? I really like the simplicity of the official integration and I would like to stick with that.

I suppose you can have both, but it would be a double…

Shelly discovers is pretty simple, if you already have a MQTT server and discovery enabled.

You install it from HACS and as soon you reboot your Home Assistant you start having discovered new devices

Since HA 2021.11 we have the “Visit Device” function for each device. I guess we can consider this feature request granted!!! :smiling_face_with_three_hearts:

Can we? Sure it’s there as a link to the device’s own UI, but how do I call the IP address as an attribute in a template?

For me the purpose of the attribute was to be able to ‘visit’ the actual device. So for me it is solved. But indeed we still do not have the IP address as an attribute. You may want to file a feature request if you need it for other uses…

Having the IP address for the Shellys might be very useful to use “call-service” to send a “rest_command” on a Shelly since, for example, the integration still doesn’t provide to open a shade with “duration” like below:

url: "http://{{ ip_address }}/roller/0?go={{ open_or_close }}&duration={{ duration_seconds }}"

Though I successfully managed to use the following command only in the HA developper tools template, but unfortunately not in any other card:

{% set ip_address = device_attr('cover.cover_bedroom', 'configuration_url').split("//")[1] %}
{{ ip_address }}

I was neither able to use it with the custom:button-card, which is amazing, like below:

variables:
  ip_address: "[[[ return device_attr('cover.cover_bedroom', 'configuration_url').split('//')[1] ]]]"

Any ideas?

Of course it might also be very good to have available on Shelly rollers “duration” and “last_direction”. This would be very useful to be able to orient the shades using different timings depending on the roller last_direction, either with a button or automatically.

Thanks!

1 Like

Just wanted to share the good solution I found.
It requires you to use dhcp on your router to give shellies a fixed IP address.

In configuration.yaml:

homeassistant:
  customize:
    light.light_kitchen_sink:
      ip: "192.168.0.101"
    light.light_kitchen_bar:
      ip: "192.168.0.102"

Then you can use your normal entity, and refer to the attribute “ip”.
For example in a rest service like this:

rest_command:
  #Sets brightness preset without turning the light on
  shelly_dimmer_preset_brightness:
    url: "http://{{ state_attr(entity_id, 'ip') }}/light/0?brightness={{ brightness | default(50) }}"

Which you can call by:

- service: rest_command.shelly_dimmer_preset_brightness
  data:
    entity_id: light.light_kitchen_bar
    brightness: 80
1 Like

@zynth This is very interesting, I ended up doing something very similar, but I had to create specific sensors, which I call “extended”, to add specific attributes for the “current position” and the “last direction”, to be able to “orient/tilt” the shades… The following is my code in case you might want to use it or you think of a better soultion to integrate these changing variable attributes inside the main entity (Shelly 2.5PM):
Inside sensors.yaml file:

  - platform: rest
    name: "Persiana 1 Era extended"
    resource: http://192.168.131.181/status
    method: GET
    scan_interval: 2
    json_attributes_path: "$.rollers[0]"
    value_template: "{{ value_json.wifi_sta.ip }}"
    json_attributes:
      - "state"
      - "current_pos"
      - "last_direction"

And then, similarly as you pointed out inside rest.yaml:

persiana_tilt_open_close:
  url: "http://{{ ip_address }}/roller/0?go={{ tilt_open_close }}&duration={{ duration }}"
  method: put

This is a screen capture of the state:

Please, let me know if you think of a better solution, i.e. to integrate these attributes inside the main Shelly entity.
Thank you very much!

Thanks, this is working great for me!

I managed to use the construct device_attr('my_entity_id', 'configuration_url') successfully within auto-entities + template-entity-row to show the IP addresses of all my Shelly devices with very few lines of YAML. If you copy this, don’t forget to adapt the line - entity_id: /switch.mini/ to match the naming convention of your shellies:

type: custom:auto-entities
card:
  type: entities
  title: Shelly
  show_header_toggle: false
filter:
  include:
    - entity_id: /switch.mini/
      options:
        type: custom:template-entity-row
        toggle: true
        secondary: >-
          {{ device_attr('this.entity_id', 'configuration_url') |
          replace('http://','') | replace(':80','') }}

UPDATE:

Here is a new version that uses device_attr() not only to add the IP address as a secondary info, but also to find all my Shellies using a filter template with a for loop. The advantage of this version is that it does not rely on any special naming pattern in the entity_id like the version above does. Instead it uses a generic device_attr(e.entity_id, 'manufacturer') == "Shelly" to find all Shelly devices.

So this should basically work through copy&paste for everyone.

type: custom:auto-entities
card:
  type: entities
  title: Shelly
  show_header_toggle: false
sort:
  method: name
  ignore_case: true
filter:
  template: |
    [
      {% for e in states %}
        {% 
          if  device_attr(e.entity_id, 'manufacturer') == "Shelly" 
          and (
            "switch." in e.entity_id
            or "light." in e.entity_id
            or "cover." in e.entity_id
          )
        %}
          {
            "type": "custom:template-entity-row",
            "toggle": "true",
            "entity": "{{ e.entity_id }}",
            "secondary": 
              "{{ device_attr(e.entity_id, 'configuration_url') |
              replace('http://','') | replace(':80','') }}"
          },

        {% endif %}
      {% endfor %}
    ]

Hint:
All possible second arguments for device_attr() can be found here:

3 Likes

Took me a moment to realize you can just use device_attr(..., configuration_url) directly in a rest_command:

# define a REST command in configuration.yaml
rest_command:
  call_shelly:
    url: "{{ device_attr(shelly_entity, \"configuration_url\") }}{{ rest_endpoint }}"
# use it in an automation:
- service: rest_command.call_shelly
  metadata: {}
  data:
    shelly_entity: binary_sensor.office_input_1_input
    rest_endpoint: /script/1/floodData?rrr=rrr

# will call http://192.168.1.137/script/1/floodData?rrr=rrr