Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 108680

Adding a trigger to a sensor

$
0
0

Hello. I am a newby trying to get started. I have understood that there should be an option to add triggers to a sensor so it would be possible to determine when a sensor runs. However, I am unable to do that.
Would appreciate greatly a push to the right direction.

configuration.yaml:

sensor: !include sensors.yaml

sensors.yaml:

- platform: template
  sensors:
    night_charge_scheduler:
      unique_id: carchargerids.00001
      friendly_name: Night Charge Scheduler
      value_template: >-
        {# Variables read from Helpers -#}
        {% set charge_time = states('input_number.CarCharge_time') | int -%}
        {% set night_start = states('input_datetime.CarCharge_Night_Start') | truncate(3, False, '.', 0) | int -%}
        {% set night_end = states('input_datetime.CarCharge_Night_End') | truncate(3, False, '.', 0) | int -%}
        {# Setting price addition -#}
        {% set a = {
          "day_tariff": 0.0521,
          "day_peak_tariff": 0.0802,
          "weekend_peak_tariff": 0.0464,
          "night_tariff": 0.0299,
          "contract_price_addition": 0.0024
                  }
        -%}
        {# Gettting Nordpool prices-#}
        {% if state_attr('sensor.nordpool_kwh_ee_eur_3_10_02', 'tomorrow')[23]| string == 'None'  -%}
          {% set prices = state_attr('sensor.nordpool_kwh_ee_eur_3_10_02', 'today') -%}
        {% else -%}
          {% set prices = state_attr('sensor.nordpool_kwh_ee_eur_3_10_02', 'today') +
                          state_attr('sensor.nordpool_kwh_ee_eur_3_10_02', 'tomorrow') -%}
        {% endif -%}
        {# Adding price additions to get final price vector for today + tomorrow -#}
        {% set final_prices = namespace(values = []) -%}
        {% for price in prices -%}
          {% set x = loop.index0 -%}
          {% if loop.index0 < 24 -%}
            {% set hour = loop.index0 -%}
            {% set day = now().weekday() -%}
          {% elif loop.index0 >= 24 -%}
            {% set hour = loop.index0 - 24 -%}
            {% set day = now().weekday() + 1 -%}
          {% endif -%}
          {% if day < 5 and ((hour >= 7 and hour < 9)
                                or (hour >= 12 and hour < 16)
                                or (hour >= 20 and hour < 22))-%}
            {% set final_prices.values = final_prices.values + 
                                [(price + a.day_tariff + a.contract_price_addition) | round(4) ]  -%}
          {% elif day < 5 and ((hour >= 9 and hour < 12)
                                or (hour >= 16 and hour < 20)) -%}
            {% set final_prices.values = final_prices.values +
                                [(price + a.day_peak_tariff + a.contract_price_addition) | round(4) ]   -%}
          {% elif day >= 5 and hour > 16 and hour < 20 -%}
            {% set final_prices.values = final_prices.values +
                                [(price + a.weekend_peak_tariff + a.contract_price_addition) | round(4) ] -%}
          {% else -%}
            {% set final_prices.values = final_prices.values +
                                [(price + a.night_tariff + a.contract_price_addition) | round(4) ] -%}
        {% endif -%}
        {% endfor -%}
        {#{ final_prices.values }-#}
        {# making a vector of prices/times during "night charge" time window i.e. from night_start to
                                                                                        night_end  -#}
        {% set night_charge_prices = namespace(values = [], time = []) -%}
        {% for price in final_prices.values -%}
          {% if (loop.index0 >= night_start) and (loop.index0 < (night_end + 24)) -%}
              {% set night_charge_prices.values = night_charge_prices.values + [price] -%}
              {% set night_charge_prices.time = night_charge_prices.time + [loop.index0 % 24]-%}
          {% endif -%}
        {% endfor -%}
        {# during "night charging time" turn on only at cheapest hours -#}
        {#{ 'need to turn on the charger (at hour, price)' }-#}
        {% for price in night_charge_prices.values -%}
          {% if (night_charge_prices.values | sort)[charge_time - 1] >= price  -%}
            {{ night_charge_prices.time[loop.index0], night_charge_prices.values[loop.index0] }}
          {% endif -%}
        {% endfor -%}

Question1: where do I add the trigger and how? I do not seem to be able to - get an error…
Question2: currently I am getting an array as an output that looks crappy (example:
(4, 0.1703) (5, 0.1703) (6, 0.1233) (7, 0.1773)
How can I access these values or assign these as attributes?

Thanks!

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 108680

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>