#Automation to change tariffs on Utility Meter helpers

1 messages · Page 1 of 1 (latest)

lethal axle
#

Trying to follow this guide: https://community.home-assistant.io/t/quick-tldr-on-how-to-create-multi-tariff-energy-meters/585379/4
I have set up a couple Utility Meters each with two tariffs being "Peak" and "Off-Peak" based off daily energy from my Emporia Vue. That works fine, however it seems the automation I created in YAML from the above guide is like this:

alias: Electricity Tariff Automation
description: ""
mode: single
triggers:
  - trigger: state
    entity_id:
      - sensor.electricity_tariff
    not_to:
      - unavailable
      - unknown
conditions: []
actions:
  - service: select.select_option
    target:
      entity_id:
        # Add each of the sensors created from the energy Utility Meter helpers
        - select.total_energy_meter
        - select.hot_tub_energy_meter
    data:
      option: {{ trigger.to_state.state.strip('state: \"').strip('\"') if 'to_state' in trigger else 'Off-Peak' }}

but when I go to view it after saving, it is changed to this:

alias: Electricity Tariff Automation
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.electricity_tariff
    not_to:
      - unavailable
      - unknown
conditions: []
actions:
  - target:
      entity_id:
        - select.total_energy_meter
        - select.hot_tub_energy_meter
    data:
      option:
        "[object Object]": null
    action: select.select_option
mode: single

and when it was supposed to run this morning, it did not switch the tariffs and this is what the trace timeline says:

Triggered by the state of sensor.electricity_tariff at January 30, 2025 at 6:00:00 AM

Perform action 'Select: Select' on Total Energy Meter and Hot Tub Energy Meter

Stopped because an error was encountered at January 30, 2025 at 6:00:00 AM (runtime: 0.00 seconds)
value should be a string for dictionary value @ data['option']
unborn olive
#

option: {{ trigger.to_state.state.strip('state: "').strip('"') if 'to_state' in trigger else 'Off-Peak' }}

This needs string handling, either quotation marks around the template, or the multiline string marker

#

Confused by this template though, the actual state has state: as part of the state? mindblown

lethal axle
# unborn olive Confused by this template though, the actual state has `state: ` as part of the ...

I have a templated sensor that gives the string value of Peak/Off-Peak and I set the tariffs on my utility meters to match those exactly. The modification to the option was based on a comment in the guide that made it seem that having a hyphen in the tariff name was fixed by this.

Does this fix make sense:

alias: Electricity Tariff Automation
description: ''
triggers:
  - trigger: state
    entity_id:
      - sensor.electricity_tariff
    not_to:
      - unavailable
      - unknown
conditions: []
actions:
  - target:
      entity_id:
        - select.total_energy_meter
        - select.hot_tub_energy_meter
    data:
      option: >-
        {{ trigger.to_state.state.strip('state: "').strip('"') if 'to_state' in
        trigger else 'Off-Peak' }}
    action: select.select_option
mode: single
#

Also, is there any reason why the option couldn't just be option: "{{ trigger.to_state.state }}"?

unborn olive
#

That looks more like what I would expect

#

I have no idea what issue they were having in your guide though

lethal axle
#

That's fine, I thought it was fine being only about a year old. It looks like that's working when I run the automation.

Can you explain why the "else 'Off-Peak'" statement seems to be necessary (for my understanding)? The trigger.to_state.state value is 'Off-Peak', so why can't it just resolve that from it without the else statement?

#

eg: how would this be modified if there were 3 or more values

unborn olive
#

That should work to the best of my knowledge. Are you claiming that it doesn't?

#

I have no idea why someone would write that else clause

lethal axle
#

I also added my other utility meters, but if I just take out the else and have this as the option:

      option: >-
        {{ trigger.to_state.state.strip('state: "').strip('"') if 'to_state' in
        trigger }}
#

I get this error in trace:

Error: Option is not valid for entity select.clothes_washer_energy_meter, valid options are: Peak, Off-Peak
Result:
params:
  domain: select
  service: select_option
  service_data:
    option: ''
    entity_id:
      - select.total_energy_meter
      - select.air_conditioner_energy_meter
      - select.balance_vue_energy_meter
      - select.clothes_dryer_energy_meter
      - select.clothes_washer_energy_meter
      - select.dishwasher_energy_meter
      - select.furnace_energy_meter
      - select.garage_dedicated_energy_meter
      - select.garage_main_energy_meter
      - select.hot_tub_energy_meter
      - select.kitchen_fridge_energy_meter
      - select.office_energy_meter
      - select.oven_energy_meter
      - select.pantry_mini_fridge_dining_energy_meter
      - select.patio_and_panel_outlets_energy_meter
      - select.sump_pump_energy_meter
      - select.utility_room_energy_meter
      - select.water_heater_energy_meter
  target:
    entity_id:
      - select.total_energy_meter
      - select.air_conditioner_energy_meter
      - select.balance_vue_energy_meter
      - select.clothes_dryer_energy_meter
      - select.clothes_washer_energy_meter
      - select.dishwasher_energy_meter
      - select.furnace_energy_meter
      - select.garage_dedicated_energy_meter
      - select.garage_main_energy_meter
      - select.hot_tub_energy_meter
      - select.kitchen_fridge_energy_meter
      - select.office_energy_meter
      - select.oven_energy_meter
      - select.pantry_mini_fridge_dining_energy_meter
      - select.patio_and_panel_outlets_energy_meter
      - select.sump_pump_energy_meter
      - select.utility_room_energy_meter
      - select.water_heater_energy_meter
running_script: false
unborn olive
#
  {{ trigger.to_state.state.strip('state: "').strip('"') if 'to_state' in
    trigger }}

Again I don't know why you are doing this

#

What if it's just this: option: "{{ trigger.to_state.state }}" ?

unborn olive
#

Do you have other triggers than a state trigger?

#

Or are you doing things like manually running the automation?

#

A state trigger should always have to_state object.

lethal axle
#

I was manually running the automation to test it (when it included the else statement it ran fine even when the to state was the same as the current state) - my sensor.electricity_tariff is just a template sensor that looks like this:

{{ 'Off-Peak' if states('schedule.peak_cost_schedule') == 'off' or states('calendar.power_holidays') == '0' else 'Peak' }}
unborn olive
#

That's why it fails if you manually run it, there will be no trigger object, so how could it do anything.

#

It would work if it was actually triggered by the state trigger.

lethal axle
#

Oh so manually running it with the else is just hitting the else statement to catch it?

unborn olive
#

Yeah sounds like it.

lethal axle
#

Ah cool, that makes sense - I feel like there's a more elegant way of having that fallback, like
"{{ trigger.to_state.state if 'to_state' in trigger else trigger.from_state }}"?

unborn olive
#

Well if to_state doesn't exist from_state won't exist either...

#

I'm not sure about the syntax

lethal axle
#

That makes sense...is there a way to just get the value of the current state in case it gets ran ad-hoc?

unborn olive
#

states("sensor.electricity_tariff") ?

#

you could probably always just use that and then never use the trigger object as well

lethal axle
#
description: TEST
triggers:
  - trigger: state
    entity_id:
      - sensor.electricity_tariff
    not_to:
      - unavailable
      - unknown
conditions: []
actions:
  - target:
      entity_id:
        - select.total_energy_meter
        - select.air_conditioner_energy_meter
        - select.balance_vue_energy_meter
        - select.clothes_dryer_energy_meter
        - select.clothes_washer_energy_meter
        - select.dishwasher_energy_meter
        - select.furnace_energy_meter
        - select.garage_dedicated_energy_meter
        - select.garage_main_energy_meter
        - select.hot_tub_energy_meter
        - select.kitchen_fridge_energy_meter
        - select.office_energy_meter
        - select.oven_energy_meter
        - select.pantry_mini_fridge_dining_energy_meter
        - select.patio_and_panel_outlets_energy_meter
        - select.sump_pump_energy_meter
        - select.utility_room_energy_meter
        - select.water_heater_energy_meter
    data:
      option: >-
        {{ trigger.to_state.state if 'to_state' in trigger else
        states('sensor.electricity_tariff') }}
    action: select.select_option
mode: single```
That seems to work - I'm wondering why it was written the way it was originally. Is it some form of error handling in case the tariff state doesn't exist on the energy meter (like it was mistyped on setting up the energy meter)?
unborn olive
#

🤷

#

people have misconceptions about things all the time. I don't know if they were founded or unfounded

#

I don't see any reason not to do this:

      option: >-
        {{ states('sensor.electricity_tariff') }}
#

should work for triggered or not

lethal axle
#

I'll just run with that - I've been trying to set this up in a way that in case my tariffs change, I wouldn't have to go through a ton of steps to change this since I have 18 utility meters - however it seems you can't edit existing utility meters to adjust the tariffs, so I don't think I'd be able to get around just changing my tariff sensor logic