So I successfully setup my GUDE PDU (8045-1) and configured the MQTT broker.
Now I want to add a switch for each power port to Home Assistant. Ideally showing the current state (ON/OFF) and of course the capability to switch ON/OFF via HA.
In configuration.yaml I have:
mqtt:
switch:
- unique_id: pdu_switch_2
name: "PDU Switch 2"
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/switch/2"
command_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmd"
#availability:
# - topic: "home/bedroom/switch1/available"
payload_on: '{"type": "cli", "cmd": "port 2 state set 1", "id": 10}'
payload_off: '{"type": "cli", "cmd": "port 2 state set 0", "id": 10}'
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: true
In HA it looks like this now:
(ideally I would also like a visual appearance like the PLUG-Shelve Light)
Switching works fine - so I can click OFF and the plug goes off. What does not work is showing the correct status. It always appears offline:
In the MQTT broker this is the message that is pushed when I change a switch:
You state is a long json string and not just 1 and 0
You need to define a value_template so get the value from the json
Something like
value_template: “{{ value_json.state }}”
KennethLavrsen:
{{ value_json.state }}
Thanks for the hint! I actually also found a good post of how to start with json command splitting:
will try to explan
as the MQTT is a Json Message
02:16:38 MQT: tele/sonoff_pow_1/SENSOR = {“Time”:“2018-01-25T02:16:38”,“ENERGY”:{“Total”:0.149,“Yesterday”:0.079,“Today”:0.070,“Period”:0,“Power”:22,“Factor”:0.52,“Voltage”:228,“Current”:0.183}}
think of each { as start of a object and the next { is the child of it
so want to read the
MQTT of the tele/sonoff_pow_1/SENSOR
state_topic: "tele/sonoff_pow_1/SENSOR"
as we want to Read the Yesterday value which is in side the ENERGY { } which is …
But actually the GUDE PDU support an additional command via CLI:
mqtt:
switch:
- unique_id: pdu_switch_2
name: "PDU Switch 2"
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmdres/port/2"
command_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmd/port/2"
#availability:
# - topic: "home/bedroom/switch1/available"
payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: true
Now I actually get the right control in the GUI and it feedbacks the current state via the state_topic also correctly:
The only issue is … that when I start HA the state is unknown. I need to change the state once for the system to get a feedback from MQTT about the current switch state
The PDU pushes out periodically (user setting every 0…60s) a state overview:
Can this be somehow integrated in the MQTT switch to additionally update the state of the switch - not just after actually changing ON/OFF state? Maybe with the value template … ?!?
Digging deeper into value templates I receive state for port 2 with this in my config:
sensor:
- name: "PDU Sensor 2"
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/device/telemetry"
value_template: "{{ value_json.portstates[1].state }}"
returns 1 or 0 and is updated every 60 seconds.
Now I just need to connect this with my switch to also have a state update even if I never switched the port manually via HA…
But I have a feeling that I can use either / or … so I have to decide if I use the instant state feedback when manually switching ("state_topic: “de/gudesystems/epc/00:19:32:01:79:ee/cmdres/port/2"”) and do not have inital state of the switch when I never changed it.
Or alternative to use the state topic “de/gudesystems/epc/00:19:32:01:79:ee/device/telemetry” in combination with the value_template with the downside it is updated only every 60 seconds …
mqtt:
switch:
- unique_id: pdu_switch_2
name: "PDU Switch 2"
#state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmdres/port/2" # -> no delay but no inital state data
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/device/telemetry" # -> delay but inital state data
value_template: "{{ value_json.portstates[1].state }}"
command_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmd/port/2"
payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: true
So for now I solved it by implementing both - the direct state feedback after a manual switch and the general state update that happens every 60 seconds by adding an additional 1 or 0 in front of the controls:
Ok I found now an additional CLI command:
payload_on: '{"type": "cli", "cmd": "port all state 1 show"}'
will feedback:
P1=OFF,P2=OFF,P3=OFF,P4=OFF,P5=OFF,P6=OFF,P7=OFF,P8=OFF,P9=OFF,P10=OFF,P11=OFF,P12=OFF
I am trying now to create a HA start script which updates all switches once and that should be the solution!
I got this far with my HA script… publish a MQTT message:
alias: Update PDU States via MQTT
sequence:
- service: mqtt.publish
data:
topic: de/gudesystems/epc/00:19:32:01:79:ee/cmd
payload: "{\"type\": \"cli\", \"cmd\": \"port all state 1 show\"}"
retain: true
mode: single
icon: mdi:power-socket
… which will trigger a response MQTT message from my PDU:
state_topic:
de/gudesystems/epc/00:19:32:01:79:ee/cmdres
response value:
{"type": "cli", "cmdres": ["P1=OFF,P2=OFF,P3=OFF,P4=OFF,P5=OFF,P6=OFF,P7=OFF,P8=OFF,P9=OFF,P10=OFF,P11=OFF,P12=OFF"], "result": {"num": 0, "hint": "ok"}}
Now I need to convert this response somehow into a string or array? And compare each value to set the ports via the script:
- if: []
then:
- service: switch.turn_on
data: {}
target:
entity_id: switch.pdu_port_1_switch
else:
- service: switch.turn_off
data: {}
target:
entity_id: switch.pdu_port_1_switch
So the solution I settled for now is to create 12 sensors:
- unique_id: pdu_1_sensor
name: "PDU Port 1 Sensor"
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/switch/1"
value_template: "{{ value_json.state }}"
… with a simple script that compares all sensors with all switches and sets the status accordingly (if/then action).
For that to work I had to SSH into the PDU and set:
system 0 events mqtt retain set 1
So that the “…/switch” events are retained.
So I finally cobbled everything together!
exchange “00:19:32:01:79:ee” with your unique MAC address
SSH into PDU and set retain MQTT messages (‘system 0 events mqtt retain set 1’)
In configuration.yaml:
# MQTT Switches
mqtt:
switch:
#GUDE PDU: switch for each port
- unique_id: pdu_1_switch
name: "PDU Port 1 Switch"
icon: mdi:power-socket
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/switch/1"
value_template: "{{ value_json.state }}"
command_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmd/port/1"
payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: true
- unique_id: pdu_2_switch
name: "PDU Port 2 Switch"
icon: mdi:power-socket
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/switch/2"
value_template: "{{ value_json.state }}"
command_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmd/port/2"
payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: true
- unique_id: pdu_3_switch
name: "PDU Port 3 Switch"
icon: mdi:power-socket
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/switch/3"
value_template: "{{ value_json.state }}"
command_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmd/port/3"
payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: true
- unique_id: pdu_4_switch
name: "PDU Port 4 Switch"
icon: mdi:power-socket
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/switch/4"
value_template: "{{ value_json.state }}"
command_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmd/port/4"
payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: true
- unique_id: pdu_5_switch
name: "PDU Port 5 Switch"
icon: mdi:power-socket
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/switch/5"
value_template: "{{ value_json.state }}"
command_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmd/port/5"
payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: true
- unique_id: pdu_6_switch
name: "PDU Port 6 Switch"
icon: mdi:power-socket
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/switch/6"
value_template: "{{ value_json.state }}"
command_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmd/port/6"
payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: true
- unique_id: pdu_7_switch
name: "PDU Port 7 Switch"
icon: mdi:power-socket
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/switch/7"
value_template: "{{ value_json.state }}"
command_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmd/port/7"
payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: true
- unique_id: pdu_8_switch
name: "PDU Port 8 Switch"
icon: mdi:power-socket
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/switch/8"
value_template: "{{ value_json.state }}"
command_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmd/port/8"
payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: true
- unique_id: pdu_9_switch
name: "PDU Port 9 Switch"
icon: mdi:power-socket
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/switch/9"
value_template: "{{ value_json.state }}"
command_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmd/port/9"
payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: true
- unique_id: pdu_10_switch
name: "PDU Port 10 Switch"
icon: mdi:power-socket
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/switch/10"
value_template: "{{ value_json.state }}"
command_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmd/port/10"
payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: true
- unique_id: pdu_11_switch
name: "PDU Port 11 Switch"
icon: mdi:power-socket
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/switch/11"
value_template: "{{ value_json.state }}"
command_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmd/port/11"
payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: true
- unique_id: pdu_12_switch
name: "PDU Port 12 Switch"
icon: mdi:power-socket
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/switch/12"
value_template: "{{ value_json.state }}"
command_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmd/port/12"
payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: true
text:
#GUDE PDU: retrieve the current state of all PDU power ports in a single string
- name: "PDU Port States"
icon: mdi:ab-testing
mode: "text"
command_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmd"
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/cmdres"
value_template: "{{ value_json.cmdres[0] }}"
The script to update all ports with a prior MQTT query:
alias: Update PDU States via MQTT Query
sequence:
- service: mqtt.publish
data:
topic: de/gudesystems/epc/00:19:32:01:79:ee/cmd
payload: "{\"type\": \"cli\", \"cmd\": \"port all state 1 show\"}"
- delay:
hours: 0
minutes: 0
seconds: 5
milliseconds: 0
- alias: MQTT Text to Array
variables:
pdu_states: "{{ states('text.pdu_port_states').split(',') }}"
- delay:
hours: 0
minutes: 0
seconds: 2
milliseconds: 0
- repeat:
for_each:
- switch.pdu_port_1_switch
- switch.pdu_port_2_switch
- switch.pdu_port_3_switch
- switch.pdu_port_4_switch
- switch.pdu_port_5_switch
- switch.pdu_port_6_switch
- switch.pdu_port_7_switch
- switch.pdu_port_8_switch
- switch.pdu_port_9_switch
- switch.pdu_port_10_switch
- switch.pdu_port_11_switch
- switch.pdu_port_12_switch
sequence:
- if:
- condition: template
value_template: |
{% if pdu_states[repeat.index-1][-2:] == "FF" %}
true
{% else %}
false
{% endif %}
then:
- service: switch.turn_off
target:
entity_id: "{{ repeat.item }}"
data: {}
else:
- if:
- condition: template
value_template: |
{% if pdu_states[repeat.index-1][-2:] == "ON" %}
true
{% else %}
false
{% endif %}
then:
- service: switch.turn_on
target:
entity_id: "{{ repeat.item }}"
data: {}
mode: single
icon: mdi:power-socket
The final result:
2 Likes
I also asked this question on your “Homeassistant replacing Heimdall, Homer … etc” thread, but maybe it is better placed here: I saw on this other thread that you now even managed to show the current power consumption. That is awesome! Could you give advice or even share the code on how to do that?
Power consumption via MQTT came with an update the GUDE team did specifically after my request.
Basically you receive the values via MQTT. I have my mosquitto broker running as a docker container and hocked those credentials into the Gude frontend.
Then you need to put those values into Homeassistant. My configuration looks something like this:
sensor:
#GUDE PDU: total system power consumption
- unique_id: pdu_consumption
name: "PDU Total Consumption"
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/device/telemetry"
value_template: "{{ (value_json.line_in[0].voltage * value_json.line_in[0].current) | round(1)}}"
unit_of_measurement: W
#GUDE PDU: per port power consumption (actual patch 1.3.2)
- unique_id: pdu_1_act_consumption
name: "PDU Port 1 Actual Consumption"
state_topic: "de/gudesystems/epc/00:19:32:01:79:ee/device/telemetry"
value_template: "{{ (value_json.outlets[0].act_pow) | round(0) }}"
unit_of_measurement: W