Help with JSON formatting MQTT Sensor

Hi

I have Node-Red going out to my mining provider via their API and dumping all data into an MQTT topic. I setup an MQTT Sensor that works fine.

- platform: mqtt
  name: "Total Hash Rate"
  state_topic: "mining/pool"
  qos: 0
  unit_of_measurement: "MH/s"
  value_template: "{{ value_json.user.hash_rate / 1000 | round(4) }}"

The problem is the mining provider uses a . in their fields on some object fields. See below.

  "workers": {
    "Miner.1": {
      "connected": true,
      "hash_rate": 582990.8,
      .....

Im not sure how to extract the data into a sensor as using a . won’t work as part of the name

Im sure this is a simple fix but Iv tried a few options using ["workers’][“Miner.1”] but I can’t seem to make it work.

Any suggestions?

Have you tried using bracket notation?

value_template: "{{ value_json.workers["Miner.1"].hash_rate / 1000 | round(4) }}"

Yeah I have. With above line I get

Error loading /config/configuration.yaml: while parsing a block mapping in "/config/components/sensors.yaml", line 82, column 3 expected <block end>, but found '<scalar>' in "/config/components/sensors.yaml", line 87, column 43

EDIT:

It works with single quotes. Thanks so much.

value_template: "{{ value_json.workers['Miner.1'].hash_rate / 1000 | round(4) }}"

Oh, oops. My bad.

I had no quotes around the template when I tested it and when I copy/pasted it I put the wrong quote types around the outside.

1 Like