Utility_meter has been removed in the 2022.9, help in converting code using `select`

Hi everyone!
Reading the breaking changes in the latest release (2022.9) i saw that utility_meter has been removed.
Now it should be used the select function, but I honestly dunno how to do that conversion.


utility_meter:

  total_energy_hour:
    source: sensor.total_kWh
    cycle: hourly 

  total_energy_day:
    source: sensor.total_kWh
    cycle: daily 
    tariffs:
      - f1
      - f23
      
  total_energy_mth:
    source: sensor.total_kWh
    cycle: monthly
    tariffs:
      - f1
      - f23

And then the usage in:


  - alias: Energy__Tariffa_F23_sera
    id: Energy__Tariffa_F23_sera 
    initial_state: on
    trigger:
      platform: template
      value_template: >-
        {{ states('sensor.time') == (states.input_datetime.energy_f23_start.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}
    condition:
      - condition: state
        entity_id: binary_sensor.giorni_lavorativi
        state: 'on'
    action:
    - service: utility_meter.select_tariff
      data:
        entity_id: utility_meter.total_energy_day
        tariff: f23
    - service: utility_meter.select_tariff
      data:
        entity_id: utility_meter.total_energy_mth
        tariff: f23

I would like to have some help on how to move this code to the select
Thanks in advance to everyone!

Here is my automation:

- alias: tarif_electricite
  initial_state: true
  trigger:
    - platform: time
      at: "07:00:00"
      variables:
        tariff: "pleines"
    - platform: time
      at: "23:00:00"
      variables:
        tariff: "creuses"
  condition:
    - condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
  action:
    - service: select.select_option
      target:
        entity_id: 
          - select.solar_exported_power_hourly
          - select.solar_exported_power_daily
          - select.solar_exported_power_monthly
          - select.solar_exported_power_yearly
          - select.solar_imported_power_hourly
          - select.solar_imported_power_daily
          - select.solar_imported_power_monthly
          - select.solar_imported_power_yearly
          - select.solar_house_consumption_hourly
          - select.solar_house_consumption_daily
          - select.solar_house_consumption_monthly
          - select.solar_house_consumption_yearly
          - select.solar_production_hourly
          - select.solar_production_daily
          - select.solar_production_monthly
          - select.solar_production_yearly
      data:
        option: "{{ tariff }}"

Look in Developer Tools → States. You should now have a select.total_energy_day and a select.total_energy_mth instead of utility_meter.total_energy_day and utility_meter.total_energy_mth respectively.

It is just a matter of using the select.select_option service instead of the utility_meter.select_tariff service. e.g.

- alias: Energy__Tariffa_F23_sera
  id: Energy__Tariffa_F23_sera
  initial_state: on
  trigger:
    platform: template
    value_template: >-
      {{ states('sensor.time') == (states.input_datetime.energy_f23_start.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}
  condition:
    - condition: state
      entity_id: binary_sensor.giorni_lavorativi
      state: 'on'
  action:
  - service: select.select_option
    target:
      entity_id: 
        - select.total_energy_day
        - select.total_energy_mth
    data:
      option: f23

hi @tom_l , thanks for your reply. I’ve checked and there are the select.total_energy_mth and select.total_energy_day as you said. I don’t understand how that is possible, give that i didn’t convert them.
Is there a way to have them natively converted in the code in select.total_energy_mth and select.total_energy_day?

Hi! I’m running into a problem with using select. The automation seemed to work but the value/state of the tariff is not. Here is an example of my automation that should have changed the tariff this morning. The automation did execute (checked the traces and execution time and for good measure I executed it manually as well)

alias: Tariff - Shoulder (morning)
description: ""
trigger:
  - platform: time
    at: "07:00:00"
condition: []
action:
  - service: select.select_option
    target:
      entity_id:
        - utility_meter.daily_energy
        - utility_meter.quarterly_energy
    data:
      option: shoulder
mode: single

And here are the current states of both utility meter tariffs that it should have changed.
Screenshot 2022-09-11 073846

Any ideas?

I am also in trouble because i have this code :

utility_meter:

  total_energy_hour:
    source: sensor.total_kWh
    cycle: hourly 

  total_energy_day:
    source: sensor.total_kWh
    cycle: daily 
    tariffs:
      - f1
      - f23
      
  total_energy_mth:
    source: sensor.total_kWh
    cycle: monthly
    tariffs:
      - f1
      - f23

that is an utility_meter declaration afaik. If I remove it, on the restart HA complains with the following logs:

homeassistant    | Traceback (most recent call last):
homeassistant    |   File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 421, in async_render
homeassistant    |     render_result = _render_with_context(self.template, compiled, **kwargs)
homeassistant    |   File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1906, in _render_with_context
homeassistant    |     return template.render(**kwargs)
homeassistant    |   File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
homeassistant    |     self.environment.handle_exception()
homeassistant    |   File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
homeassistant    |     raise rewrite_traceback_stack(source=source)
homeassistant    |   File "<template>", line 1, in top-level template code
homeassistant    |   File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1679, in forgiving_float_filter
homeassistant    |     return float(value)
homeassistant    | jinja2.exceptions.UndefinedError: 'None' has no attribute 'state'
homeassistant    |
homeassistant    | The above exception was the direct cause of the following exception:
homeassistant    |
homeassistant    | Traceback (most recent call last):
homeassistant    |   File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 537, in async_render_to_info
homeassistant    |     render_info._result = self.async_render(variables, strict=strict, **kwargs)
homeassistant    |   File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 423, in async_render
homeassistant    |     raise TemplateError(err) from err
homeassistant    | homeassistant.exceptions.TemplateError: UndefinedError: 'None' has no attribute 'state'
homeassistant    | 2022-09-08 18:49:55.379 ERROR (MainThread) [homeassistant.helpers.template_entity] TemplateError('UndefinedError: 'None' has no attribute 'state'') while processing template 'Template("{{((states.sensor.total_energy_mth_f23.state | float) * (states.input_number.energy_cost_f23.state| float)) | round(2) }}")' for attribute '_attr_native_value' in entity 'sensor.energy_cost_mth_f23'
homeassistant    | 2022-09-08 18:49:55.381 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{{((states.sensor.energy_cost_mth_f1.state | float) + (states.sensor.energy_cost_mth_f23.state| float)) | round(2) }}")

And this because I have this:

  - platform: template
    sensors:
      total_energy_mth:
        friendly_name_template: "Consumo mensile"
        icon_template: mdi:counter
        value_template: "{{((states.sensor.total_energy_mth_f1.state | float) + (states.sensor.total_energy_mth_f23.state | float)) | round(3) }}"
        unit_of_measurement: 'KWh'

I would like to the keep the logic, while replacing the utily_meter with something newer, does anyone can help me?

That’s why. The title of the post is incorrect. utility_meter has not been removed at all, - it’s still very much available.

All that has changed is how to select the tariff - an example of which is provided on the linked page.

I would however suggest that you update your template to this:

        value_template: "{{((states('sensor.total_energy_mth_f1') | float(0)) + (states('sensor.total_energy_mth_f23') | float(0))) | round(3) }}"
2 Likes

thanks a lot @mobile.andrew.jones for your help, really appreciated!

1 Like

Here is your problem.

You need to change it to

        - select.daily_energy
        - select.quarterly_energy

As shown in your own screenshots

image

image

Sorry to bother you, I couldn’t resolve this problem.
Here below you can find my automation. The program doesn’t change tariffs between F1 and F2. It always show me F2 tariff.
Thank you so much in advance!

automation:

  • alias: Cambio Tariffa F1 con festivi
    initial_state: on
    trigger:
    platform: template
    value_template: >-
    {{ states(‘sensor.time’) == (states.input_datetime.f1_ora_inizio.attributes.timestamp | int | timestamp_custom(’%H:%M’, False)) }}
    condition:
    condition: and
    conditions:

    • condition: state
      entity_id: binary_sensor.giorni_fascia_elettrica
      state: ‘on’
    • condition: state
      entity_id: input_boolean.tariffa_weekend
      state: ‘on’
      action:
    • service: select.select_option
      data:
      entity_id: select.consumo_giornaliero
      tariff: f1
    • service: select.select_option
      data:
      entity_id: select.consumo_mensile
      tariff: f1
  • alias: Cambio Tariffa F2 con festivi
    initial_state: on
    trigger:
    platform: template
    value_template: >-
    {{ states(‘sensor.time’) == (states.input_datetime.f2_ora_inizio.attributes.timestamp | int | timestamp_custom(’%H:%M’, False)) }}
    condition:
    condition: and
    conditions:

    • condition: state
      entity_id: binary_sensor.giorni_fascia_elettrica
      state: ‘on’
    • condition: state
      entity_id: input_boolean.tariffa_weekend
      state: ‘on’
      action:
    • service: select.select_option
      data:
      entity_id: select.consumo_giornaliero
      tariff: f2
    • service: select.select_optionf
      data:
      entity_id: select.consumo_mensile
      tariff: f2
  • alias: cambio tariffa f2 giorni festivi
    initial_state: ‘on’
    trigger:
    platform: state
    entity_id: binary_sensor.giorni_fascia_elettrica
    to: ‘off’
    condition:

    • condition: state
      entity_id: input_boolean.tariffa_weekend
      state: ‘on’
      action:
    • service: select.select_option
      data:
      entity_id: select.consumo_giornaliero
      tariff: f2
    • service: select.select_option
      data:
      entity_id: select.consumo_mensile
      tariff: f2
  • alias: Cambio Tariffa F1 senza festivi
    initial_state: on
    trigger:
    platform: template
    value_template: >-
    {{ states(‘sensor.time’) == (states.input_datetime.f1_ora_inizio.attributes.timestamp | int | timestamp_custom(’%H:%M’, False)) }}
    condition:

    • condition: state
      entity_id: input_boolean.tariffa_weekend
      state: ‘off’
      action:
    • service: select.select_option
      data:
      entity_id: select.consumo_giornaliero
      tariff: f1
    • service: select.select_option
      data:
      entity_id: select.consumo_mensile
      tariff: f1
  • alias: Cambio Tariffa F2 senza festivi
    initial_state: on
    trigger:
    platform: template
    value_template: >-
    {{ states(‘sensor.time’) == (states.input_datetime.f2_ora_inizio.attributes.timestamp | int | timestamp_custom(’%H:%M’, False)) }}
    condition:

    • condition: state
      entity_id: input_boolean.tariffa_weekend
      state: ‘off’
      action:
    • service: select.select_option
      data:
      entity_id: select.consumo_giornaliero
      tariff: f2
    • service: select.select_option
      data:
      entity_id: select.consumo_mensile
      tariff: f2

I also have the same problem, but I think I have solved it try to change tariff in option

Thank you very much for your reply.
I made this change but nothing happened.
Could you please post all your Automation code? I think we have the same package.
Thank you!

Also check

  • service:
1 Like
- id: '1665514557073'
  alias: Cambio Tariffa F1 con festivi
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.time'') == (states.input_datetime.f1_ora_inizio.attributes.timestamp
      | int | timestamp_custom(''%H:%M'', False)) }}'
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: binary_sensor.giorni_fascia_elettrica
      state: 'on'
    - condition: state
      entity_id: input_boolean.tariffa_weekend
      state: 'on'
  action:
  - service: select.select_option
    data:
      option: f1
    target:
      entity_id:
      - select.consumo_giornaliero
      - select.immissione_giornaliero
      - select.produzione_giornaliero
  - service: select.select_option
    data:
      option: f1
    target:
      entity_id:
      - select.consumo_mensile
      - select.immissione_mensile
      - select.produzione_mensile
  - service: select.select_option
    data:
      option: f1
    target:
      entity_id:
      - select.consumo_annuale
      - select.immissione_annuale
      - select.produzione_annuale
  mode: single
  initial_state: 'on'
- id: '1665515052405'
  alias: Cambio Tariffa F2 con festivi
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.time'') == (states.input_datetime.f2_ora_inizio.attributes.timestamp
      | int | timestamp_custom(''%H:%M'', False)) }}'
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: binary_sensor.giorni_fascia_elettrica
      state: 'on'
      attribute: workdays
    - condition: state
      entity_id: input_boolean.tariffa_weekend
      state: 'on'
  action:
  - service: select.select_option
    data:
      option: f2
    target:
      entity_id:
      - select.consumo_giornaliero
      - select.immissione_giornaliero
      - select.produzione_giornaliero
  - service: select.select_option
    data:
      option: f2
    target:
      entity_id:
      - select.consumo_mensile
      - select.immissione_mensile
      - select.produzione_mensile
  - service: select.select_option
    data:
      option: f2
    target:
      entity_id:
      - select.consumo_annuale
      - select.immissione_annuale
      - select.produzione_annuale
  mode: single
  initial_state: 'on'
- id: '1665515503479'
  alias: Cambio Tariffa F1 senza festivi
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.time'') == (states.input_datetime.f1_ora_inizio.attributes.timestamp
      | int | timestamp_custom(''%H:%M'', False)) }}'
  condition:
  - condition: state
    entity_id: input_boolean.tariffa_weekend
    state: 'off'
  action:
  - service: select.select_option
    data:
      option: f1
    target:
      entity_id:
      - select.consumo_giornaliero
      - select.immissione_giornaliero
      - select.produzione_giornaliero
  - service: select.select_option
    data:
      option: f1
    target:
      entity_id:
      - select.consumo_mensile
      - select.immissione_mensile
      - select.produzione_mensile
  - service: select.select_option
    data:
      option: f1
    target:
      entity_id:
      - select.consumo_annuale
      - select.immissione_annuale
      - select.produzione_annuale
  mode: single
  initial_state: 'on'
- id: '1665515523866'
  alias: Cambio Tariffa F2 senza festivi
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.time'') == (states.input_datetime.f2_ora_inizio.attributes.timestamp
      | int | timestamp_custom(''%H:%M'', False)) }}'
  condition:
  - condition: state
    entity_id: input_boolean.tariffa_weekend
    state: 'off'
  action:
  - service: select.select_option
    data:
      option: f2
    target:
      entity_id:
      - select.consumo_giornaliero
      - select.immissione_giornaliero
      - select.produzione_giornaliero
  - service: select.select_option
    data:
      option: f2
    target:
      entity_id:
      - select.consumo_mensile
      - select.immissione_mensile
      - select.produzione_mensile
  - service: select.select_option
    data:
      option: f2
    target:
      entity_id:
      - select.consumo_annuale
      - select.immissione_annuale
      - select.produzione_annuale
  mode: single
  initial_state: 'on'
- id: '1665515541435'
  alias: Cambio Tariffa F2 giorni festivi
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.giorni_fascia_elettrica
    to: 'off'
  condition:
  - condition: state
    entity_id: input_boolean.tariffa_weekend
    state: 'on'
  action:
  - service: select.select_option
    data:
      option: f2
    target:
      entity_id:
      - select.consumo_giornaliero
      - select.immissione_giornaliero
      - select.produzione_giornaliero
  - service: select.select_option
    data:
      option: f2
    target:
      entity_id:
      - select.consumo_mensile
      - select.immissione_mensile
      - select.produzione_mensile
  - service: select.select_option
    data:
      option: f2
    target:
      entity_id:
      - select.consumo_annuale
      - select.immissione_annuale
      - select.produzione_annuale
  mode: single
  initial_state: 'on'

hi guys
i’m using the updated package with select.select_option.
it was working months ago, but then i noticed that, even with the utility_meter changing tariff (f1 or f2) according to the schedule, the kHw gets added always to F2, but few random days (didn’t find any logic behind that)
It’s the same for daily, monthly and yearly counters.