Multiple entities from one sensor?

Good afternoon all, I have been researching how to ‘use’ some data from scraping an online hub from my MyEnergi hub that controls a Eddi at my house. I have got the following code in my Configuration.YAML file:

# Sensors
sensor:
  - platform: rest
    name: Eddi
    resource: https://s8.myenergi.net/cgi-jstatus-E
    device_class: power
    scan_interval: 60
    timeout: 30
    authentication: digest
    username: ***********
    password: ***********
    headers:
      User-Agent: Home Assistant
      Content-Type: application/json
      Accept: application/json
    unit_of_measurement: W
    value_template: >
      {% if value_json.eddi[0].gen is defined %}
        {{ value_json.eddi[0].gen }}
      {% else %}
        0
      {% endif %}
    json_attributes_path: '$.eddi[0]'
    json_attributes:
      - dat       # Date (UTC)
      - tim       # Time (UTC)
      - ectp1     # Internal CT Value
      - ectp2     # Grid CT Value
      - ectp3     # Generation CT Value
      - ectt1     # Internal CT Name
      - ectt2     # Grid CT Name
      - ectt3     # Generation CT Name
      - frq       # Grid Frequency
      - vol       # Grid Voltage
      - zmo       # Zappi Mode
      - sta       # Zappi Status
      - pst       # Charge Status
      - che       # KWh Diverted (This Session)
      - fwv       # Firmware Version
      - gen       # Generation now
      - grd       # From Grid Now

It is working, as I can create a sensor in my HA UI that shows the generation, or ‘gen’ as defined above. What I’d like to do is show various bits of data, for example generation, grid supply now, KWh diverted, etc. All of this data is available but the only way I know to ‘change’ the data shown to me is by changing the attribute above in the ‘value_template’ section. Is there a way to ‘read’ all of the data then create different entities based on that data?

PS: Noob here. Only got this working through a lot of trial and error!

Thanks,
Matt

The typical way to expose attributes on a sensor is to create a template sensor which is based on the attribute.

Example:

sensor:
  - platform: template
    sensors:
      entrance_dashboard_battery_level:
        friendly_name: "Entrance Dashboard Battery Level"
        device_class: battery
        unit_of_measurement: '%'
        value_template: "{{ states.sensor.entrance_dashboard.attributes.batteryLevel }}"

Thanks for the quick reply Tony, so I need to re-write my code in the Confi.YAML or is the ‘template sensor’ code that you’ve shown written somewhere else?

This would be in the configuration.yaml.

Btw, you can split the configuration up into multiple files. There are multiple ways to do this and examples of includes and packages are given in the documentation. I do not recall, maybe someone else can comment, if template sensors can be configured through the UI.

Ok, many thanks for your early assistance with this. From what I understand from the bit of reading I’ve done so far, this specific data i’m grabbing (the MyEnergi ‘Hub’ data) is stored online and can be grabbed using cgi or json or something with the ‘RESTful’ sensor platform. What I really need to to is get ‘something’ to go and grab all the data every 5mins or so, then I can make sensors for the individual bits that I want to show on my UI…

Using your configuration, here is what I would do. :slight_smile:

sensor:
  - platform: rest
    name: Eddi
    resource: https://s8.myenergi.net/cgi-jstatus-E
    device_class: power
    scan_interval: 60
    timeout: 30
    authentication: digest
    username: ***********
    password: ***********
    headers:
      User-Agent: Home Assistant
      Content-Type: application/json
      Accept: application/json
    unit_of_measurement: W
    value_template: >
      {% if value_json.eddi[0].gen is defined %}
        {{ value_json.eddi[0].gen }}
      {% else %}
        0
      {% endif %}
    json_attributes_path: '$.eddi[0]'
    json_attributes:
      - dat       # Date (UTC)
      - tim       # Time (UTC)
      - ectp1     # Internal CT Value
      - ectp2     # Grid CT Value
      - ectp3     # Generation CT Value
      - ectt1     # Internal CT Name
      - ectt2     # Grid CT Name
      - ectt3     # Generation CT Name
      - frq       # Grid Frequency
      - vol       # Grid Voltage
      - zmo       # Zappi Mode
      - sta       # Zappi Status
      - pst       # Charge Status
      - che       # KWh Diverted (This Session)
      - fwv       # Firmware Version
      - gen       # Generation now
      - grd       # From Grid Now

  - platform: template
    sensors:
      eddi_charge_status:
        friendly_name: Eddi Charge Status
        value_template: "{{ states.sensor.eddi.attributes.pst }}"
      eddi_grid_voltage:
        friendly_name: Eddi Grid Voltage
        unit_of_measure: "V"
        device_class: voltage
        value_template: "{{ states.sensor.eddi.attributes.vol }}"
        icon_template: mdi:transmission-tower

Just a simple example, do check out the documentation for the Template Sensor, you can set the device class (which will influence how the value is displayed and the icon), the unit of measurement, icon, etc…

Edit: Added additional sensor template example!

1 Like

Tony! Thank you! This is exactly the help I needed. Just this small bit of code is enough for me to get my head around it and continue to tinker. Thanks very much. It makes a lot of sense, and I’ve already got two sensors working. Perfect. Thanks so much.

To answer the specific question…

Basically what you have started. You need the REST sensor to gather all the values with one request, then you the template sensors to expose attribute values you want in the UI or to use directly. As of version 0.115 of Home Assistant Core you can more easily trigger automation and base conditions off attributes of an entity. If you inspect the details of the REST sensor you have created for Eddi in the UI you will see all the attributes listed that are defined.

To adjust how often the REST sensor updates set the scan_interval: value to the number of seconds, 5 minutes being 300 seconds, to influence how often it is updated. Also based on the type of information and if you use template sensors, I would also set force_update: true the the REST sensor so that you have values such as voltage updated even if they did not change.

Welcome, I remember the learning curve years ago and I’m happy to help when I can!

Is the

unit_of_measurement: W
    value_template: >
      {% if value_json.eddi[0].gen is defined %}
        {{ value_json.eddi[0].gen }}
      {% else %}
        0
      {% endif %}

bit required in the REST bit?

Personally I use a more generic value for the REST sensor, something that identifies Eddi overall status? You need some value, it could simply be text like Ok. :slight_smile:

eddi_ha

Looking good already! Thanks again. :slight_smile:

@mattlivermore could you possibly post your now-working config? I’m trying to do the same as you, get Eddi talking to home assistant. I have looked at some of the examples further up the thread but I must be missing something.
thanks
Ian

Hi Ian,

I am now using Node-Red to call the MyEnergi API once a minute for my Eddi / Zappi data. I have real time data (5sec updates) from my Sofar inverter giving good usable data. I’m not great with the Node-Red stuff and have had a lot of help in the Facebook group. Feel free to join https://www.facebook.com/groups/2477195449252168

Whilst it’s title indicates it’s a Sofar group (which you may or may not have) - there are some great Node-Red tutorials and then some very useful and customisable Eddi ‘flows’ which are easy to change for your own needs.

Hope this helps in some way!

Thanks,
Matt

This is what my Eddi (and MyEnergi) stuff looks like at the mo!

Please Matt, im trying to get my Eddie integrated with HA, i’ve copied in your code as above.

And ive put in my username/password in, but cant find any sensors listed.

Have i missed something.

What is your level of HA expertise: do you already have sensors up and running? Where have you put the code? Did you restart HA? Is there anything in your logs?

Yes i have running HA with Shellys, running my heating with weather data, TOU, PV input.

Im not a coder, more of a copy and paste guy, very basic.

Ive put you code into my YAML file.

Rebooted the system.

Thx

Are you sure you only have one sensor: section in configuration.yaml? There should only be one, with all the sensors gathered under it.

ok not many entrys.

I can put that right