Rewrite a code

Hi, a need a help. I have something like this. It is working but in lovelace i get bad values. I have battery voltage in kWh, or Last Readout in Current consumption. Where is an error. I readed out template docs, but i don’t know how to rewrite it to. Any help or advice ?

sensor:
- platform: rest
  scan_interval: 900
  name: OneMeter
  resource: https://cloud.onemeter.com/api/devices/<DEVICE_ID>
  value_template: "{{ value_json.lastReading.OBIS['15_8_0'] }} {{ value_json.lastReading.OBIS['S_1_1_2'] }} {{ value_json.lastReading.OBIS['S_1_1_4'] }} {{ value_json.usage['thisMonth'] }} {{ value_json.usage['previousMonth'] }} {{ value_json.firmware['currentVersion'] }}"
  force_update: true
  headers:
    Authorization: <API_KEY>
- platform: template
  sensors:
    onemeter_kwh:
      friendly_name: "kWh"
      unit_of_measurement: "kWh"
      icon_template: mdi:flash
      value_template: "{{ states.sensor.onemeter.state.split(' ')[0] }}"
    onemeter_voltage:
      friendly_name: "OneMeter Battery Voltage"
      unit_of_measurement: "v"
      icon_template: mdi:battery
      value_template: "{{ states.sensor.onemeter.state.split(' ')[1] }}"
    onemeter_timestamp:
      friendly_name: "OneMeter Last Readout"
      icon_template: mdi:clock
      value_template: "{{ states.sensor.onemeter.state.split(' ')[2] }}"
    onemeter_this_month:
      friendly_name: "Current Consumption"
      unit_of_measurement: "kWh"
      icon_template: mdi:calendar-month
      value_template: "{{ states.sensor.onemeter.state.split(' ')[3] }}"
    onemeter_previous_month:
      friendly_name: "Previous Consumption"
      unit_of_measurement: "kWh"
      icon_template: mdi:calendar-month-outline
      value_template: "{{ states.sensor.onemeter.state.split(' ')[4] }}"
    onemeter_firmware:
      friendly_name: "OneMeter Firmware Version"
      icon_template: mdi:chip
      value_template: "{{ states.sensor.onemeter.state.split(' ')[5] }}"

It’s working or it’s not :wink:
Most likely, your `lastReading.OBIS[‘…’] sequence is wrong. Recheck the doc you got those values from.

Yeah, it is reading values from source. It’s working. But not, because template mismatch them.

Then it is not working. But no-one can help if we don’t know what the raw data is.

Did you wrote that code yourself or copied it from some random place?
The order or values do not match your sensor templates

If you use the rest integration (not rest sensor integration). You can configure everything in 1 configuration and avoid the templates all together.

From site How to monitor your electricity consumption in Home Assistant with OneMeter | OneMeter.com

My API:

Device ID:

Put them in rest section without <> and you see data.

Remove those details, now. Those are supposed to be secret.

What is shown on the site is just an example.

See

	"meta": {
		"0_2_2": {
			"key": "TARIFF",
			"constant": false,
			"category": "meter",
			"subcategory": "identification",
			"unit": "",
			"description": {
				"short": "Tariff",
				"long": "Current tariff"
			},
			"shortcode": "1"
		},
		"0_9_1": {
			"key": "TIME",
			"constant": false,
			"category": "meter",
			"subcategory": "identification",
			"unit": "",
			"description": {
				"short": "Time",
				"long": "Current time on meter"
			},
			"shortcode": "2"
		},
		"0_9_2": {
			"key": "DATE",
			"constant": false,
			"category": "meter",
			"subcategory": "identification",
			"unit": "",
			"description": {
				"short": "Date",
				"long": "Current date on meter"
			},
			"shortcode": "3"
		},
		"1_8_0": {
			"key": "POSITIVE_ACTIVE_ENERGY_TOTAL",
			"constant": false,
			"category": "energy",
			"subcategory": "active",
			"unit": "kWh",
			"description": {
				"short": "Energy A+ (total)",
				"long": "Positive active energy A+ (total)"
			},
			"shortcode": "12"
		},
		"1_8_1": {
			"key": "POSITIVE_ACTIVE_ENERGY_T1",
			"constant": false,
			"category": "energy",
			"subcategory": "active",
			"unit": "kWh",
			"description": {
				"short": "Energy A+ (tariff 1)",
				"long": "Positive active energy A+ (tariff 1)"
			},
			"shortcode": "13"
		},
[...]

for the actual values you have to use in the value_json.lastReading.OBIS fields for your case.

I see now. Thank you for your help. I re-edited it. And sorry for my English. I own you a beer. See you.

Hi,

I have modified the solution provided by “onemeter” so if an value doesn’t exist the whole thing still works


- platform: rest
  scan_interval: 900
  name: OneMeter
  force_update: true
  headers:
    Authorization: <API_KEY>
  resource: https://cloud.onemeter.com/api/devices/<DEVICE_ID>
  json_attributes:
  - lastReading
  - usage
  - firmware  
  value_template: "{{ value_json._id }}"

- platform: template
  sensor:
    - unique_id: "onemeter_last_readout"
      name: "OneMeter Last Readout"
      icon: mdi:clock
      state: >-
        {% set wert = state_attr('sensor.onemeter', 'lastReading')['OBIS'] %}
        {{ wert and 'S_1_1_4' in wert and wert['S_1_1_4'] or float(0) }}
    - unique_id: "onemeter_syncDate"
      name: "OneMeter Sync Date"
      icon: mdi:clock
      state: >-
        {% set wert = state_attr('sensor.onemeter', 'lastReading') %}
        {{ wert and 'syncDate' in wert and wert['syncDate'] or null}}
    - unique_id: onemeter_this_month_consumption"
      name: "OneMeter This Month Consumption"
      unit_of_measurement: "kWh"
      icon: mdi:calendar-month
      state: >-
        {% set wert = state_attr('sensor.onemeter', 'usage') %}
        {{ wert and 'thisMonth' in wert and wert['thisMonth'] or float(0)}}
      device_class: energy
    - unique_id: "onemeter_previous_month_consumption"
      name: "OneMeter Previous Month Consumption"
      unit_of_measurement: "kWh"
      icon: mdi:calendar-month-outline
      state: >-
        {% set wert = state_attr('sensor.onemeter', 'usage') %}
        {{ wert and 'previousMonth' in wert and wert['previousMonth'] or float(0) }}
      device_class: energy
    - unique_id: "onemeter_usage_fullTime_consumption"
      name: "OneMeter Full Time Consumption"
      unit_of_measurement: "kWh"
      icon: mdi:calendar-month-outline
      state: >-
        {% set wert = state_attr('sensor.onemeter', 'usage') %}
        {{ wert and 'fullTime' in wert and wert['fullTime'] or float(0) }}
      device_class: energy
    - unique_id: "onemeter_voltage"
      name: "OneMeter Battery Voltage"
      unit_of_measurement: "V"
      icon: mdi:battery
      state: >-
        {% set wert = state_attr('sensor.onemeter', 'lastReading')['OBIS'] %}
        {{ wert and 'S_1_1_2' in wert and wert['S_1_1_2'] or float(0) }}
      device_class: voltage
    - unique_id: "onemeter_kwh"
      name: "OneMeter kWh"
      unit_of_measurement: "kWh"
      icon: mdi:flash
      state: >-
        {% set wert = state_attr('sensor.onemeter', 'lastReading')['OBIS'] %}
        {{ wert and '15_8_0' in wert and wert['15_8_0'] or float(0) }}
      device_class: energy
    - unique_id: "onemeter_total_energy"
      name: "OneMeter Total Energy"
      unit_of_measurement: "kWh"
      icon: mdi:flash
      state: >-
        {% set wert = state_attr('sensor.onemeter', 'lastReading')['OBIS'] %}
        {{ wert and '1_8_0' in wert and wert['1_8_0'] or float(0) }}
      device_class: energy
    - unique_id: "onemeter_readout_succeeded_count"
      name: "OneMeter Readout Succeeded Count"
      icon: mdi:counter
      state: >-
        {% set wert = state_attr('sensor.onemeter', 'lastReading')['OBIS'] %}
        {{ wert and 'S_1_1_6' in wert and wert['S_1_1_6'] or float(0) }}
    - unique_id: "onemeter_readout_succeeded_update"
      name: "OneMeter Readout Succeeded Count Update"
      icon: mdi:clock
      state: >-
        {% set wert = state_attr('sensor.onemeter', 'lastReading')['updatedAt'] %}
        {{ wert and 'S_1_1_6' in wert and wert['S_1_1_6'] or float(0) }}
    - unique_id: "onemeter_readout_faild_count"
      name: "OneMeter Readout Faild Count"
      icon: mdi:counter
      state: >-
        {% set wert = state_attr('sensor.onemeter', 'lastReading')['OBIS'] %}
        {{ wert and 'S_1_1_7' in wert and wert['S_1_1_7'] or float(0) }}
    - unique_id: "onemeter_readout_faild_update"
      name: "OneMeter Readout Faild Count Update"
      icon: mdi:clock
      state: >-
        {% set wert = state_attr('sensor.onemeter', 'lastReading')['updatedAt'] %}
        {{ wert and 'S_1_1_7' in wert and wert['S_1_1_7'] or float(0) }}
    - unique_id: "onemeter_last_correct_readout_timestamp"
      name: "OneMeter Last Correct Readout Timestamp"
      icon: mdi:flash
      state: >-
        {% set wert = state_attr('sensor.onemeter', 'lastReading')['OBIS'] %}
        {{ wert and 'S_1_1_10' in wert and wert['S_1_1_10'] or null }}
    - unique_id: "onemeter_firmware"
      name: "OneMeter Firmware Version"
      icon: mdi:chip
      state: "{{ state_attr('sensor.onemeter', 'firmware')['currentVersion'] }}"

Here how it looks in HA

type: custom:auto-entities
card:
  type: entities
  title: One Meter
  state_color: false
filter:
  include:
    - name: Bedroom *
    - entity_id: sensor.onemeter*
  exclude:
    - entity_id: sensor.onemeter
sort:
  method: friendly_name
  numeric: true
  ignore_case: true
  reverse: false
1 Like

Thanks, it’s working.

1 Like

Unfortunately, this modified version contains many errors.

Helpful post of the week.

2 Likes

And what are those errors?

Otherwise Onemeter starts to be a bit obsolete. Since all the DSOs are changing the meters to “Smart” meters with DLMS encryption, and not willing to give out any username and decryption keys for that to the end client to decode either the IR, P1 or W-MBUS data (which actually is against the content of the Energy Law and the EU directives regarding access to the measurement data of the meter and interoperability, etc…), all the Onemeter devices can work only as a blink counter… It is so great to count blinks if you have PV, and would like to know how much have you transferred to the grid and how much you used from there. It is just so pointless.

If you are from the Czech Republic, then it might be a different situation there, but that is the case in Poland, the original market of Onemeter.
I assume that it will change in a similar way, if not yet, as encryption is one of the smart metering requirements for meter data transfer of the EU directive.

I think, that all information from OneMeter Cloud could be loaded into HA, but integration from OM shows nonsense only. Modified integration from DirkStorck couldn’t be loaded, check before restart shows many errors.
We have such 7 legal devices which could be attached to optical port in Czech republic. Not sure about other, I didn’t find useful information about it, I found only OneMeter as legal solution for my electricity meter.

You are not being helpful. Configs, logs. Those are helpful.

1 Like

Sorry for the late reply. I have not seen your post before.

What is your problem? What is the error? Do you need help or just moaning about this?

If you want some help, then share some error logs, or provide the output of the Onemeter API.

I used OneMeter for more than a year in Poland with two meters. It is a brilliant device, which is unfortunately getting block by the blindness of the regulators and the lack of helpfulness and interest of the operators. (Really it is not their interest to know how much you consume, they just want you to pay more and more…)

Developers confirmed me by email, that integration in HA is not working, so they are preparing something new.

Which developer?

Integrating OneMeter to Home Assistant is just doing a Rest call.