Advice on command line sensor, while moving to new format

it is far from the same…it shows all the elements orderly…

This properly formatted JSON separates the ‘STATUS’ and ‘STATS’ sections into arrays with their respective key-value pairs.


{
   "STATUS":[
      {
         "STATUS":"S",
         "When":1695794937,
         "Code":70,
         "Msg":"CGMiner stats",
         "Description":"cgminer 1.0.0"
      }
   ],
   "STATS":[
      {
         "BMMiner":"1.0.0",
         "Miner":"uart_trans.1.3",
         "CompileTime":"Wed Apr 27 15:19:27 CST 2022",
         "Type":"Antminer S19"
      },
      {
         "STATS":0,
         "ID":"BTM_SOC0",
         "Elapsed":78201,
         "Calls":0,
         "Wait":0,
         "Max":0,
         "Min":99999999,
         "GHS 5s":91721.26,
         "GHS av":91628.16,
         "rate_30m":91933.53,
         "Mode":2,
         "miner_count":3,
         "frequency":675,
         "fan_num":4,
         "fan1":5940,
         "fan2":6000,
         "fan3":6000,
         "fan4":6000,
         "temp_num":3,
         "temp1":66,
         "temp2_1":71,
         "temp2":64,
         "temp2_2":69,
         "temp3":65,
         "temp2_3":70,
         "temp_pcb1":"52-50-66-66",
         "temp_pcb2":"54-50-64-64",
         "temp_pcb3":"54-50-65-64",
         "temp_pcb4":"0-0-0-0",
         "temp_chip1":"57-55-71-71",
         "temp_chip2":"59-55-69-69",
         "temp_chip3":"59-55-70-69",
         "temp_chip4":"0-0-0-0",
         "temp_pic1":"42-40-56-56",
         "temp_pic2":"44-40-54-54",
         "temp_pic3":"44-40-55-54",
         "temp_pic4":"0-0-0-0",
         "total_rateideal":90000.0,
         "rate_unit":"GH",
         "total_freqavg":675,
         "total_acn":264,
         "total rate":91628.16,
         "temp_max":0,
         "no_matching_work":4,
         "chain_acn1":88,
         "chain_acn2":88,
         "chain_acn3":88,
         "chain_acn4":0,
         "chain_acs1":" oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo",
         "chain_acs2":" oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo",
         "chain_acs3":" oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo oo",
         "chain_acs4":"",
         "chain_hw1":0,
         "chain_hw2":1,
         "chain_hw3":3,
         "chain_hw4":0,
         "chain_rate1":"30874.29",
         "chain_rate2":"30830.31",
         "chain_rate3":"30016.67",
         "chain_rate4":"",
         "freq1":675,
         "freq2":675,
         "freq3":675,
         "freq4":0,
         "miner_version":"uart_trans.1.3",
         "miner_id":"no miner id now"
      }
   ],
   "id":1
}

i’ll try to see if i have time today, but maybe @petro knows faster (as i don’t use jason myself)

1 Like

while we are at it…

I would like to separate these values somehow:

so that I could display them in HA and later build stuff with graphna out from that data. I still fight my liquid cooling system on how to adjust the flow rate for best results. And these numbers, in an almost life view, would come in real handy for that. And if HA has em, I could grab them from other places too. Either stand alone with same syntax or from HA. Dont know jet. My pumps are controled by a TA Austria UVR16x2, but I have not hooked that one to HA jet, their API is a hint and miss thing…, but thats about the only thing one could probably bitch around a little bit about, realy, the hardware and support is nothing short of amazing, and its reliably… never ever had anything from TA dieing on me or anyone I know who has these systems… hence why i go with that and put up with the API policiy they run :slight_smile:

Not at all the same. Without seeing this structure, I’m not sure how you even managed to get to each piece of information.


If you only want it to access the py once…

command_line:
- sensor:
    name: S19-31-Board
    command: "python3 /config/shell/S19-31.py"
    unit_of_measurement: "°C"
    value_template: '{{ value_json.STATUS.When | as_datetime | as_local }}'
    device_class: timestamp
    state_class: measurement
    json_attributes:
    - STATUS
    - STATS
    - id

Then in the new template section…

template:
- sensor:
  - name: S19-31-Inlet-Temp-Board-1
    state: "{{ state_attr('sensor.s19_31_board', 'STATS')[1].temp1 }}"
    availability: "{{ state_attr('sensor.s19_31_board_1', 'STATS') is not none }}"
    device_class: temperature
    state_class: measurement
    unit_of_measure: ºC

- sensor:
  - name: S19-31-Outlet-Temp-Board-1
    state: "{{ state_attr('sensor.s19_31_board', 'STATS')[1].temp2_1 }}"
    availability: "{{ state_attr('sensor.s19_31_board_1', 'STATS') is not none }}"
    device_class: temperature
    state_class: measurement
    unit_of_measure: ºC

Getting the temperatures for each chip…

- sensor:
  - name: S19-31-Pcb-1-1-Board-1
    state: "{{ state_attr('sensor.s19_31_board', 'STATS')[1].temp_pcb1.split('-')[0] }}"
    availability: "{{ state_attr('sensor.s19_31_board_1', 'STATS') is not none }}"
    device_class: temperature
    state_class: measurement
    unit_of_measure: ºC
- sensor:
  - name: S19-31-Pcb-1-2-Board-1
    state: "{{ state_attr('sensor.s19_31_board', 'STATS')[1].temp_pcb1.split('-')[1] }}"
    availability: "{{ state_attr('sensor.s19_31_board_1', 'STATS') is not none }}"
    device_class: temperature
    state_class: measurement
    unit_of_measure: ºC
- sensor:
  - name: S19-31-Pcb-1-3-Board-1
    state: "{{ state_attr('sensor.s19_31_board', 'STATS')[1].temp_pcb1.split('-')[2] }}"
    availability: "{{ state_attr('sensor.s19_31_board_1', 'STATS') is not none }}"
    device_class: temperature
    state_class: measurement
    unit_of_measure: ºC
- sensor:
  - name: S19-31-Pcb-1-4-Board-1
    state: "{{ state_attr('sensor.s19_31_board', 'STATS')[1].temp_pcb1.split('-')[3] }}"
    availability: "{{ state_attr('sensor.s19_31_board_1', 'STATS') is not none }}"
    device_class: temperature
    state_class: measurement
    unit_of_measure: ºC

Just keep adding template sensors for each peice of information.

Thanks @petro :stuck_out_tongue:

I was thinking of a single sensor, with all data availbable as attributes, something like this line:

command_line:
- sensor:
    name: S19-31-Board
    command: "python3 /config/shell/S19-31.py"
    value_template: "OK"  # This can be any value, as the real attributes are set via the template
    attribute_templates:
      temp1: "{{ state_attr('sensor.miner_temperatures', 'STATS')['temp1'] }}"
      temp2: "{{ state_attr('sensor.miner_temperatures', 'STATS')['temp2'] }}"
      temp_pcb1: "{{ state_attr('sensor.miner_temperatures', 'STATS')['temp_pcb1'] }}"
      temp_pcb2: "{{ state_attr('sensor.miner_temperatures', 'STATS')['temp_pcb2'] }}"
      # Add more attributes as needed

But as you suggested will do fine :wink:

that’s what this does

command_line:
- sensor:
    name: S19-31-Board
    command: "python3 /config/shell/S19-31.py"
    unit_of_measurement: "°C"
    value_template: '{{ value_json.STATUS.When | as_datetime | as_local }}'
    device_class: timestamp
    state_class: measurement
    json_attributes:
    - STATUS
    - STATS
    - id

however command_line doesn’t support attribute_templates, only json_attributes.

Ah…see, that is why i asked you to be involved (as i don’t use json at all):thinking:
I know kindah how it works, but lack the details :crazy_face:

@mannebk Main difference with your original code is that the command line is only called once, and reads in all data. The rest of the template sensors just pick the data they need from that :wink:

hint and miss (the n is intentional)… lots of trys, and some input from other members here, who had somehow done it before me…

wow, you guys are something… I have to work my way through that input.

So I @petro, i did put your code into my yaml, no error while checking in dev tools.

But after restart I did get

Logger: homeassistant.config
Source: config.py:864
First occurred: 17:03:50 (6 occurrences)
Last logged: 17:03:50

Invalid config for [template]: [unit_of_measure] is an invalid option for [template]. Check: template->sensor->0->unit_of_measure. (See /config/templates.yaml, line 130).
Invalid config for [template]: [unit_of_measure] is an invalid option for [template]. Check: template->sensor->0->unit_of_measure. (See /config/templates.yaml, line 139).
Invalid config for [template]: [unit_of_measure] is an invalid option for [template]. Check: template->sensor->0->unit_of_measure. (See /config/templates.yaml, line 146).
Invalid config for [template]: [unit_of_measure] is an invalid option for [template]. Check: template->sensor->0->unit_of_measure. (See /config/templates.yaml, line 153).
Invalid config for [template]: [unit_of_measure] is an invalid option for [template]. Check: template->sensor->0->unit_of_measure. (See /config/templates.yaml, line 160).

so i googled a bit, and found, it must be unit_of_measurement

and while the sensors are there, i dont get any data into them. but i have no idea why.

grafik

Well, I’m not sure what you did wrong but my examples had unit_of_measurement. So it’s hard to say what you changed to make it not work.

EDIT: Ah I see, I copied your posts that had unit_of_measure, I should have noticed that. Anyways, with that change those templates should work. I can’t read what’s in your image becuase it’s not english.

sorry, “nicht verfügbar” means not available.

Did you create the original sensor in that post? They require it.

its copy paste from above, since you used same names as I had, I added -neu behind the name for “new” during testing

this is my command_line.yaml

and thats the templates.yaml

edit: i also added uuids to each, but that should not be a problem, right?

Then it should be working as intended or you’ll have errors in the logs

no errors concerning that sensors, i do have some errors thought, from stuff thats offline, like a printer in my shop, some cameras, due to zoneminder beeing offline right now, stuff like that.

hows the best way to go forward in troubleshooting?

Verify that the commandline sensor is working as intended. Then start looking at the attributes on it, verify that they are what is expected.

use the ‘developer tools/states’ to inspect
sensor.S19_31_board
would be nice to see what is states are :thinking: