Create Custom Sensors with Multiple Entities

Hi there,

I currently have a handful of custom sensors that I use in the below format:

    sensors:
      cambackgarden_motion:
        friendly_name: "CAM BackGarden Motion"
        value_template: "{{states('input_text.cambackgarden_motion')}}"
        unique_id: sensor.cambackgarden_motion

I stole this template from somewhere years ago, and If I read the HA docs correctly this is now a legacy way of doing it. I use these to send motion states from my Blue Iris instance using a custom script.

What I want to do, is have a custom sensor with multiple (two) entities (if thats the right term), but my YAML is rubbish and I’ve no idea if this is possible, let alone figure it out!

Then I’ll send the sensor two states, “Motion_ABC” and “person:86%”… where the first is the location of the motion, the latter what the AI found and it’s confidence.

Any help on the sensor would be greatly appreciated.

You need to be more specific on what you want the output to be in relation to the input. Here’s an example that just puts two made-up sensors’ states together into a string separated by a space:

template:
  - sensor:
      - name: "My example sensor"
        state: "{{ states('sensor.motion_location') ~ ' ' ~ states('sensor.iris_output') }}"

Understood. I don’t know the right terminolgy to use to describe what I’m after, so let me come at it from a different angle.

This is a completely unrelated device that has got multiple entities. So I can see Tempreture, Humidity, Battery etc all grouped on a single device, in their own entities… That’s what I’m after with a custom sensor.

My Sensor

  • Motion
  • Type

In terms of the input, currently I use a powershell script to JSON POST to HA… snippet of which is below:

$entityStatus = "sensor.cam"+"$camera"+"_motion"
$entityStatusName = "CAM " + "$camera" + " Motion"
$headers = @{"Authorization"="Bearer $HAToken";}

    $params = @{
     "state"="$Zone";
     "attributes"= @{
        "friendly_name"="$entityStatusName";
        "icon"="$icon";
        }
    }

    $params = $params | ConvertTo-Json
    Invoke-RestMethod -Uri "$HAUrl/api/states/$entityStatus" -Method POST -Headers $headers -Body ([System.Text.Encoding]::UTF8.GetBytes($params)) -ContentType "application/json"

BlueIris passes parameters to the script which sets the $camera and $zone. So here I would include an additional parameter for $type and pass it in the same JSON or even a second POST so the single sensor in HA has both entities up to date with different information.

I hope that makes sense, and it’s possible!

Terminology confusion: a sensor is an entity, which has a single mandatory state and zero or more attributes. I think you’re looking to add attributes. Your code already creates two — just add the extras that you want.

Here’s an example: this phone sensor has a state of 0.0W and separate current and voltage attributes (plus others, including the icon and friendly_name that you’re setting):

Ohhhhhhhhhhhhhhhh! Damn!

Thank you, that makes complete sense. Now you’ve laid it out, yes whacking in another field to the $params and passing this instantly opens up another attribute on the sensor. Exactly what I was after and much simpler!

Thanks @Troon !

1 Like