Hi there,
Because there’s no integration for the Voltcraft SEM6000 power switch/measurement device, I’m trying to implement an Expect script I found at htt ps://github.com/Heckie75/voltcraft-sem-6000) which is able to output JSON data. I’m using the command_line integration as a sensor.
As the device outputs multiple values (voltage/ampere/watts/powerfactor among others), I thought it would be wise to use the json_attributes
, just like the documentation linked above describes:
The example shows how you can retrieve multiple values with one sensor (where the additional are attributes) by using
value_json
andjson_attributes
.
This is my current configuration:
sensor:
- platform: command_line
name: koelkast
json_attributes:
- voltage
- ampere
- watts
- power_factor
- total
command: '/usr/local/bin/sem-6000.exp AB:CD:12:34:56:78 1234 --status --json'
command_timeout: 30
value_template: '{{ value_json.status }}'
The output of the sem-6000.exp
script is as followed:
homeassistant@raspberrypi:~ $ sem-6000.exp 2C:AB:33:01:11:D7 1590 --status --json
{
"device" : null,
"status" : {
"power" : 1,
"voltage" : 228,
"ampere" : 0.013,
"watts" : 0.59,
"frequency" : 50,
"power_factor" : 0.20,
"total" : 0.0
},
"settings" : null,
"schedulers" : null,
"countdown" : null,
"randommode" : null,
"data_per_hour" : [
],
"data_per_day" : [
],
"data_per_month" : [
]
}
homeassistant@raspberrypi:~ $
By using value_template: '{{ value_json.status }}'
I expected HA would select only the JSON dictionary under the key status
. And I expected json_attributes
would extract the keys used in that option from the status dictionary.
Unfortunately, I’m getting an entity sensor.fridge
with the value {'power': 1, 'voltage': 227, 'ampere': 0.013, 'watts': 0.635, 'frequency': 50, 'power_factor': 0.22, 'total': 0.0}
and the only state attributes the entity contains is friendly_name
.
So it seems value_template
works, but json_attributes
does not work There are also no other entities like sensor.fridge_watts
to be found in the Developer Tools.
I’ve tried to add some extra debug logging to the source file, especially between lines 103 and 128. According to this logging, it seems self._attributes
is empty after the mapping.
So my theory is: the value_template
is applied after HA tries to map the json_attributes
over the initial value. But that doesn’t make sense, especially when the example from the documentation shows a similar example as what I’m trying, right?
My question to you all: is my theory remotely correct? Does anyone have experience with this json_attributes
and the command_line sensor integration? And does anyone know what I’m doing wrong? I thought it would be rather simple…
My HA version is 0.112.3 and runs on a Raspberry Pi in a venv.