Integrating SMARTFOX Energy Meter into Home Assistant

Hello,

I finally managed to include my SMARTFOX Energy Meter into Home Assistant. Albeit it is an older one, it might be still relevant for somebody else, so I would like to share it. I am not sure about the model, but on my.smartfox.at it shows Version: EMeterHC 12.3.4q

Anyway, it all builds on the idea of using curl to get the data from the IP on the local network, and then dissecting it an putting it to sensors accordingly.

You need to know your Smartfox’s IP address for this.

Make a new file

/config/scripts/smartfox.sh

with the content:

#!/bin/bash
#script to read analog out and relais from smartfox
#curls data, deletes string before and after relevant section, replaces unwanted <>, returns -1 if failed
timeout 25 curl http://192.168.0.54/v_outputs.html | sed 's/.*ANALOGOUT/\nANALOGOUT/' | sed 's/Restlaufzeit.*//' | awk '{gsub(/<[^>]*>/, " "); print}' | sed 's/%.*Status/Status/g' | sed 's/\s\+/;/g' || echo 'ANALOGOUT;-1;Status;-1;-1;-1;-1;' 

make it exectuable with:
chmod +x /config/scripty/smartfox.sh

You can try to execute it with ./config/scripts/smartfox.sh
and it should return something like this:
ANALOGAOUT;38;Status;1;1;1;0;
Which would mean that the analog output is at 38% and the relais 1 to 3 are activated, and relais 4 is off. If it can’t be parsed, it will default to -1 for all values.

Add this to the configuration.yaml:

templates:
    - name: "smartfox_aout_perc"
      unit_of_measurement: "%"
      state: "{{ states('sensor.smartfox_raw_data', 'state').split(';')[1] }}"
    - name: "smartfox_aout_power"
      unit_of_measurement: "W"
      #scale up with 100% value of analog load, which is 3800W in this case
      state: >-
        {% if states('sensor.smartfox_aout_perc') | int >= 0 and states('sensor.smartfox_aout_perc') | int <= 100 %}
          {{ (states('sensor.smartfox_aout_perc') | int) * 38 }}
        {% else %}
          -1
        {% endif %}
    - name: "smartfox_relais1_state"
      state: "{{ states('sensor.smartfox_raw_data', 'state').split(';')[3] }}"
    - name: "smartfox_relais2_state"
      state: "{{ states('sensor.smartfox_raw_data', 'state').split(';')[4] }}"
    - name: "smartfox_relais3_state"
      state: "{{ states('sensor.smartfox_raw_data', 'state').split(';')[5] }}"
    - name: "smartfox_relais4_state"
      state: "{{ states('sensor.smartfox_raw_data', 'state').split(';')[6] }}"

command_line:
  - sensor:
      name: 'smartfox_raw_data'
      command: "/bin/bash /config/scripts/smartfox.sh"
      scan_interval: 30  # Adjust the frequency of polling as necessary

That’s it, now there should be the relais states and analog output state available as entities in your home assistant.

It should also be easily adjustable for other use cases. The way I did it was to first try to curl it in the home assitant terminal (can be done from the web with the ssh terminal plugin) and see what is there. Paste this output into ChatGPT and ask it to parse it so that you get the values you want. Be aware that e.g. grep, awk, sed, … are a slimmed-down version of busybox, so some options are not available.

Hope it helps somebody!

1 Like

Hello,

I’ve also been struggling with the integration of my Smartfox device into the energy dashboard. As it seems, I have the same type and version as you have. The good part: there is actually an API which delivers the desired values as JSON. The specification can be found here.

I use the following code in configuration.yaml to parse the values:

rest:
  - resource: http://dafi-smartfox/all
    sensor:
      - name: "Netz-Bezug"
        unique_id: "netz_bezug_wh"
        value_template: "{{ value_json.energy_in }}"
        device_class: energy
        state_class: "total_increasing"
        unit_of_measurement: Wh
      - name: "Netz-Lieferung"
        unique_id: "netz_lieferung_wh"
        value_template: "{{ value_json.energy_out }}"
        device_class: energy
        state_class: "total_increasing"
        unit_of_measurement: Wh
      - name: "Smartfox-Verbrauch"
        unique_id: "smartfox_wh"
        value_template: "{{ value_json.energy_sf }}"
        device_class: energy
        state_class: "total_increasing"
        unit_of_measurement: Wh
      - name: "PV-Produktion"
        unique_id: "pv_prod_wh"
        value_template: "{{ value_json.PvEnergy[0] }}"
        device_class: energy
        state_class: "total_increasing"
        unit_of_measurement: Wh
      - name: "Netz-Aktuell"
        unique_id: "netz_aktuell_w"
        value_template: "{{ value_json.power_io }}"
        device_class: power
        state_class: "measurement"
        unit_of_measurement: W
      - name: "Smartfox-Aktuell"
        unique_id: "smartfox_aktuell_w"
        value_template: "{{ value_json.power_sf }}"
        device_class: power
        state_class: "measurement"
        unit_of_measurement: W
      - name: "PV-Aktuell"
        unique_id: "pv_aktuell_w"
        value_template: "{{ value_json.PvPower[0] }}"
        device_class: power
        state_class: "measurement"
        unit_of_measurement: W

This seems to work just fine, but I’ve got a problem with some execessively high values. I don’t quite get the problem yet. I’ve tried to filter those values using the following code, but that didn’t help either.

- platform: filter
  name: "PV-Produktion gefiltert"
  unique_id: "filtered_pv_prod_wh"
  entity_id: sensor.pv_produktion
  filters:
    - filter: outlier
      window_size: 7
      radius: 3.0

I still get strange values in the energy dashboards.

Any ideas?