Skip to content

Instantly share code, notes, and snippets.

@arfoll
Created December 5, 2021 14:13
Show Gist options
  • Save arfoll/3094d66ce5ff1e6b4f202579eb80d801 to your computer and use it in GitHub Desktop.
Save arfoll/3094d66ce5ff1e6b4f202579eb80d801 to your computer and use it in GitHub Desktop.
shelly gen 2 mqtt
## configuration.yaml
automation: !include automation.yaml
switch: !include switches.yaml
binary_sensor: !include binary_sensors.yaml
sensor: !include sensors.yaml
## automation.yaml
- id: shellies_get_status
alias: Shellies Get Status
description: ''
trigger:
- platform: homeassistant
event: start
- platform: time_pattern
hours: /1
condition: []
action:
- service: mqtt.publish
data:
topic: homeassistant/shellies/shellyplus1pm-XXXX
payload: '{"id":1, "src": "homeassistant/shellies/shellyplus1pm-XXXX", "method": "Shelly.GetStatus"}'
mode: single
## switches.yaml
# Just turn on/off the switch
- platform: mqtt
name: "Shelly Plus 1PM Switch"
unique_id: switch.shellyplus1pm-xxx_switch
payload_on: '{"id":1, "src": "homeassistant/shellies/shellyplus1pm-XXXX", "method": "Switch.Set", "params":{"id":0,"on":true}}'
payload_off: '{"id":1, "src": "homeassistant/shellies/shellyplus1pm-XXXX", "method": "Switch.Set", "params":{"id":0,"on":false}}'
qos: 1
state_topic: shellies/shellyplus1pm-XXXX/status/switch:0
value_template: "{{ value_json.output }}"
state_on: true
state_off: false
command_topic: shellies/shellyplus1pm-XXXX/rpc
## binary_sensors.yaml
# Physical input state
- platform: mqtt
name: "Shelly Plus 1PM Input"
unique_id: binary_sensor.shellyplus1pm-xxx_input
value_template: "{{ value_json.state }}"
payload_on: true
payload_off: false
state_topic: shellies/shellyplus1pm-XXXX/status/input:0
#stable firmware only
- platform: mqtt
device_class: update
name: "Shelly Plus 1PM Firmware Update"
unique_id: binary_sensor.shellyplus1pm-xxx_firmware_update
state_topic: homeassistant/shellies/shellyplus1pm-XXXX/rpc
value_template: >
{%- if value_json.result.sys.available_updates.stable is defined %}
ON
{%- else %}
OFF
{%- endif %}
json_attributes_topic: homeassistant/shellies/shellyplus1pm-XXXX/rpc
json_attributes_template: >
{%- if value_json.result.sys.available_updates.stable is defined %}
{{ value_json.result.sys.available_updates.stable | tojson }}
{%- endif %}
##sensors.yaml
#Temperature
## i clould probably use a better template to consider F as well... .tF can help ##
- platform: mqtt
name: "Shelly Plus 1PM Temperature"
unique_id: sensor.shellyplus1pm-xxx_temperature
device_class: temperature
unit_of_measurement: '°C'
icon: mdi:temperature-celsius
state_topic: shellies/shellyplus1pm-XXXX/status/switch:0
value_template: "{{ value_json.temperature.tC }}"
#Voltage
- platform: mqtt
name: "Shelly Plus 1PM Voltage"
unique_id: sensor.shellyplus1pm-xxx_voltage
device_class: voltage
unit_of_measurement: 'V'
state_topic: shellies/shellyplus1pm-XXXX/status/switch:0
value_template: "{{ value_json.voltage }}"
#Power
- platform: mqtt
name: "Shelly Plus 1PM Power"
unique_id: sensor.shellyplus1pm-xxx_power
device_class: power
unit_of_measurement: 'W'
icon: mdi:lightning-bolt-outline
state_topic: shellies/shellyplus1pm-XXXX/status/switch:0
value_template: "{{ value_json.apower }}"
#Energy - Works with energy dashboard
- platform: mqtt
name: "Shelly Plus 1PM Energy"
unique_id: sensor.shellyplus1pm-xxx_energy
device_class: energy
state_class: total_increasing
unit_of_measurement: 'kWh'
icon: mdi:lightning-bolt-outline
state_topic: shellies/shellyplus1pm-XXXX/status/switch:0
value_template: "{{ ((value_json.aenergy.total / 1000)|float) | round(2) }}"
last_reset_value_template: '1970-01-01T00:00:00+00:00'
- platform: mqtt
name: "Shelly Plus 1PM Uptime"
unique_id: sensor.shellyplus1pm-xxx_uptime
device_class: timestamp
state_topic: homeassistant/shellies/shellyplus1pm-XXXX/rpc
value_template: "{{(as_timestamp(now())-value_json.result.sys.uptime)|timestamp_local}}"
- platform: mqtt
name: "Shelly Plus 1PM IP"
unique_id: sensor.shellyplus1pm-xxx_ip
state_topic: homeassistant/shellies/shellyplus1pm-XXXX/rpc
value_template: "{{ value_json.result.wifi.sta_ip }}"
- platform: mqtt
name: "Shelly Plus 1PM SSID"
unique_id: sensor.shellyplus1pm-xxx_ssid
icon: mdi:wifi
state_topic: homeassistant/shellies/shellyplus1pm-XXXX/rpc
value_template: "{{ value_json.result.wifi.ssid }}"
- platform: mqtt
name: "Shelly Plus 1PM RSSI"
unique_id: sensor.shellyplus1pm-xxx_rssi
unit_of_measurement: dBm
device_class: signal_strength
state_topic: homeassistant/shellies/shellyplus1pm-XXXX/rpc
value_template: "{{ value_json.result.wifi.rssi }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment