Creating two sensors from one entity updated via RESTful API

I have an entity that is updated via POST to the HA RESTful API.

Each post contains a number of attributes. One of those attributes is the value I want, another is the sourceIPAddress the post came from.

I want to store data in a sensor value. This works to get the value I want for every post, but obviously it only stores the most recent value regardless of what IP address the post came from.

template:
  - sensor:
     - name: "rateCount" 
       state: "{{ state_attr('sensor.ratereceived', 'rateCount') }}"

How do I add a condition that only updates the value of this sensor if another attribute matches an IP address? Something like this:

template:
  - sensor:
     - name: "rateCountOutdoor"
       value_template: >-
          {% if state_attr('sensor.ratereceived', 'sourceIpAddress') == '192.168.1.116' %}
            state_attr('sensor.ratereceived', 'rateCount')
          {% endif %}

Thanks very much!

{% if state_attr('sensor.ratereceived', 'sourceIpAddress') == '192.168.1.116' %}
{{ state_attr('sensor.ratereceived', 'rateCount') }}
{% else %}
{{ "something when not that ip" }}
{% endif %}`

Try it out first in the Dev Tools > Templates…saves restarts :slight_smile:

That works perfectly, thank you!

Thanks too for the Dev Tools > Templates tip - I had no idea that was available, it’s a big help.

I haven’t got it working in configuration.yaml yet but it’s a work in progress.

As an extension question: is this an appropriate way to store values as they are received and store them in a unique sensor for each source? There is a new post every few seconds and I’m displaying the values in Lovelace, but also acting on them in automations.

As a tip… create a separate template.yaml and refer to that from the configuration.yaml.
Template entities can be loaded without restart and this allows you to edit the template file without possibly damaging the core config and…keeping it neat
On the sensor, depends, if it is data all related to the same onject/device then I myself would prefer one sensor for all values. But e.g. I have sensors to gasstation fuel prices, each station ends up in 3 sensors as I need the fuel price in the state value so I can historize them… 15 stations around me leading to 45 sensors
On the update freq…depends too… e.g. I donot care for the outisde temp update < 30 mins but I do care about the energy use every minute (to catch peaks)

Thank you, that’s another great tip!

In this case each sensor represents a different location, but the data from them comes into the same RESTful entity in HA. I wrote the REST calls so I could actually direct them to different entities in Home Assistant, but I figured it was worth working this out because there’ll be other types of data I want to receive later that I don’t control.

OK, just take into account that the state-value is used for history/stats etc. so if you wants charts/graphs/etc. then you have little choice then to create multiple sensors. There are more complex solutions via a InfluxDb/Grafana but I will not elaborate in this thread…you can find them elsewhere

1 Like