Send multiple values in one rest command (and convert a text status to a number)

Hi everyone. I have the feeling this is a very basic question, but after hours on Google and the forum, I’m unable to find a solution and hope someone can help or at least point me in the right direction/documentation.

I have setup HA, which is properly receiving data from an eBus via the MQTT integration. Now I would like to push some information to a PRTG monitor server, which I can already do with a rest_command, and is working.

To do this, I defined the rest_command in my configuration.yaml

rest_command:
  prtg:
    url: https://xxx.xxx/some_id
    method: POST
    headers:
      accept: 'application/json, text/html'
    payload: '{"prtg":{"result":[{"channel":"{{ sensor_name }}","value":"{{ value }}"}]}}'
    content_type:  'application/json; charset=utf-8'
    verify_ssl: false

and I define the trigger in automations.yaml:

- alias: Push Value to PRTG
  trigger:
  - entity_id: sensor.ebusd_hmu_state_energy
    platform: state
  action:
    service: rest_command.prtg
    data_template:
      sensor_name: Compressor Modulation
      value: '{{ states("sensor.ebusd_hmu_state_energy") | int }}'
  id: ba170497e97b4e138d9fdd4ceca8eb80

This all works fine. Now I would like to add also the status of the compressor to this rest_command. This status gets reported in HA as a text (for example ‘frost_protection’, or ‘standby’), and to send it to PRTG I need to convert these codes back to their number. I have the (long) list for the conversion, but don’t know how to do this in the YAML.

The sensor I need to use for this is called ebusd_hmu_rundatastatuscode, and the conversion list looks like this (about 50 entries):

   { 34: 'Frost_protection' },
   { 100: 'Standby' },
   { 101: 'Heating_Compressor_shutdown' },
   { 102: 'Heating_Compressor_blocked' },
   { 103: 'Heating_Prerun' },
   { 104: 'Heating_Compressor_active' },
   { 107: 'Heating_Overrun' },
   { 111: 'Cooling_Compressor_shutdown' },
   { 112: 'Cooling_Compressor_blocked' },
   { 113: 'Cooling_Prerun' },
  ...

To add this extra information, I’m assuming I need to update the part in automations.yaml to the following:

- alias: Push Value to PRTG
  trigger:
  - entity_id: sensor.ebusd_hmu_state_energy
    platform: state
  - entity_id: sensor.ebusd_hmu_rundatastatuscode
    platform: state
  action:
    service: rest_command.prtg
    data_template:
      - sensor_name: Compressor Modulation
        value: '{{ states("sensor.ebusd_hmu_state_energy") | int }}'
      - sensor_name: Compressor Status
        value: --What to put here to convert the text to the correct number for the status?--
  id: ba170497e97b4e138d9fdd4ceca8eb80

My question is what to put in the value attribute of that new sensor in the code above?

Edit: in addition to my previous question, I figured out that my ‘assumed’ new part in automations.yaml will not work, as the rest_command.prtg expects only 1 channel/value pair in the payload. Adding the 2 sensor names and results in the action will not work. What would be an elegant way to define the rest_command.prtg so it would take all channel/value pairs in it’s value? Can I write a loop in the definition of the payload field of the rest_command.prtg to include all present channel/value pairs? If so, could someone help me how I would do that? (Let me know if I should create a seperate topic for this?)

Can anyone help?
Thank you in advance,
Wim

An example how to map text states to numbers was posted here:

Note however your rest command only has two variables and you can’t re-use them in the one action like this:

You would have to repeat the whole action:

actions:
  - action: rest_command.prtg
    data:
      sensor_name: Compressor Modulation
      value: '{{ states("sensor.ebusd_hmu_state_energy") | int }}'
  - action: rest_command.prtg
    data:
      sensor_name: Compressor Status
      value: --What...

Or provide two more unique variables for your rest command.

Also note the use of data_template is outdated (though still valid). You can use just data in actions with templates now. As is the use of service instead of action.

Thanks for the reply. It seems that should help me out for the first question. For readability, how would I go defining that lookup table on multiple lines (instead of on 1 line as in the example)?

Hi Tom_l,

Thanks for the help. I updated the code to reflect the newer way of writing.

Repeating the action is not an option. (This is due to the way the receiving system (in this case PRTG) works. Both values need to be passed in the same call)

Your remark is what I was writing about in the Edit at the end. Could I use a loop in the definition of the rest command, to add every name/value pair that is passed to it from the trigger?

So in automations.yaml:

  actions:
    - action: rest_command.prtg
      data:
        - sensor_name: Compressor Modulation
          value: '{{ states("sensor.ebusd_hmu_state_energy") | int }}'	
        - sensor_name: Compressor Status
          value: '{{ states("sensor.ebusd_hmu_rundatastatuscode") | int }}'

But then in the definition of the rest command what can I put under ‘payload’ if the output I would like is the following:

{
  "prtg": {
    "result": [
       {
         "channel": "Compressor Modulation",
         "value": 10
       },
       {
         "channel": "Compressor Status",
         "value": 104
       }
     ]
  }
}

I think as per your suggestion I could just add 2 more variables to the definition and rename them accordingly in the data of the action, but is it possible to do this dynamically, so without knowing in advance how many ‘sensors’ will be passed to the rest command?

The 2ay the expample was written, you can already insert newlines between items.

change your rest command to

rest_command:
  prtg:
    url: https://xxx.xxx/some_id
    method: POST
    headers:
      accept: 'application/json, text/html'
    payload: '{"prtg":{"result":{{result}} }}'
    content_type:  'application/json; charset=utf-8'
    verify_ssl: false

and your command to

  - variables:
      items:
        - channel: Compressor Modulation
          value: '{{ states("sensor.ebusd_hmu_state_energy") | int }}'	
        - channel: Compressor Status
          value: '{{ states("sensor.ebusd_hmu_rundatastatuscode") | int }}'
  - action: rest_command.prtg
    data:
      result: "{{ items }}"

Just list your items in the items variable.

Otherwise, I’m not sure I understand what you’re asking.

2 Likes

Thank you, this is what I was looking for. In the end it was not complicated, but somehow my brain refused to figure it out. Much appreciated!

Wim

Thanks Edwin_D. I was confused as I wanted newlines in between the {{ }}, but with your help I figured it out.