TheFes / cheapest-energy-hours

Jinja macro to find the cheapest energy prices
GNU General Public License v3.0
85 stars 9 forks source link

Need help for sensor data #156

Closed froggi1930 closed 2 months ago

froggi1930 commented 2 months ago

Hi TheFes,

sorry to bother you again, but I fail to understand how I can create a sensor which contains the data from the jinja expression:

{% from 'cheapest_energy_hours.jinja' import cheapest_energy_hours %}
{% set output = cheapest_energy_hours(sensor='sensor.tibber_forecast', hours=3,mode = 'all') %}
{{output}}

In the configuration.yaml I have set up a sensor tibber_forcast

template:
  - trigger:
      - platform: time_pattern
        hours: "/1"
      - platform: homeassistant
        event: start
    action:
      - service: tibber.get_prices
        data:
          start: "{{ (today_at() - timedelta(days=1)).strftime('%Y-%m-%d %H:%M:%S') }}"
          end: "{{ (today_at() + timedelta(days=2)).strftime('%Y-%m-%d %H:%M:%S') }}"
        response_variable: prices
    sensor:
      - name: tibber_forecast
        state: "{{ prices.prices.values() | first | selectattr('start_time', '<=', now()) | map(attribute='price') | list | last }}"
        attributes:
          prices: >
            {% set ns = namespace(prices=[]) %}
            {% for i in prices.prices.values() | first %}
              {% set n = dict(start_time = i.start_time.isoformat(), price = i.price) %}
              {% set ns.prices = ns.prices + [n] %}
            {% endfor %}
            {{ ns.prices }}

Hope that this is not a too stupid question :-(

froggi1930 commented 2 months ago

solved with a binary sensor