EDIT: I figured it out, and added the details below. Hope it helps somebody in the furture.
EDIT2: It would be nice to have this as a HACS addon, does anybody has a hint how to do this best?
EDIT3: updated the entries, there were some errors
EDIT4: the -sensor was missing in templates
It took me a while, but I was able to write a bash script to retrieve the json.
The device is a B-control EM 300.
It looks like this:
#!/bin/bash
# Hardcoded password
PASSWORD="password"
# Define the base URL and the cookie file location
BASE_URL="http://192.168.1.115"
COOKIE_FILE="/tmp/cookie.txt"
# Step 1: Initial GET request to /start.php to get the session cookie and serial
RESPONSE=$(curl -s -X GET "$BASE_URL/start.php" \
-H "Content-Type: application/x-www-form-urlencoded" \
-c "$COOKIE_FILE")
# Extract the serial from the response using grep and awk
SERIAL=$(echo "$RESPONSE" | grep -o '"serial":"[0-9]*' | awk -F ':"' '{print $2}')
# Step 2: POST request to /start.php to authenticate using the serial as the username
curl -s -X POST "$BASE_URL/start.php" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "login=$SERIAL&password=$PASSWORD" \
-b "$COOKIE_FILE" \
-c "$COOKIE_FILE" > /dev/null
# Step 3: Final GET request to /mum-webservice/data.php to retrieve data and clean the response
DATA=$(curl -s -X GET "$BASE_URL/mum-webservice/data.php" \
-H "Content-Type: application/x-www-form-urlencoded" \
-b "$COOKIE_FILE")
# Remove the trailing '#' from the response if it exists
echo "$DATA" | sed 's/#$//'
Place the password in the script.
I added this script under:
/config/scripts/bcontrol_em300.sh
Important to make it executable:
chmod +x bcontrol_em300.sh
Test it like this:
./bcontrol_em300.sh
The json it returns looks like this:
{"serial":"XXXXXXXXX","1-0:1.4.0*255":3439.2,"1-0:1.8.0*255":20903203.5,"1-0:2.4.0*255":0,"1-0:2.8.0*255":9886005.6,"1-0:3.4.0*255":0,"1-0:3.8.0*255":18001880.4,"1-0:4.4.0*255":787.1,"1-0:4.8.0*255":14142255.3,"1-0:9.4.0*255":3528.1,"1-0:9.8.0*255":33908295.4,"1-0:10.4.0*255":0,"1-0:10.8.0*255":18362501.7,"1-0:13.4.0*255":0.975,"1-0:14.4.0*255":49.977,"1-0:21.4.0*255":3396.2,"1-0:21.8.0*255":8834143.1,"1-0:22.4.0*255":0,"1-0:22.8.0*255":7619529.1,"1-0:23.4.0*255":0,"1-0:23.8.0*255":4808838.1,"1-0:24.4.0*255":465.9,"1-0:24.8.0*255":7141044.8,"1-0:29.4.0*255":3428,"1-0:29.8.0*255":12187909.5,"1-0:30.4.0*255":0,"1-0:30.8.0*255":10962298,"1-0:31.4.0*255":15.414,"1-0:32.4.0*255":222.71,"1-0:33.4.0*255":0.991,"1-0:41.4.0*255":58.8,"1-0:41.8.0*255":13418464.6,"1-0:42.4.0*255":0,"1-0:42.8.0*255":4530593.2,"1-0:43.4.0*255":0,"1-0:43.8.0*255":9768404.1,"1-0:44.4.0*255":120,"1-0:44.8.0*255":2535989.6,"1-0:49.4.0*255":133.6,"1-0:49.8.0*255":18661896.7,"1-0:50.4.0*255":0,"1-0:50.8.0*255":5341396.4,"1-0:51.4.0*255":0.823,"1-0:52.4.0*255":225.811,"1-0:53.4.0*255":0.44,"1-0:61.4.0*255":0,"1-0:61.8.0*255":9041140.1,"1-0:62.4.0*255":15.8,"1-0:62.8.0*255":8126429,"1-0:63.4.0*255":0,"1-0:63.8.0*255":6023640.2,"1-0:64.4.0*255":201.2,"1-0:64.8.0*255":7064222.1,"1-0:69.4.0*255":0,"1-0:69.8.0*255":13536996,"1-0:70.4.0*255":201.9,"1-0:70.8.0*255":11033267.8,"1-0:71.4.0*255":0.942,"1-0:72.4.0*255":225.252,"1-0:73.4.0*255":-0.079,"status":0}
Now I am looking for a way to get all the json values as sensors, and also remap the name fields to something more meaningful. I manage to do it for one, but since there are many values I don’t want to curl the file for each value, but parse all with one update.
Does somebody has an idea how to do that?
In the configuration.yaml I added the script as a command_line sensor like this:
command_line:
- sensor:
name: "bcontrol_em300_data"
command: "/bin/bash /config/scripts/bcontrol_em300.sh"
scan_interval: 60 # Adjust based on how often you want to call the script (in seconds)
value_template: "{{ value_json['status'] }}"
json_attributes:
- "1-0:1.4.0*255"
- "1-0:1.8.0*255"
- "1-0:2.4.0*255"
- "1-0:2.8.0*255"
- "1-0:3.4.0*255"
- "1-0:3.8.0*255"
- "1-0:4.4.0*255"
- "1-0:4.8.0*255"
- "1-0:9.4.0*255"
- "1-0:9.8.0*255"
- "1-0:10.4.0*255"
- "1-0:10.8.0*255"
- "1-0:13.4.0*255"
- "1-0:14.4.0*255"
- "1-0:21.4.0*255"
- "1-0:21.8.0*255"
- "1-0:22.4.0*255"
- "1-0:22.8.0*255"
- "1-0:23.4.0*255"
- "1-0:23.8.0*255"
- "1-0:24.4.0*255"
- "1-0:24.8.0*255"
- "1-0:29.4.0*255"
- "1-0:29.8.0*255"
- "1-0:30.4.0*255"
- "1-0:30.8.0*255"
- "1-0:31.4.0*255"
- "1-0:32.4.0*255"
- "1-0:33.4.0*255"
- "1-0:41.4.0*255"
- "1-0:41.8.0*255"
- "1-0:42.4.0*255"
- "1-0:42.8.0*255"
- "1-0:43.4.0*255"
- "1-0:43.8.0*255"
- "1-0:44.4.0*255"
- "1-0:44.8.0*255"
- "1-0:49.4.0*255"
- "1-0:49.8.0*255"
- "1-0:50.4.0*255"
- "1-0:50.8.0*255"
- "1-0:51.4.0*255"
- "1-0:52.4.0*255"
- "1-0:53.4.0*255"
- "1-0:61.4.0*255"
- "1-0:61.8.0*255"
- "1-0:62.4.0*255"
- "1-0:62.8.0*255"
- "1-0:63.4.0*255"
- "1-0:63.8.0*255"
- "1-0:64.4.0*255"
- "1-0:64.8.0*255"
- "1-0:69.4.0*255"
- "1-0:69.8.0*255"
- "1-0:70.4.0*255"
- "1-0:70.8.0*255"
- "1-0:71.4.0*255"
- "1-0:72.4.0*255"
- "1-0:73.4.0*255"
- "status"
template:
- sensor:
- name: "BControlEM300 Active Power Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:1.4.0*255') | float }}"
unit_of_measurement: "W"
device_class: "power"
- name: "BControlEM300 Active Energy Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:1.8.0*255') | float }}"
unit_of_measurement: "Wh"
device_class: "energy"
- name: "BControlEM300 Active Power Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:2.4.0*255') | float }}"
unit_of_measurement: "W"
device_class: "power"
- name: "BControlEM300 Active Energy Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:2.8.0*255') | float }}"
unit_of_measurement: "Wh"
device_class: "energy"
- name: "BControlEM300 Reactive Power Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:3.4.0*255') | float }}"
unit_of_measurement: "var"
device_class: "power"
- name: "BControlEM300 Reactive Energy Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:3.8.0*255') | float }}"
unit_of_measurement: "varh"
device_class: "energy"
- name: "BControlEM300 Reactive Power Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:4.4.0*255') | float }}"
unit_of_measurement: "var"
device_class: "power"
- name: "BControlEM300 Reactive Energy Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:4.8.0*255') | float }}"
unit_of_measurement: "varh"
device_class: "energy"
- name: "BControlEM300 Apparent Power Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:9.4.0*255') | float }}"
unit_of_measurement: "VA"
device_class: "power"
- name: "BControlEM300 Apparent Energy Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:9.8.0*255') | float }}"
unit_of_measurement: "VAh"
device_class: "energy"
- name: "BControlEM300 Apparent Power Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:10.4.0*255') | float }}"
unit_of_measurement: "VA"
device_class: "power"
- name: "BControlEM300 Apparent Energy Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:10.8.0*255') | float }}"
unit_of_measurement: "VAh"
device_class: "energy"
- name: "BControlEM300 Power Factor"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:13.4.0*255') | float }}"
unit_of_measurement: ""
device_class: "power_factor"
- name: "BControlEM300 Frequency"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:14.4.0*255') | float }}"
unit_of_measurement: "Hz"
device_class: "frequency"
- name: "BControlEM300 L1 Active Power Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:21.4.0*255') | float }}"
unit_of_measurement: "W"
device_class: "power"
- name: "BControlEM300 L1 Active Energy Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:21.8.0*255') | float }}"
unit_of_measurement: "Wh"
device_class: "energy"
- name: "BControlEM300 L1 Active Power Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:22.4.0*255') | float }}"
unit_of_measurement: "W"
device_class: "power"
- name: "BControlEM300 L1 Active Energy Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:22.8.0*255') | float }}"
unit_of_measurement: "Wh"
device_class: "energy"
- name: "BControlEM300 L1 Reactive Power Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:23.4.0*255') | float }}"
unit_of_measurement: "var"
device_class: "power"
- name: "BControlEM300 L1 Reactive Energy Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:23.8.0*255') | float }}"
unit_of_measurement: "varh"
device_class: "energy"
- name: "BControlEM300 L1 Reactive Power Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:24.4.0*255') | float }}"
unit_of_measurement: "var"
device_class: "power"
- name: "BControlEM300 L1 Reactive Energy Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:24.8.0*255') | float }}"
unit_of_measurement: "varh"
device_class: "energy"
- name: "BControlEM300 L1 Apparent Power Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:29.4.0*255') | float }}"
unit_of_measurement: "VA"
device_class: "power"
- name: "BControlEM300 L1 Apparent Energy Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:29.8.0*255') | float }}"
unit_of_measurement: "VAh"
device_class: "energy"
- name: "BControlEM300 L1 Apparent Power Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:30.4.0*255') | float }}"
unit_of_measurement: "VA"
device_class: "power"
- name: "BControlEM300 L1 Apparent Energy Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:30.8.0*255') | float }}"
unit_of_measurement: "VAh"
device_class: "energy"
- name: "BControlEM300 L1 Current"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:31.4.0*255') | float }}"
unit_of_measurement: "A"
device_class: "current"
- name: "BControlEM300 L1 Voltage"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:32.4.0*255') | float }}"
unit_of_measurement: "V"
device_class: "voltage"
- name: "BControlEM300 L1 Power Factor"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:33.4.0*255') | float }}"
unit_of_measurement: ""
device_class: "power_factor"
- name: "BControlEM300 L2 Active Power Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:41.4.0*255') | float }}"
unit_of_measurement: "W"
device_class: "power"
- name: "BControlEM300 L2 Active Energy Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:41.8.0*255') | float }}"
unit_of_measurement: "Wh"
device_class: "energy"
- name: "BControlEM300 L2 Active Power Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:42.4.0*255') | float }}"
unit_of_measurement: "W"
device_class: "power"
- name: "BControlEM300 L2 Active Energy Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:42.8.0*255') | float }}"
unit_of_measurement: "Wh"
device_class: "energy"
- name: "BControlEM300 L2 Reactive Power Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:43.4.0*255') | float }}"
unit_of_measurement: "var"
device_class: "power"
- name: "BControlEM300 L2 Reactive Energy Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:43.8.0*255') | float }}"
unit_of_measurement: "varh"
device_class: "energy"
- name: "BControlEM300 L2 Reactive Power Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:44.4.0*255') | float }}"
unit_of_measurement: "var"
device_class: "power"
- name: "BControlEM300 L2 Reactive Energy Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:44.8.0*255') | float }}"
unit_of_measurement: "varh"
device_class: "energy"
- name: "BControlEM300 L2 Apparent Power Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:49.4.0*255') | float }}"
unit_of_measurement: "VA"
device_class: "power"
- name: "BControlEM300 L2 Apparent Energy Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:49.8.0*255') | float }}"
unit_of_measurement: "VAh"
device_class: "energy"
- name: "BControlEM300 L2 Apparent Power Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:50.4.0*255') | float }}"
unit_of_measurement: "VA"
device_class: "power"
- name: "BControlEM300 L2 Apparent Energy Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:50.8.0*255') | float }}"
unit_of_measurement: "VAh"
device_class: "energy"
- name: "BControlEM300 L2 Current"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:51.4.0*255') | float }}"
unit_of_measurement: "A"
device_class: "current"
- name: "BControlEM300 L2 Voltage"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:52.4.0*255') | float }}"
unit_of_measurement: "V"
device_class: "voltage"
- name: "BControlEM300 L2 Power Factor"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:53.4.0*255') | float }}"
unit_of_measurement: ""
device_class: "power_factor"
- name: "BControlEM300 L3 Active Power Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:61.4.0*255') | float }}"
unit_of_measurement: "W"
device_class: "power"
- name: "BControlEM300 L3 Active Energy Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:61.8.0*255') | float }}"
unit_of_measurement: "Wh"
device_class: "energy"
- name: "BControlEM300 L3 Active Power Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:62.4.0*255') | float }}"
unit_of_measurement: "W"
device_class: "power"
- name: "BControlEM300 L3 Active Energy Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:62.8.0*255') | float }}"
unit_of_measurement: "Wh"
device_class: "energy"
- name: "BControlEM300 L3 Reactive Power Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:63.4.0*255') | float }}"
unit_of_measurement: "var"
device_class: "power"
- name: "BControlEM300 L3 Reactive Energy Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:63.8.0*255') | float }}"
unit_of_measurement: "varh"
device_class: "energy"
- name: "BControlEM300 L3 Reactive Power Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:64.4.0*255') | float }}"
unit_of_measurement: "var"
device_class: "power"
- name: "BControlEM300 L3 Reactive Energy Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:64.8.0*255') | float }}"
unit_of_measurement: "varh"
device_class: "energy"
- name: "BControlEM300 L3 Apparent Power Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:69.4.0*255') | float }}"
unit_of_measurement: "VA"
device_class: "power"
- name: "BControlEM300 L3 Apparent Energy Plus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:69.8.0*255') | float }}"
unit_of_measurement: "VAh"
device_class: "energy"
- name: "BControlEM300 L3 Apparent Power Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:70.4.0*255') | float }}"
unit_of_measurement: "VA"
device_class: "power"
- name: "BControlEM300 L3 Apparent Energy Minus"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:70.8.0*255') | float }}"
unit_of_measurement: "VAh"
device_class: "energy"
- name: "BControlEM300 L3 Current"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:71.4.0*255') | float }}"
unit_of_measurement: "A"
device_class: "current"
- name: "BControlEM300 L3 Voltage"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:72.4.0*255') | float }}"
unit_of_measurement: "V"
device_class: "voltage"
- name: "BControlEM300 L3 Power Factor"
state: "{{ state_attr('sensor.bcontrol_em300_data', '1-0:73.4.0*255') | float }}"
unit_of_measurement: ""
device_class: "power_factor"
After a reboot go to overview, all the values should be visible there and are also usable in home assistant now!