Help: how to update a sensor state value with a shell command response

I have a shell_command that sends a curl command to my solar provider and has the following response:

{
    "updateRefreshRate": 3,
    "unit": "kW",
    "connections": [
        {
            "from": "PV",
            "to": "Load"
        },
        {
            "from": "Load",
            "to": "Grid"
        }
    ],
    "grid": {
        "status": "Active",
        "currentPower": 0.16
    },
    "load": {
        "status": "Active",
        "currentPower": 4.04
    },
    "pv": {
        "status": "Active",
        "currentPower": 4.2
    },
    "storage": null,
    "evCharger": null,
    "loadType": "Residential"
}

This shell command output can be saved in a response_variable called “json_value”.

I can create two sensor templates but I want to update the template state value with the shell_command response.

e.g.

sensor.grid_power = json_value.grid.currentPower
sensor.pv_power = json_value.pv.currentPower

How can I create a sensor that can update itself with the output of the shell_command?

Ideally I would like to use the homeassistant.update_entity service to update these values as required in an automation or script.

Set it up as a command line sensor:

command_line:
  - sensor:
      name: Power stuff
      command: YOUR CURL COMMAND
      scan_interval: 864000
      value_template: "{{ now() }}"
      json_attributes:
        - grid
        - pv

That will have a state of the last-refreshed timestamp, and the values you want as attributes. It’ll only update every ten days, and you can force an update with the homeassistant.update_entity as you suggest.

If you need the two sensors you list, set them up as template sensors reading their states from the command line sensor’s attributes.

1 Like

Thank you so much! It’s a much more elegant way of approaching the problem I had.

1 Like