Stuck on getting sensor value out of an array

I have searched around the forum, and I can’t find anything that works for me. I have this code in Node-RED:

msg.topic = kjede + ' ' + navn;
msg.payload['pris'] = msg.payload.prices.B95.price;
msg.payload['tid'] =  timer +':'+minutter;

The array looks like this:

(Price of 95 octane gasoline, and it was updated 1 hour and 48 minutes ago.)

Then I have created a sensor in configuration. yaml:

mqtt:
  - sensor:
    - name: "Bensinpris på Valand"
      state_topic: "Driv Valand"
      unique_id: bensinpris-driv-valand
      force_update: true
      value_template: "{{ payload_json['pris'] }}"

    - name: "Bensinpris oppdatert på Valand"
      state_topic: "Driv Valand"
      unique_id: bensinpris-oppdratert-driv-valand
      force_update: true
      value_template: "{{ value_json.payload.tid }}"

But none of those two versions (or a few others I have tried) work. They keep saying variants of

Template variable error: ‘dict object’ has no attribute ‘payload’ when rendering ‘{{ value_json.payload.tid }}’

Depending on which version I try. Can somebody please tell me what I’m doing wrong here?

In bracket notation:

value_template: "{{ value_json['pris'] }}" 

Or in dot notation:

value_template: "{{ value_json.pris }}" 

Reference

MQTT Sensor - Owntracks example

1 Like

Thank you very much! :grin:

1 Like