Template sensor append to namespace

Hi there,

Trying to extract the hourly electricity prices for MY provider from a rest sensor that spits them out for ALL available providers.

{% set ns = namespace(datum='', prijs='') -%}
{% for i in states.sensor.enever_stroomprijs_vandaag.attributes.data %}
{% set ns.datum = ns.datum + i.datum %}
{% set ns.prijs = ns.prijs + i.prijsZP %}
{% endfor %}
{{ ns }}

What I would like to see is a list like so (nicely paired):

datum: '2023-02-06 00:00:00’, prijs: '0.330898'
datum: '2023-02-06 01:00:00', prijs: '0.329361'
datum: '2023-02-06 02:00:00’, prijs: '0.331430'

etc.

What I am getting from the template above is:

<Namespace {'datum': '2023-02-06 00:00:002023-02-06 01:00:002023-02-06 02:00:002023-02-06 03:00:002023-02-06 04:00:002023-02-06 05:00:002023-02-06 06:00:002023-02-06 07:00:002023-02-06 08:00:002023-02-06 09:00:002023-02-06 10:00:002023-02-06 11:00:002023-02-06 12:00:002023-02-06 13:00:002023-02-06 14:00:002023-02-06 15:00:002023-02-06 16:00:002023-02-06 17:00:002023-02-06 18:00:002023-02-06 19:00:002023-02-06 20:00:002023-02-06 21:00:002023-02-06 22:00:002023-02-06 23:00:00', 'prijs': '0.3308980.3293610.3314300.3274250.3249690.3356530.3680090.3982340.4136500.3982100.3793950.3533310.3125060.3122030.3193790.3364640.3754260.3961650.4137830.4039580.3807620.3568890.3399730.341788'}>

so first all dates and then all prices…

Help! :slight_smile:

(removed, as I’ve been corrected below :slight_smile: )

1 Like
{% set ns = namespace(items=[]) -%}
{% for i in states.sensor.enever_stroomprijs_vandaag.attributes.data %}
{% set ns.items = ns.items + [ {'datum': i.datum, 'prijs': i.prijsZP} ] %}
{% endfor %}
{{ ns.items }}
1 Like

You can build a dictionary using dictionary methods: <dict>.from_keys(...), or <dict>.fromkeys(...). Or using the literal dict.

I have examples all over the forums as replies for different ways to make a dictionary from scratch. Or append items to a dictionary. Or adding keys to a dictionary.

You can also take advantage of **items in combination w/ the methods I mentioned above.

Super! Many thanks for the quick response and elegant solution! Works like a charm.

Final template sensor now looks like this:

- platform: template
  sensors:
    zonneplan_stroom_prijs:
      friendly_name: "Zonneplan Stroomprijs"
      unit_of_measurement: €/kWh
      device_class: monetary
      value_template: >-
        {% set t = now().hour %}
        {% set prijs = state_attr('sensor.enever_stroomprijs_vandaag', 'data')[t].prijsZP | float %}
        {{ prijs }}
      attribute_templates:
        hourly: >-
          {% set ns = namespace(items=[]) %}
          {% for i in states.sensor.enever_stroomprijs_vandaag.attributes.data %}
          {% set ns.items = ns.items + [ {'datum': i.datum, 'prijs': i.prijsZP} ] %}
          {% endfor %}
          {% for i in states.sensor.enever_stroomprijs_morgen.attributes.data %}
          {% set ns.items = ns.items + [ {'datum': i.datum, 'prijs': i.prijsZP} ] %}
          {% endfor %}
          {{ ns.items }}

And a nice graph based on this sensor:

with code as follows:

type: custom:apexcharts-card
experimental:
  color_threshold: true
graph_span: 48h
span:
  start: day
now:
  show: true
  label: Now
yaxis:
  - id: first
    min: -0.1
    max: 0.8
    decimals: 3
    apex_config:
      tickAmount: 9
header:
  show: true
  title: Zonneplan (enever) 48h (€/kwh)
series:
  - entity: sensor.zonneplan_stroom_prijs
    stroke_width: 2
    float_precision: 3
    type: column
    opacity: 1
    data_generator: |
      return entity.attributes.hourly.map((record, index) => {
        return [record.datum, record.prijs];
      });
    color_threshold:
      - value: 0.2
        color: lightgreen
      - value: 0.25
        color: green
      - value: 0.3
        color: blue
      - value: 0.35
        color: orange
      - value: 0.4
        color: red