Issue with template config for current energy tariff

I am having issue with the code, I took from LINK and updated per my config.

          {% if is_state('sensor.current_electricity_season', 'summer') %} 
            {% if is_state('sensor.current_electricity_rate_type', 'offpeak')%} 
              {{(states('input_number.summer_offpeak'))}}
            {% elif is_state('sensor.current_electricity_rate_type', 'peak')%} 
              {{(states('input_number.summer_peak'))}}
            {% endif %}
          {% else %} 
            {% if is_state('sensor.current_electricity_rate_type', 'offpeak')%} 
              {{(states('input_number.winter_offpeak'))}}
            {% elif is_state('sensor.current_electricity_rate_type', 'peak')%} 
              {{(states('input_number.winter_peak'))}}
            {% endif %}
          {% endif %}

The output of above is “Unknown”. I think {{(states('input_number.summer/winter_offpeak/peak'))}} is not getting created.
The sensors sensor.current_electricity_season and sensor.current_electricity_rate_type is working and I see the correct output.I am looking to feed the value from offpeak/peak to go into those four sensors input_number.summer_offpeak input_number.summer_peak input_number.winter_offpeak input_number.winter_peak

My utility meter has tarriffs as peak, offpeak
and switching the tarriff using this code

      current_electricity_rate_type:
        friendly_name: Current Electricity Rate Type  
        value_template: >-
          {% if now() >= today_at("14:00:00") and now() < today_at("18:59:59")%} 
          peak
          {% else %} 
          offpeak
          {% endif %}

Thank You

1 Like

Entities who’s IDs start input_ are user-created. You can create the necessary Input Numbers in the Helpers Setting menu or in your configuration.yaml file.

Once you have your helper entities set up, you can simplify that template a lot:

{% set season = 'summer' if 
is_state('sensor.current_electricity_season', 'summer') else 'winter' %}
{% set type = states('sensor.current_electricity_rate_type') %}
{% set ent = 'input_number.'~season~'_'~type %}
{{ states(ent) }}
1 Like

This is very clever, thank you so much for sharing it.

I need another favor if you can please. I’m not sure how to write this. I am trying to set(automate) the Utility daily_enegy based on current summer_peak, summer_offpeak, winter_peak and winter_offpeak.

     Set -> daily_energy
      Condition so that this is if season = Summer and time >=14:00:00 < 18:59:59 
      then daily_energy_summer_peak 
      else if season = Summer and time >=19:00:00 < 13:59:59 
      then daily_energy_summer_offpeak)
          {% else %} 
      Condition so that this is if season = Winter and time >=14:00:00 < 18:59:59 
      then daily_energy_Winter_peak 
      else if season = Winter and time >=19:00:00 < 13:59:59
      then daily_energy_Winter_offpeak)
          {% endif %}
Reset Daily_Energy: End of each month at 12:00 AM

My utility meter is

utility_meter:

  daily_energy:
    source: sensor.total_from_main
    name: Daily Energy
    cycle: daily
    tariffs:
      - winter_offpeak
      - winter_peak
      - summer_offpeak
      - summer_peak
  monthly_energy:
    source: sensor.total_from_main
    name: Monthly Energy
    cycle: monthly
    tariffs:
      - winter_offpeak
      - winter_peak
      - summer_offpeak
      - summer_peak

It’s basically the same as the other one, just a little shorter:

      current_electricity_rate_type:
        friendly_name: Current Electricity Rate Type  
        value_template: >-
          {% set season = 'summer' if is_state('sensor.current_electricity_season', 'summer') else 'winter' %}
          {% set type = 'peak' if today_at('14:00:00') <= now() < today_at('18:59:59') else 'offpeak' %}
          {{ season~'_'~type }}

If I’m understanding the rest of your question correctly, the next step would be an automation to switch the tariffs via the select entities:

trigger:
  -  platform: homeassistant
     event: start
  -  platform: state
     entity_id: sensor.current_electricity_rate_type
     not_to:
       - unavailable
       - unknown
action:
  - action: select.select_option
    target:
      entity_id: 
        - select.daily_energy
        - select.monthly_energy
    data:
      option: "{{ states('sensor.current_electricity_rate_type') }}"
1 Like

Thank you so much for your help. Your solution worked perfectly, and saved me weeks worth of work. I really appreciate your assistance.

What does the not_to in the automation does? I could not find it in the HA guide.

In general, it is used so that a State trigger:

  1. Fires only on changes to the state, not on attribute changes.
  2. Fires on any state change except those where the new state is one of the listed values.

State Trigger docs

In this case, using not_to prevents the automation running if/when the sensor goes into a fail state or during start up when the state briefly goes from unknown to unavailable.

It would probably be fine to leave it out, since there won’t be a a matching option that the select could be changed to that would be a problem, but adding it doesn’t hurt anything and may prevent a couple error messages showing up in the logs.